最新最全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 相关文章推荐
php 缩略图实现函数代码
Jun 23 PHP
fetchAll()与mysql_fetch_array()的区别详解
Jun 05 PHP
php float不四舍五入截取浮点型字符串方法总结
Oct 28 PHP
php检测用户是否用手机(Mobile)访问网站的类
Jan 09 PHP
PHP curl 抓取AJAX异步内容示例
Sep 09 PHP
PHP的运行机制与原理(底层)
Nov 16 PHP
浅析Yii2 GridView实现下拉搜索教程
Apr 22 PHP
php使用number_format函数截取小数的方法分析
May 27 PHP
php is_executable判断给定文件名是否可执行实例
Sep 26 PHP
php自定义排序uasort函数示例【二维数组按指定键值排序】
Jun 19 PHP
关于laravel后台模板laravel-admin select框的使用详解
Oct 03 PHP
PHP实用小技巧之调用录像的方法
Dec 05 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
Zerg剧情介绍
2020/03/14 星际争霸
PHP中冒号、endif、endwhile、endfor使用介绍
2010/04/28 PHP
用Simple Excel导出xls实现方法
2012/12/06 PHP
php include和require的区别深入解析
2013/06/17 PHP
PHP中的按位与和按位或操作示例
2014/01/27 PHP
浅析application/x-www-form-urlencoded和multipart/form-data的区别
2014/06/22 PHP
Linux下PHP安装mcrypt扩展模块笔记
2014/09/10 PHP
ThinkPHP中html:list标签用法分析
2016/01/09 PHP
使用TextRange获取输入框中光标的位置的代码
2007/03/08 Javascript
JS简单的图片放大缩小的两种方法
2013/11/11 Javascript
require.js深入了解 require.js特性介绍
2014/09/04 Javascript
CSS+JS实现点击文字弹出定时自动关闭DIV层菜单的方法
2015/05/12 Javascript
jQuery实现鼠标滑过预览图片大图效果的方法
2017/04/26 jQuery
详解基于Bootstrap+angular的一个豆瓣电影app
2017/06/26 Javascript
angular使用bootstrap方法手动启动的实例代码
2017/07/18 Javascript
jQuery实现的简单动态添加、删除表格功能示例
2017/09/21 jQuery
Vue.js组件通信的几种姿势
2017/10/23 Javascript
vue中动态绑定表单元素的属性方法
2018/02/23 Javascript
微信小程序使用template标签实现五星评分功能
2018/11/03 Javascript
JavaScript之实现一个简单的Vue示例
2019/01/17 Javascript
JavaScript如何实现元素全排列实例代码
2019/05/14 Javascript
layui table数据修改的回显方法
2019/09/04 Javascript
教你如何将 Sublime 3 打造成 Python/Django IDE开发利器
2014/07/04 Python
举例讲解Python设计模式编程中的访问者与观察者模式
2016/01/26 Python
pytorch对可变长度序列的处理方法详解
2018/12/08 Python
Python人工智能之路 jieba gensim 最好别分家之最简单的相似度实现
2019/08/13 Python
Python any()函数的使用方法
2019/10/28 Python
pycharm显示远程图片的实现
2019/11/04 Python
python装饰器的特性原理详解
2019/12/25 Python
Python网络爬虫四大选择器用法原理总结
2020/06/01 Python
全球知名提供各类营养保健品的零售商:Vitamin Shoppe
2016/10/09 全球购物
外贸主管求职简历的自我评价
2013/10/23 职场文书
《蜗牛》教学反思
2014/02/18 职场文书
2014最新预备党员思想汇报范文:中国梦,我的梦
2014/10/25 职场文书
刑事申诉状范文
2015/05/20 职场文书
初中生物教学随笔
2015/08/15 职场文书