生成注册验证码
更新: 2024/7/9 字数: 0 字 时长: 0 分钟
INFO
由于是期末作业所以写详细给同学们学习
class Captcha {
public static function create() {
$charset = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'; //定义验证码字符
$code = ''; //初始化验证码字符串
for ($i = 0; $i < 5; $i++) { // 0到验证码数量的次数
$code .= $charset[mt_rand(0,strlen($charset) - 1)];
//使用 mt_rand() 函数从字符集中随机选择字符
// 接着用 strlen() 获取字符集长度并减1 并拼接成验证码字符串
// 因为索引从0开始而字符长度从1开始算,为了获取到所有字符所以要-1
}
return $code; // 把生成的验证码返回
}
public static function show($code, $x = 250, $y = 62){
$im = imagecreate($x, $y); //创建图像资源
imagecolorallocate($im, mt_rand(50, 200), mt_rand(0, 155), mt_rand(0, 155));
$fontcolor = imagecolorallocate($im, 255, 255, 255); //分配字体颜色
$fontfile = realpath('C:/windows/Fonts/simsun.ttc'); //设置字体文件路径
for ($i = 0;$i < strlen($code); $i++) { //从0循环到code的次数
imagettftext( //调用imagettftext函数在图像上绘制文字
$im, //图像资源
30, //文字大小
mt_rand(-20, 20), //文字角度
32 + $i * 40, //x轴坐标角度
mt_rand(30, 50), //y轴坐标角度
$fontcolor, //文字颜色
$fontfile, //文字字体
$code[$i] //打印文字
);
}
for ($i = 0; $i < 8; $i++) {
$linecolor = imagecolorallocate($im,
mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //分配干扰线颜色
imageline(
$im, //图像资源
mt_rand(0, $x), 0, //(X1, Y1)
mt_rand(0, $x), $y, //(X2, Y2)
$linecolor //干扰线颜色
);
}
for ($i = 0; $i < 250; $i++) {
imagesetpixel(
$im, //图像资源
mt_rand(0, $x), // (X1)
mt_rand(0, $y), // (X2)
$fontcolor //噪点颜色
);
}
header('Content-Type: image/png');
imagepng($im); //输出图像为PNG格式
imagedestroy($im); //销毁图像资源
}
}
$code = Captcha::create();
Captcha::show($code);
以上的完整代码,用于生成和显示验证码图像
下面是对类中方法和属性的逐行解释
1. 类定义
定义了一个名为 Captcha
的新类
class Captcha
2. 创建验证码文本的方法
这是一个静态方法,用于生成一个由随机字符组成的验证码字符串
public static function create() {
3. 定义字符集
定义了验证码可使用字符的集合,排除了容易混淆的字符如I, O, 0等
$charset = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
4. 初始化验证码字符串
$code = '';
5. 获取字符集长度
strlen($charset) - 1;
6. 生成验证码
循环指定的次数,使用 mt_rand()
函数从字符集中随机选择字符,并拼接成验证码字符串
for ($i = 0; $i < $count; $i++) {
$code .= $charset[mt_rand(0, strlen($charset) - 1)];
}
7. 返回生成的验证码
return $code;
8. 显示验证码图像的方法
这是另一个静态方法,接受验证码字符串并显示对应的验证码图像,默认图像宽为250像素,高为62像素
public static function show($code, $x = 250, $y = 62) {
9. 创建图像资源
$im = imagecreate($x, $y);
10. 分配随机背景色
imagecolorallocate($im, mt_rand(50, 200), mt_rand(0, 155), mt_rand(0, 155));
11. 分配字体颜色
$fontcolor = imagecolorallocate($im, 255, 255, 255);
12. 字体文件路径
$fontfile = realpath('C:/windows/Fonts/simsun.ttc');
13. 绘制验证码文字
使用imagettftext()
函数在图像上逐个字符地绘制验证码$im
用于选择图像30
用于设置文字大小mt_rand(-20, 20)
用于设置文字角度32 + $i * 40
用于设置x轴坐标mt_rand(30, 50)
用于设置y轴坐标$fontcolor
用于设置文字颜色$fontfile
用于设置文字字体$code[$i]
用于设置文字
for ($i = 0; $i < strlen($code); $i++) {
imagettftext(
$im,
30,
mt_rand(-20, 20),
32 + $i * 40,
mt_rand(30,50),
$fontcolor,
$fontfile,
$code[$i]
);
}
14. 发送图像内容类型头信息
让浏览器了解这个页面是一个png格式的图片
header('Content-Type: image/png');
15. 绘制干扰线
使用imageline函数绘制一条线段$im
用于选择图像mt_rand(0, $x)
用于设置第一个点的x坐标0
用于设置第一个点的y坐标mt_rand(0, $x)
用于设置第二个点的x坐标$y
用于设置第二个点的y坐标$linecolor
用于设置文字颜色
$linecolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imageline($im, mt_rand(0, $x), 0, mt_rand(0, $x), $y, $linecolor);
16. 绘制噪点
使用imagesetpixel函数在图像上设置单个像素点$im
用于选择图像mt_rand(0, $x)
用于设置像素点的x坐标mt_rand(0, $y)
用于设置像素点的y坐标$fontcolor
用于设置文字颜色
imagesetpixel($im, mt_rand(0, $x), mt_rand(0, $y), $fontcolor);
17. 输出图像为PNG格式:
输出的格式同上是png
imagepng($im);
18. 销毁图像资源
解放内存
imagedestroy($im);
19. 生成并显示验证码
调用captcha类和它的show方法
$code = Captcha::create();
Captcha::show($code);
如果你发现这篇指南有用,或者有改进建议,请随时联系我们或参与讨论。🎉 🎉 🎉