PHP验证码生成核心函数解析与实现
1.0 首先先看代码
<?php
header("Content-Type:text/html;Charset=UTF-");// 设置页面的编码风格
header("Content-Type:image/jpeg");// 通知浏览器输出的是jpeg格式的图像
$img = imagecreatetruecolor(,);//创建画布并设置大小 x轴 y轴
$bgcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,));//分配背景颜色
imagefill($img, , , $bgcolor); ////把背景填充到图像
imagejpeg($img); // 输出图像
imagedestroy($img); // 销毁图像
?>
接下来,我们将结合上述代码,深入剖析其中涉及的关键函数及其作用机制。
① imagecreatetruecolor();
imagecreatetruecolor — 新建一个真彩色图像(名称虽长,但拆解为 image/create/true/color 后易于记忆。真彩色图像具体指什么?请继续阅读下文)。
resource imagecreatetruecolor ( int $width , int $height )
在PHP中,imagecreatetruecolor() 和 imagecreate() 均可用于创建图像画布。
resource imagecreate ( int $x_size , int $y_size )
imagecreatetruecolor() 创建的是一个尺寸为 x 和 y 的黑色图像(默认为黑色背景,尽管其名称暗示真彩色)。若需更改背景色,则必须执行后续操作。
需调用填充颜色函数 imagefill($img,0,0,$color) 来设置背景。
相比之下,imagecreate 仅新建空白图像资源,需配合 imagecolorAllocate() 来指定背景色。
综上所述,这两个函数仅是实现同一功能的两种不同途径。
② imagecolorallocate();
imagecolorallocate — 为一幅图像分配颜色。
int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
颜色由红、绿、蓝三色组合而成,参数取值范围为 0 到 255 的整数,或十六进制的 0x00 到 0xFF。
③ mt_rand();
mt_rand — 生成更高质量的随机数。
int mt_rand ( int $min , int $max )
$min 为可选的最小返回值(默认值:0),$max 为可选的最大返回值(默认值:mt_getrandmax())。此处用于随机生成背景颜色,取值范围 0-255。因此,每次页面刷新时,画布背景颜色都会发生变化。效果图如下:

2.0 开始往里面做干扰线,干扰点。防止验证图像被秒识别
<?php
header("Content-Type:text/html;Charset=UTF-");// 设置页面的编码风格
header("Content-Type:image/jpeg");// 通知浏览器输出的是jpeg格式的图像
$img = imagecreatetruecolor(,);//创建画布并设置大小 x轴 y轴
$bgcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,));//分配背景颜色
//添加干扰线,并循环次,背景颜色随机
for($i=;$i<;$i++){
$linecolor = imagecolorallocate($img,mt_rand(,),mt_rand(,),mt_rand(,));
imageline($img, mt_rand(,), mt_rand(,), mt_rand(,), mt_rand(,), $linecolor);
}
//添加干扰点,并循环次,背景颜色随机
for($i=;$i<;$i++){
$dotcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,));
imagesetpixel($img, mt_rand(,), mt_rand(,), $dotcolor);
}
imagefill($img, , , $bgcolor); ////把背景填充到图像
imagejpeg($img); // 输出图像
imagedestroy($img); // 销毁图像
?>
相关函数分析如下:
① imageline();
imageline — 画一条线段。
bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
imageline() 使用指定的 color 颜色,在图像 image 中从坐标 (x1, y1) 到 (x2, y2)(图像左上角为原点 0, 0)绘制一条线段。
示例代码 imageline($img, mt_rand(0,150), mt_rand(0,50), mt_rand(0,150), mt_rand(0,50), $linecolor) 的含义是:在画布 $img 中,从随机起点 (x1, y1) 到随机终点 (x2, y2) 绘制线条。
② imagesetpixel();
imagesetpixel— 画一个单一像素。
bool imagesetpixel ( resource $image , int $x , int $y , int $color )
imagesetpixel() 在图像 image 中,使用 color 颜色在坐标 (x, y)(图像左上角为原点 0,0)上绘制一个点。
imagesetpixel($img, mt_rand(0,150), mt_rand(0,60), $dotcolor);具体含义同上
效果图如下:

3.0 添加验证字母数字
<?php
header("Content-Type:text/html;Charset=UTF-");// 设置页面的编码风格
header("Content-Type:image/jpeg");// 通知浏览器输出的是jpeg格式的图像
$img = imagecreatetruecolor(,);//创建画布并设置大小 x轴 y轴
$bgcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,));//分配背景颜色
//添加干扰线,并循环次,背景颜色随机
for($i=;$i<;$i++){
$linecolor = imagecolorallocate($img,mt_rand(,),mt_rand(,),mt_rand(,));
imageline($img, mt_rand(,), mt_rand(,), mt_rand(,), mt_rand(,), $linecolor);
}
//添加干扰点,并循环次,背景颜色随机
for($i=;$i<;$i++){
$dotcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,));
imagesetpixel($img, mt_rand(,), mt_rand(,), $dotcolor);
}
//添加需要验证的字母或者数字
$rand_str = "qwertyuiopasdfghjklzxcvbnm";//需要使用到验证的一些字母和数字
$str_arr = array(); //命名一个数组
for($i = ;$i<;$i++){ //循环次,就是有四个随机的字母或者数字
$pos = mt_rand(,strlen($rand_str)-);
$str_arr[] = $rand_str[$pos];//临时交换
}
$x_start=/;//单个字符X轴位置
foreach ($str_arr as $key) {
$fontcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,));
imagettftext($img, , mt_rand(-,), $x_start, /, $fontcolor, "C:/Windows/Fonts/Verdana.TTF", $key);
$x_start +=;//遍历后单个字符沿X轴 +
}
imagefill($img, , , $bgcolor); ////把背景填充到图像
imagejpeg($img); // 输出图像
imagedestroy($img); // 销毁图像
?>
主要使用的函数为:
imagettftext();
imagettftext — 用 TrueType 字体向图像写入文本。
array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
对以下代码参数进行分析:
imagettftext($img, 25, mt_rand(-15,15), $x_start, 50/2, $fontcolor, "C:/Windows/Fonts/Verdana.TTF", $key);
$img-----------画布对象
25-----------字体的尺寸大小。
mt_rand(-15,15)----------角度制表示的角度,0 度为从左向右读的文本。更高数值表示逆时针旋转。例如 90 度表示从下向上读的文本。(即控制字体旋转角度)。
$x_start----------通俗来讲,即字符在X轴上的起始位置。
50/2----------字符高度的中心偏移量计算。
$fontcolor----------字符的颜色。
"C:/Windows/Fonts/Verdana.TTF"----------字符所使用的字体样式文件路径。
$key-----------遍历生成的待显示字符。
最终效果如下:

以上内容是本文给大家介绍的最新最全PHP生成制作验证码代码详解(推荐)的全部叙述,希望对大家有所帮助!