PHP中GD库的使用

由于我要用到php的验证码

php 复制代码
<?php
session_start();

// 生成验证码
$random_code = substr(md5(uniqid(mt_rand(), true)), 0, 6);

// 将验证码保存到 session 中
$_SESSION['captcha'] = $random_code;

// 创建图片
$font = 6;
$image_width = 100;
$image_height = 40;

// 创建图像
$image = imagecreatetruecolor($image_width, $image_height);

// 设置背景色
$background_color = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $background_color);

// 设置验证码颜色
$text_color = imagecolorallocate($image, 0, 0, 0);

// 在图像上绘制验证码
imagestring($image, $font, 15, 10, $random_code, $text_color);

// 输出图像
header("Content-Type: image/png");
imagepng($image);

// 释放内存
imagedestroy($image);
?>

php.ini 配置文件中开启 GD 扩展即可使用

相关推荐
丰锋ff3 小时前
基于 Qt 的智能门禁系统(二)
开发语言·qt
fpcc3 小时前
跟我学C++中级篇——编译期的条件选择
开发语言·c++
SomeB1oody4 小时前
【RustyML入门】6.3. 并行归约
开发语言·后端·机器学习·rust·教程
mqiqe5 小时前
AgentScope Java 2.0 协议集成全景解析:A2A、AG-UI、Agent Protocol 三大开放协议实战指南
java·开发语言·ui
lzhdim5 小时前
12、JavaScript常见的内存泄露问题 - JavaScript学习系列文章
开发语言·前端·javascript·学习·ecmascript
脉动数据行情5 小时前
Java 实现台股 TWSE/TPEx 行情采集(个股 + K 线)
java·开发语言·twse·tpex·台股
爱学习的小邓同学5 小时前
Golang --- (1)第一个Golang程序
开发语言·后端·golang
zx_741484816 小时前
【Python 入门】面向对象基础:类、对象、成员变量与构造方法
开发语言·python
那年窗外下的雪.6 小时前
AIDC 学习日志|第 15 天|EVPN 多归属故障收敛与撤销机制
网络·学习·php
liangshanbo12157 小时前
虚拟列表深度面试题整理
java·开发语言·前端