<?phpheader("Content-Type:text/html;Charset=UTF-8");// 设置页面的编码风格header("Content-Type:image/jpeg");// 通知浏览器输出的是jpeg格式的图像$img = imagecreatetruecolor(150,50);//创建画布并设置大小 x轴150 y轴50$bgcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));//分配背景颜色imagefill($img, 0, 0, $bgcolor); ////把背景填充到图像imagejpeg($img); // 输出图像imagedestroy($img); // 销毁图像?>好,现在结合以上代码,来分析分析以上用到的几个函数:
resource imagecreatetruecolor ( int $width , int $height )imagecreatetruecolor() 和 imagecreate()两个函数都能创建画布
resource imagecreate ( int $x_size , int $y_size )imagecreatetruecolor()建立的是一幅大小为 x和 y的黑色图像(默认为黑色[即便叫法就是真彩色图像]),如想改变背景颜色则需要用填充颜色函数 imagefill($img,0,0,$color);
int imagecolorallocate ( resource $image , int $red , int $green , int $blue )颜色分别用 红 绿 蓝三色组合,这些参数是 0 到 255 的整数或者十六进制的 0x00 到 0xFF。
int mt_rand ( int $min , int $max )$min 可选的、返回的最小值(默认:0) $max 可选的、返回的最大值(默认:mt_getrandmax())

2.0 开始往里面做干扰线,干扰点。防止验证图像被秒识别
<?phpheader("Content-Type:text/html;Charset=UTF-8");// 设置页面的编码风格header("Content-Type:image/jpeg");// 通知浏览器输出的是jpeg格式的图像$img = imagecreatetruecolor(150,50);//创建画布并设置大小 x轴150 y轴50$bgcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));//分配背景颜色//添加干扰线,并循环3次,背景颜色随机for($i=0;$i<3;$i++){$linecolor = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));imageline($img, mt_rand(0,150), mt_rand(0,50), mt_rand(0,150), mt_rand(0,50), $linecolor);}//添加干扰点,并循环25次,背景颜色随机for($i=0;$i<25;$i++){$dotcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));imagesetpixel($img, mt_rand(0,150), mt_rand(0,60), $dotcolor);}imagefill($img, 0, 0, $bgcolor); ////把背景填充到图像imagejpeg($img); // 输出图像imagedestroy($img); // 销毁图像?>函数分析:
3.0 添加验证字母数字
<?phpheader("Content-Type:text/html;Charset=UTF-8");// 设置页面的编码风格header("Content-Type:image/jpeg");// 通知浏览器输出的是jpeg格式的图像$img = imagecreatetruecolor(150,50);//创建画布并设置大小 x轴150 y轴50$bgcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));//分配背景颜色//添加干扰线,并循环3次,背景颜色随机for($i=0;$i<3;$i++){$linecolor = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));imageline($img, mt_rand(0,150), mt_rand(0,50), mt_rand(0,150), mt_rand(0,50), $linecolor);}//添加干扰点,并循环25次,背景颜色随机for($i=0;$i<25;$i++){$dotcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));imagesetpixel($img, mt_rand(0,150), mt_rand(0,60), $dotcolor);}//添加需要验证的字母或者数字$rand_str = "qwertyuiopasdfghjklzxcvbnm1234567890";//需要使用到验证的一些字母和数字$str_arr = array();//命名一个数组for($i = 0;$i<4;$i++){//循环4次,就是有四个随机的字母或者数字$pos = mt_rand(0,strlen($rand_str)-1);$str_arr[] = $rand_str[$pos];//临时交换}$x_start=150/4;//单个字符X轴位置foreach ($str_arr as $key) {$fontcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));imagettftext($img, 25, mt_rand(-15,15), $x_start, 50/2, $fontcolor, "C:/Windows/Fonts/Verdana.TTF", $key);$x_start +=20;//遍历后单个字符沿X轴 +20}imagefill($img, 0, 0, $bgcolor); ////把背景填充到图像imagejpeg($img); // 输出图像imagedestroy($img); // 销毁图像?>函数:array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )分析下面的代码:

看起来还是挺可爱的。
以上这篇PHP生成制作验证码的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。