700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > PHP生成制作验证码 php生成验证码

PHP生成制作验证码 php生成验证码

时间:2019-12-05 03:19:11

相关推荐

PHP生成制作验证码 php生成验证码

php教程|php手册

php

php教程-php手册

PHP生成制作验证码,php生成验证码

秒微信余额二维码源码,ubuntu更改内核启动,json爬虫的原理,notpad php,seo失败教训lzw

看完就会,不会你打我,话不多说、开搞(人狠话不多)

idc销售网站php源码,ubuntu如何连接终端,tomcat应用程序乱码,爬虫代理https,如何把php改成txt,影楼seolzw

1.0首先先看代码

刷人气源码,刷ubuntu到emmc,莱茨狗爬虫,php cosket,seo领域包括lzw

1

好,现在结合以上代码,来分析分析以上用到的几个函数:

①imagecreatetruecolor();

imagecreatetruecolor — 新建一个真彩色图像(感觉哇,那么长,其实仔细一看挺好记的 image/create/true/color,什么是真彩色图像?往下看)

1 resource imagecreatetruecolor ( int $width , int $height )

imagecreatetruecolor() 和 imagecreate()两个函数都能创建画布

1 resource imagecreate ( int $x_size , int $y_size )

imagecreatetruecolor()建立的是一幅大小为 x和 y的黑色图像(默认为黑色[即便叫法就是真彩色图像]),如想改变背景颜色则需要用填充颜色函数 imagefill($img,0,0,$color);

imagecreate 新建一个空白图像资源,用imagecolorAllocate()添加背景色

上面两个函数只不过是一个功能的两种方法

②imagecolorallocate();

imagecolorallocate — 为一幅图像分配颜色

1 int imagecolorallocate ( resource $image , int $red , int $green , int $blue )

颜色分别用 红 绿 蓝三色组合,这些参数是 0 到 255 的整数或者十六进制的 0x00 到 0xFF。

③mt_rand();

mt_rand — 生成更好的随机数

1 int mt_rand ( int $min , int $max )

$min可选的、返回的最小值(默认:0)$max可选的、返回的最大值(默认:mt_getrandmax())

这里就是用来让他随机生成背景颜色,0-255随便取值。所以页面没刷新一次画布背景颜色就不一样。效果图:

2.0开始往里面做干扰线,干扰点。防止验证图像被秒识别

1 <?php 2 header("Content-Type:text/html;Charset=UTF-8");// 设置页面的编码风格 3 header("Content-Type:image/jpeg");// 通知浏览器输出的是jpeg格式的图像 4 5 $img = imagecreatetruecolor(150,50);//创建画布并设置大小 x轴150 y轴50 6 7 $bgcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));//分配背景颜色 8 9 //添加干扰线,并循环3次,背景颜色随机10 for($i=0;$i<3;$i++){11 12$linecolor = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));13imageline($img, mt_rand(0,150), mt_rand(0,50), mt_rand(0,150), mt_rand(0,50), $linecolor);14 15 }16 //添加干扰点,并循环25次,背景颜色随机17 for($i=0;$i

函数分析:

①imageline();

imageline — 画一条线段

1 bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )

imageline() 用color颜色在图像image中从坐标x1y1x2y2(图像左上角为 0, 0)画一条线段。

imageline($img, mt_rand(0,150), mt_rand(0,50), mt_rand(0,150), mt_rand(0,50), $linecolor);

这里意思就是 画布$img 中从坐标x1y1x2y2随机

②imagesetpixel();

imagesetpixel— 画一个单一像素

1 bool imagesetpixel ( resource $image , int $x , int $y , int $color )

imagesetpixel() 在image图像中用color颜色在xy坐标(图像左上角为 0,0)上画一个点。

imagesetpixel($img, mt_rand(0,150), mt_rand(0,60), $dotcolor);

具体含义同上

效果图:

3.0添加验证字母数字

1 <?php 2 header("Content-Type:text/html;Charset=UTF-8");// 设置页面的编码风格 3 header("Content-Type:image/jpeg");// 通知浏览器输出的是jpeg格式的图像 4 5 $img = imagecreatetruecolor(150,50);//创建画布并设置大小 x轴150 y轴50 6 7 $bgcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));//分配背景颜色 8 9 //添加干扰线,并循环3次,背景颜色随机10 for($i=0;$i<3;$i++){11 12$linecolor = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));13imageline($img, mt_rand(0,150), mt_rand(0,50), mt_rand(0,150), mt_rand(0,50), $linecolor);14 15 }16 //添加干扰点,并循环25次,背景颜色随机17 for($i=0;$i<25;$i++){18 19$dotcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));20imagesetpixel($img, mt_rand(0,150), mt_rand(0,60), $dotcolor);21 22 }23 24 //添加需要验证的字母或者数字25 $rand_str = "qwertyuiopasdfghjklzxcvbnm1234567890";//需要使用到验证的一些字母和数字26 $str_arr = array(); //命名一个数组27 for($i = 0;$i

函数:

imagettftext();

imagettftext — 用 TrueType 字体向图像写入文本

1 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———–遍历出后的字符

效果:

看起来还是挺可爱的。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。