最新最全PHP生成制作验证码代码详解(推荐)


Posted in PHP onJune 12, 2016

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 )

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随便取值。所以页面没刷新一次画布背景颜色就不一样。效果图:

最新最全PHP生成制作验证码代码详解(推荐)

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);具体含义同上

效果图:

最新最全PHP生成制作验证码代码详解(推荐)

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生成制作验证码代码详解(推荐)

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

PHP 相关文章推荐
Smarty结合Ajax实现无刷新留言本实例
Jan 02 PHP
用PHPdig打造属于你自己的Google[图文教程]
Feb 14 PHP
php 结果集的分页实现代码
Mar 10 PHP
PHP FOR MYSQL 代码生成助手(根据Mysql里的字段自动生成类文件的)
Jul 23 PHP
简单的PHP缓存设计实现代码
Sep 30 PHP
深入php内核之php in array
Nov 10 PHP
php数组冒泡排序算法实例
May 06 PHP
PHP判断文件是否被引入的方法get_included_files用法示例
Nov 29 PHP
php实现的统计字数函数定义与使用示例
Jul 26 PHP
Laravel中为什么不使用blpop取队列详析
Aug 01 PHP
PHP实现微信申请退款功能
Oct 01 PHP
使用laravel根据用户类型来显示或隐藏字段
Oct 17 PHP
再谈PHP中单双引号的区别详解
Jun 12 #PHP
PHP中strpos、strstr和stripos、stristr函数分析
Jun 11 #PHP
linux下php上传文件注意事项
Jun 11 #PHP
php设计模式之单例模式代码
Jun 11 #PHP
浅谈PHP Cookie处理函数
Jun 10 #PHP
php单例模式的简单实现方法
Jun 10 #PHP
PHP操作mysql数据库分表的方法
Jun 09 #PHP
You might like
php连接mysql数据库代码
2009/03/10 PHP
Trying to clone an uncloneable object of class Imagic的解决方法
2012/01/11 PHP
php 运算符与表达式详细介绍
2016/11/30 PHP
Thinkphp 空操作、空控制器、命名空间(详解)
2017/05/05 PHP
详解php语言最牛掰的Laravel框架
2017/11/20 PHP
在laravel中实现将查询的对象转换为多维数组的函数
2019/10/21 PHP
TP5框架页面跳转样式操作示例
2020/04/05 PHP
jquery select操作的日期联动实现代码
2009/12/06 Javascript
JQuery的Alert消息框插件使用介绍
2010/10/09 Javascript
JQuery动态给table添加、删除行 改进版
2011/01/19 Javascript
Jquery post传递数组方法实现思路及代码
2013/04/28 Javascript
jquery实现图片按比例缩放示例
2014/07/01 Javascript
使用jquery动态加载js文件的方法
2014/12/24 Javascript
jquery实现漂亮的二级下拉菜单代码
2015/08/26 Javascript
jQuery插件制作的实例教程
2016/05/16 Javascript
Javascript动画效果(3)
2016/10/11 Javascript
jQuery Ajax自定义分页组件(jquery.loehpagerv1.0)实例详解
2017/05/01 jQuery
React-router中结合webpack实现按需加载实例
2017/05/25 Javascript
实现div内部滚动条滚动到底部和顶部的代码
2017/11/15 Javascript
jQuery zTree 异步加载添加子节点重复问题
2017/11/29 jQuery
解决vue中无法动态修改jqgrid组件 url地址的问题
2018/03/01 Javascript
Vue.js添加组件操作示例
2018/06/13 Javascript
微信小程序实现判断是分享到群还是个人功能示例
2019/05/03 Javascript
实例分析JS中的相等性判断===、 ==和Object.is()
2019/11/17 Javascript
Vue双向绑定实现原理与方法详解
2020/05/07 Javascript
swiper实现导航滚动效果
2020/12/13 Javascript
JS实现纸牌发牌动画
2021/01/19 Javascript
[03:01]完美世界DOTA2联赛PWL S2 集锦第二期
2020/12/03 DOTA
python3简单实现微信爬虫
2015/04/09 Python
Python 3.6 性能测试框架Locust安装及使用方法(详解)
2017/10/11 Python
Python Celery多队列配置代码实例
2019/11/22 Python
Python-openCV读RGB通道图实例
2020/01/17 Python
三星新西兰官网:Samsung新西兰
2019/03/05 全球购物
BannerBuzz加拿大:在线定制横幅印刷、广告和标志
2020/03/10 全球购物
Java基础知识面试题
2014/03/25 面试题
安装harbor作为docker镜像仓库的问题
2022/06/14 Servers