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 相关文章推荐
PHP的FTP学习(二)[转自奥索]
Oct 09 PHP
mysql 查询指定日期时间内sql语句实现原理与代码
Dec 16 PHP
浏览器预览PHP文件时顶部出现空白影响布局分析原因及解决办法
Jan 11 PHP
测试php连接mysql是否成功的代码分享
Jan 24 PHP
php+mysql查询优化简单实例
Jan 13 PHP
PHP7之Mongodb API使用详解
Dec 26 PHP
Laravel执行migrate命令提示:No such file or directory的解决方法
Mar 16 PHP
PHP版微信小店接口开发实例
Nov 12 PHP
如何优雅的使用 laravel 的 validator验证方法
Nov 11 PHP
PHP反射学习入门示例
Jun 14 PHP
Laravel6.2中用于用户登录的新密码确认流程详解
Oct 16 PHP
laravel中的fillable和guarded属性详解
Oct 23 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
一个PHP的String类代码
2010/04/20 PHP
php 截取字符串并以零补齐str_pad() 函数
2011/05/07 PHP
php代码中使用换行及(\n或\r\n和br)的应用
2013/02/02 PHP
php中fgetcsv()函数用法实例
2014/11/28 PHP
一键生成各种尺寸Icon的php脚本(实例)
2017/02/08 PHP
PHP实现财务审核通过后返现金额到客户的功能
2019/07/04 PHP
php屏蔽错误及提示的方法
2020/05/10 PHP
快速排序 php与javascript的不同之处
2011/02/22 Javascript
Javascript访问器属性实例分析
2014/12/30 Javascript
jquery 属性选择器(匹配具有指定属性的元素)
2016/09/06 Javascript
javascript滚轮控制模拟滚动条
2016/10/19 Javascript
js获取时间函数及扩展函数的方法
2016/10/30 Javascript
JS 组件系列之Bootstrap Table 冻结列功能IE浏览器兼容性问题解决方案
2017/06/30 Javascript
10分钟上手vue-cli 3.0 入门介绍
2018/04/04 Javascript
在vue中获取微信支付code及code被占用问题的解决方法
2019/04/16 Javascript
layui 实现表单和文件上传一起传到后台的例子
2019/09/16 Javascript
js实现单元格拖拽效果
2020/02/10 Javascript
Python构造函数及解构函数介绍
2015/02/26 Python
Python实现比较两个文件夹中代码变化的方法
2015/07/10 Python
在Django的session中使用User对象的方法
2015/07/23 Python
Python中xrange与yield的用法实例分析
2017/12/26 Python
django 发送手机验证码的示例代码
2018/04/25 Python
Python实现多级目录压缩与解压文件的方法
2018/09/01 Python
Python 通过微信控制实现app定位发送到个人服务器再转发微信服务器接收位置信息
2019/08/05 Python
django框架基于queryset和双下划线的跨表查询操作详解
2019/12/11 Python
Python开发之身份证验证库id_validator验证身份证号合法性及根据身份证号返回住址年龄等信息
2020/03/20 Python
django实现更改数据库某个字段以及字段段内数据
2020/03/31 Python
jupyter notebook 的工作空间设置操作
2020/04/20 Python
Petmate品牌官方网站:宠物用品
2018/11/25 全球购物
印尼在线旅游门户网站:NusaTrip
2019/11/01 全球购物
澳大利亚美容产品及化妆品在线:Activeskin
2020/06/03 全球购物
查环查孕证明
2014/01/10 职场文书
个人收入证明模板
2014/09/18 职场文书
2015年店长工作总结范文
2015/04/08 职场文书
2019假期福利管理制度!
2019/07/15 职场文书
解决Golang time.Parse和time.Format的时区问题
2021/04/29 Golang