PHP生成制作验证码的简单实例


Posted in PHP onJune 12, 2016

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

1.0 首先先看代码

<?php
header("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);     // 销毁图像
?>

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

① 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-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);     // 销毁图像
?>

函数分析:

① 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-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);     // 销毁图像
?>

函数:

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 相关文章推荐
支持oicq头像的留言簿(一)
Oct 09 PHP
轻松修复Discuz!数据库
May 03 PHP
PHP Document 代码注释规范
Apr 13 PHP
PHP 全角转半角实现代码
May 16 PHP
mysql总结之explain
Feb 27 PHP
PHP判断图片格式的七种方法小结
Jun 03 PHP
解析php file_exists无效的解决办法
Jun 26 PHP
php中将一段数据存到一个txt文件中并显示其内容
Aug 15 PHP
php_imagick实现图片剪切、旋转、锐化、减色或增加特效的方法
Dec 15 PHP
php使用simplexml_load_file加载XML文件并显示XML的方法
Mar 19 PHP
Yii rules常用规则示例
Mar 15 PHP
你真的了解PHP中的引用符号(&)吗
May 12 PHP
php gd等比例缩放压缩图片函数
Jun 12 #PHP
详解PHP中cookie和session的区别及cookie和session用法小结
Jun 12 #PHP
yii2中结合gridview如何使用modal弹窗实例代码详解
Jun 12 #PHP
最新最全PHP生成制作验证码代码详解(推荐)
Jun 12 #PHP
再谈PHP中单双引号的区别详解
Jun 12 #PHP
PHP中strpos、strstr和stripos、stristr函数分析
Jun 11 #PHP
linux下php上传文件注意事项
Jun 11 #PHP
You might like
一个简单实现多条件查询的例子
2006/10/09 PHP
PHP实现多进程并行操作的详解(可做守护进程)
2013/06/18 PHP
php获取目标函数执行时间示例
2014/03/04 PHP
php实现生成code128条形码的方法详解
2017/07/19 PHP
PHP实现简单登录界面
2019/10/23 PHP
PHP读取Excel内的图片(phpspreadsheet和PHPExcel扩展库)
2019/11/19 PHP
Iframe 自适应高度并实时监控高度变化的js代码
2009/10/30 Javascript
20个非常棒的 jQuery 幻灯片插件和教程分享
2011/08/23 Javascript
详细分析使用AngularJS编程中提交表单的方式
2015/06/19 Javascript
jquery实现图片上传之前预览的方法
2015/07/11 Javascript
javascript下拉列表菜单的实现方法
2015/11/18 Javascript
CKEditor无法验证的解决方案(js验证+jQuery Validate验证)
2016/05/09 Javascript
json的使用小结
2016/06/08 Javascript
angular.js之路由的选择方法
2016/09/24 Javascript
jQuery中animate()的使用方法及解决$(”body“).animate({“scrollTop”:top})不被Firefox支持的问题
2017/04/04 jQuery
利用JavaScript在网页实现八数码启发式A*算法动画效果
2017/04/16 Javascript
浅谈pc端rem字体设置的问题
2017/08/03 Javascript
vue 里面使用axios 和封装的示例代码
2017/09/01 Javascript
ReactNative 之FlatList使用及踩坑封装总结
2017/11/29 Javascript
nodejs超出最大的调用栈错误问题
2017/12/27 NodeJs
Vue CLI3 如何支持less的方法示例
2018/08/29 Javascript
JS实现网页端猜数字小游戏
2020/03/06 Javascript
JS代码简洁方式之函数方法详解
2020/07/28 Javascript
使用python获取CPU和内存信息的思路与实现(linux系统)
2014/01/03 Python
Python生成密码库功能示例
2017/05/23 Python
用python标准库difflib比较两份文件的异同详解
2018/11/16 Python
Python 串口读写的实现方法
2019/06/12 Python
python中数据库like模糊查询方式
2020/03/02 Python
Python读取VOC中的xml目标框实例
2020/03/10 Python
快速解决pymongo操作mongodb的时区问题
2020/12/05 Python
Boda Skins皮衣官网:奢侈皮夹克,全球配送
2016/12/15 全球购物
美国在线打印网站:Overnight Prints
2018/10/11 全球购物
毕业生教师求职信
2013/10/20 职场文书
企业办公室主任岗位职责
2014/02/19 职场文书
关于长城的导游词
2015/01/30 职场文书
追讨欠款律师函
2015/05/27 职场文书