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中与数组相关的函数
Mar 22 PHP
php 各种应用乱码问题的解决方法
May 09 PHP
在VS2008中编译MYSQL5.1.48的方法
Jul 03 PHP
php编程实现获取excel文档内容的代码实例
Jun 28 PHP
php中解析带中文字符的url函数分享
Jan 20 PHP
PHP开发注意事项总结
Feb 04 PHP
PHP使用mysqldump命令导出数据库
Apr 14 PHP
PHP的Yii框架中View视图的使用进阶
Mar 29 PHP
php 计算两个时间相差的天数、小时数、分钟数、秒数详解及实例代码
Nov 09 PHP
在laravel中实现ORM模型使用第二个数据库设置
Oct 24 PHP
TP5框架model常见操作示例小结【增删改查、聚合、时间戳、软删除等】
Apr 05 PHP
PHP实现基本留言板功能原理与步骤详解
Mar 26 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
javascript 模式设计之工厂模式学习心得
2010/04/27 Javascript
在JS中最常看到切最容易迷惑的语法(转)
2010/10/29 Javascript
JS求平均值的小例子
2013/11/29 Javascript
firefox下jquery ajax返回object XMLDocument处理方法
2014/01/26 Javascript
jquery实现炫酷的叠加层自动切换特效
2015/02/01 Javascript
深入学习jQuery Validate表单验证
2016/01/18 Javascript
JS 对象(Object)和字符串(String)互转方法
2016/05/20 Javascript
工作中比较实用的JavaScript验证和数据处理的干货(经典)
2016/08/03 Javascript
js控住DOM实现发布微博效果
2016/08/30 Javascript
解决Extjs下拉框不显示的问题
2017/06/21 Javascript
浅谈webpack-dev-server的配置和使用
2018/05/17 Javascript
150行Node.js实现的dns代理工具
2019/08/02 Javascript
jQuery实现每日秒杀商品倒计时功能
2019/09/06 jQuery
js将URL网址转为16进制加密与解密函数
2020/03/04 Javascript
基于vue3.0.1beta搭建仿京东的电商H5项目
2020/05/06 Javascript
[59:08]DOTA2上海特级锦标赛C组小组赛#2 LGD VS Newbee第一局
2016/02/27 DOTA
Python中类的定义、继承及使用对象实例详解
2015/04/30 Python
通过数据库对Django进行删除字段和删除模型的操作
2015/07/21 Python
实现python版本的按任意键继续/退出
2016/09/26 Python
Python学习思维导图(必看篇)
2017/06/26 Python
pandas.loc 选取指定列进行操作的实例
2018/05/18 Python
Python3随机漫步生成数据并绘制
2018/08/27 Python
利用Python如何实现一个小说网站雏形
2018/11/23 Python
关于 Python opencv 使用中的 ValueError: too many values to unpack
2019/06/28 Python
Python enumerate函数遍历数据对象组合过程解析
2019/12/11 Python
Python使用QQ邮箱发送邮件实例与QQ邮箱设置详解
2020/02/18 Python
windows支持哪个版本的python
2020/07/03 Python
JSF面试题:Jsf中的核心类用那些?有什么作用?LiftCycle六大生命周期是什么?
2014/07/17 面试题
电大毕业自我鉴定
2014/02/03 职场文书
国际贸易专业自荐信
2014/06/10 职场文书
初中毕业生自我评价
2015/03/02 职场文书
防汛通知
2015/04/25 职场文书
60条职场经典语录,总有一条能触动你的心
2019/08/21 职场文书
如何制作自己的原生JavaScript路由
2021/05/05 Javascript
铁拳制作人赞《铁拳7》老头环Mod:制作精良 但别弄了
2022/04/03 其他游戏
Python内置的数据类型及使用方法
2022/04/13 Python