PHP如何生成带背景的图形验证码功能
发布时间:2023-05-14 05:43:09
图形验证码是当前常用的验证码之一,要求用户在输入验证码时,根据验证码图片上的文字或数字输入正确的验证码,这种方式可以有效降低机器人和脚本攻击的风险。本文将介绍如何使用PHP生成带背景的图形验证码。
1. 准备工作
生成验证码前需要确定以下几个参数:
- 验证码长度
- 验证码字符集合
- 图片宽度
- 图片高度
- 字体大小
- 字体颜色
- 干扰线条数量
- 干扰点数量
在本文中,我们使用以下参数:
- 验证码长度:4
- 验证码字符集合:数字和字母
- 图片宽度:120px
- 图片高度:40px
- 字体大小:20px
- 字体颜色:随机
- 干扰线条数量:3
- 干扰点数量:50
2. 随机生成验证码
使用PHP生成随机验证码可以用以下代码实现:
//验证码长度
$length = 4;
//验证码字符集合
$charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$code = '';
for ($i = 0; $i < $length; $i++) {
$code .= $charset[mt_rand(0, strlen($charset) - 1)];
}
3. 生成背景图
使用PHP生成带背景的验证码可以用以下代码实现:
//图片宽度
$width = 120;
//图片高度
$height = 40;
//创建图片资源
$image = imagecreatetruecolor($width, $height);
//图片背景色
$bgColor = imagecolorallocate($image, mt_rand(200,255), mt_rand(200,255), mt_rand(200,255));
//填充背景色
imagefill($image, 0, 0, $bgColor);
//添加干扰线条
for ($i = 0; $i < 3; $i++) {
$lineColor = imagecolorallocate($image, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200));
imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $lineColor);
}
//添加干扰点
for ($i = 0; $i < 50; $i++) {
$pointColor = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($image, mt_rand(0, $width), mt_rand(0, $height), $pointColor);
}
//输出验证码图片
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
4. 添加验证码文字
将生成的验证码文字添加到背景图片中:
//在背景图片上添加验证码文本 $fontFile = 'msyhbd.ttf';//字体文件 $fontSize = 20;//字体大小 $fontColor = imagecolorallocate($image, mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150));//字体颜色 $fontBox = imagettfbbox($fontSize, 0, $fontFile, $code); $fontWidth = $fontBox[2] - $fontBox[0];//字体宽度 $fontHeight = $fontBox[1] - $fontBox[7];//字体高度 $x = ($width - $fontWidth) / 2;//水平居中 $y = ($height - $fontHeight) / 2 + $fontHeight;//垂直居中 imagettftext($image, $fontSize, 0, $x, $y, $fontColor, $fontFile, $code);//添加文字
5. 完整代码
将以上代码整合在一起,即可得到完成的图形验证码生成代码:
<?php
session_start();
//验证码长度
$length = 4;
//验证码字符集合
$charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$code = '';
for ($i = 0; $i < $length; $i++) {
$code .= $charset[mt_rand(0, strlen($charset) - 1)];
}
//将验证码保存到session中
$_SESSION['captcha'] = strtolower($code);
//图片宽度
$width = 120;
//图片高度
$height = 40;
//创建图片资源
$image = imagecreatetruecolor($width, $height);
//图片背景色
$bgColor = imagecolorallocate($image, mt_rand(200,255), mt_rand(200,255), mt_rand(200,255));
//填充背景色
imagefill($image, 0, 0, $bgColor);
//添加干扰线条
for ($i = 0; $i < 3; $i++) {
$lineColor = imagecolorallocate($image, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200));
imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $lineColor);
}
//添加干扰点
for ($i = 0; $i < 50; $i++) {
$pointColor = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($image, mt_rand(0, $width), mt_rand(0, $height), $pointColor);
}
//在背景图片上添加验证码文本
$fontFile = 'msyhbd.ttf';//字体文件
$fontSize = 20;//字体大小
$fontColor = imagecolorallocate($image, mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150));//字体颜色
$fontBox = imagettfbbox($fontSize, 0, $fontFile, $code);
$fontWidth = $fontBox[2] - $fontBox[0];//字体宽度
$fontHeight = $fontBox[1] - $fontBox[7];//字体高度
$x = ($width - $fontWidth) / 2;//水平居中
$y = ($height - $fontHeight) / 2 + $fontHeight;//垂直居中
imagettftext($image, $fontSize, 0, $x, $y, $fontColor, $fontFile, $code);//添加文字
//输出验证码图片
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
