PHP生成图像验证码的方法小结(2种方法)


Posted in PHP onJuly 18, 2016

本文实例讲述了PHP生成图像验证码的方法。分享给大家供大家参考,具体如下:

1、生成加法运算验证码图片

session_start ();
/*定义头文件为图片*/
header("Content-type: image/png");
/*生成验证码*/
/*创建图片设置字体颜色*/
$im = imagecreate($w, $h);
$red = imagecolorallocate($im, 255, 255, 255);
$white = imagecolorallocate($im, 255, 255, 255);
/*随机生成两个数字*/
$num1 = rand(1, 20);
$num2 = rand(1, 20);
$_SESSION ["administratorConfirmCode"] = $num1+$num2;
/*设置图片背景颜色*/
$gray = imagecolorallocate($im, 118, 151, 199);
$black = imagecolorallocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
/*创建图片背景*/
imagefilledrectangle($im, 0, 0, 100, 24, $black);
/*在画布上随机生成大量点*/
for ($i = 0; $i < 80; $i++) {
  imagesetpixel($im, rand(0, $w), rand(0, $h), $gray);
}
/*将计算验证码写入到图片中*/
imagestring($im, 5, 5, 4, $num1, $red);
imagestring($im, 5, 30, 3, "+", $red);
imagestring($im, 5, 45, 4, $num2, $red);
imagestring($im, 5, 70, 3, "=", $red);
imagestring($im, 5, 80, 2, "?", $white);
/*输出图片*/
imagepng($im);
imagedestroy($im);

2、生成字符验证码图片【值得注意的是在字体哪里,需要引入实际的字体路径,否则,可能出现图像显示不了验证码】

session_start ();
/*设置文件头为图片输出*/
Header("Content-type: image/JPEG");
/*调用生成验证码函数*/
$str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234565789";
$result="";
for($i=0;$i<$length;$i++){
  $num[$i]=rand(0,61);
  $result.=$str[$num[$i]];
}
$text = $result;
$_SESSION ["administratorConfirmCode"] = $text;
/*设置图片的宽度和高度*/
$im_x = $w;
$im_y = $y;
/*创建图片*/
$im = imagecreatetruecolor($im_x,$im_y);
$text_c = ImageColorAllocate($im, mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
$tmpC0=mt_rand(100,255);
$tmpC1=mt_rand(100,255);
$tmpC2=mt_rand(100,255);
$buttum_c = ImageColorAllocate($im,$tmpC0,$tmpC1,$tmpC2);
imagefill($im, 16, 13, $buttum_c);
/*字体文件*/
$font = _WEB_DIR_.'/font/comic.ttf';
for ($i=0;$i<strlen($text);$i++){
  $tmp =substr($text,$i,1);
  $array = array(-1,1);
  $p = array_rand($array);
  $an = $array[$p]*mt_rand(1,10);//角度
  $size = 28;
  imagettftext($im, $size, $an, 15+$i*$size, 35, $text_c, $font, $tmp);
}
/*将字符写入文件中*/
$distortion_im = imagecreatetruecolor ($im_x, $im_y);
imagefill($distortion_im, 16, 13, $buttum_c);
for ( $i=0; $i<$im_x; $i++) {
  for ( $j=0; $j<$im_y; $j++) {
    $rgb = imagecolorat($im, $i , $j);
    if( (int)($i+20+sin($j/$im_y*2*M_PI)*10) <= imagesx($distortion_im)&& (int)($i+20+sin($j/$im_y*2*M_PI)*10) >=0 ) {
      imagesetpixel ($distortion_im, (int)($i+10+sin($j/$im_y*2*M_PI-M_PI*0.1)*4) , $j , $rgb);
    }
  }
}
/*干扰元素点的数量*/
$count = 160;
/*创建干扰元素点*/
for($i=0; $i<$count; $i++){
  $randcolor = ImageColorallocate($distortion_im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  imagesetpixel($distortion_im, mt_rand()%$im_x , mt_rand()%$im_y , $randcolor);
}
/*创建干扰线条*/
$rand = mt_rand(5,30);
$rand1 = mt_rand(15,25);
$rand2 = mt_rand(5,10);
for ($yy=$rand; $yy<=+$rand+2; $yy++){
  for ($px=-80;$px<=80;$px=$px+0.1){
    $x=$px/$rand1;
    if ($x!=0){
      $y=sin($x);
    }
    $py=$y*$rand2;
    imagesetpixel($distortion_im, $px+80, $py+$yy, $text_c);
  }
}
/*以PNG格式将图像输出到浏览器*/
ImagePNG($distortion_im);
/*销毁图像*/
ImageDestroy($distortion_im);
ImageDestroy($im);

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
一个连接两个不同MYSQL数据库的PHP程序
Oct 09 PHP
php magic_quotes_gpc的一点认识与分析
Aug 18 PHP
php实现的仿阿里巴巴实现同类产品翻页
Dec 11 PHP
PHP header()函数使用详细(301、404等错误设置)
Apr 17 PHP
PHP采用XML-RPC构造Web Service实例教程
Jul 16 PHP
php使用simplexml_load_file加载XML文件并显示XML的方法
Mar 19 PHP
php显示时间常用方法小结
Jun 05 PHP
php mysql like 实现多关键词搜索的方法
Oct 29 PHP
PHP使用星号替代用户名手机和邮箱的实现代码
Feb 07 PHP
php记录搜索引擎爬行记录的实现代码
Mar 02 PHP
PHP数组常用函数实例小结
Aug 20 PHP
laravel中Redis队列监听中断的分析
Sep 14 PHP
Yii2中DropDownList简单用法示例
Jul 18 #PHP
Yii2使用dropdownlist实现地区三级联动功能的方法
Jul 18 #PHP
Yii2框架dropDownList下拉菜单用法实例分析
Jul 18 #PHP
用HTML/JS/PHP方式实现页面延时跳转的简单实例
Jul 18 #PHP
浅谈PHP正则中的捕获组与非捕获组
Jul 18 #PHP
Yii2.0表关联查询实例分析
Jul 18 #PHP
php 实现301重定向跳转实例代码
Jul 18 #PHP
You might like
PHP 巧用数组降低程序的时间复杂度
2010/01/01 PHP
三个类概括PHP的五种设计模式
2012/09/05 PHP
PHP限制HTML内容中图片必须是本站的方法
2015/06/16 PHP
PHP页面输出搜索后跳转下一页的处理方法
2016/09/30 PHP
PHP+JavaScript实现无刷新上传图片
2017/02/21 PHP
神奇的7个jQuery 3D插件整理
2011/01/06 Javascript
解析Jquery中如何把一段html代码动态写入到DIV中(实例说明)
2013/07/09 Javascript
JavaScript html5 canvas绘制时钟效果(二)
2016/03/27 Javascript
用nodejs搭建websocket服务器
2017/01/23 NodeJs
jQuery监听浏览器窗口大小的变化实例
2017/02/07 Javascript
js实现二级菜单点击显示当前内容效果
2018/04/28 Javascript
js数组去重的N种方法(小结)
2018/06/07 Javascript
jQuery.parseJSON()函数详解
2019/02/28 jQuery
Vuex新手的理解与使用详解
2019/05/31 Javascript
vue2.0 获取从http接口中获取数据,组件开发,路由配置方式
2019/11/04 Javascript
[04:02]2014DOTA2国际邀请赛 BBC每日综述中国战队将再度登顶
2014/07/21 DOTA
[44:37]完美世界DOTA2联赛PWL S3 Forest vs access 第一场 12.11
2020/12/13 DOTA
python socket网络编程步骤详解(socket套接字使用)
2013/12/06 Python
Python中enumerate()函数编写更Pythonic的循环
2018/03/06 Python
python将处理好的图像保存到指定目录下的方法
2019/01/10 Python
python里运用私有属性和方法总结
2019/07/08 Python
对Pytorch神经网络初始化kaiming分布详解
2019/08/18 Python
python opencv 实现对图像边缘扩充
2020/01/19 Python
解决springboot yml配置 logging.level 报错问题
2020/02/21 Python
Django DRF APIView源码运行流程详解
2020/08/17 Python
极简鞋类,赤脚的感觉:Lems Shoes
2019/08/06 全球购物
材料加工硕士生求职信
2013/10/10 职场文书
医科大学生毕业的自我评价分享
2013/11/12 职场文书
2014国庆节幼儿园亲子活动方案
2014/09/16 职场文书
街道社区活动报告
2015/02/05 职场文书
北京青年观后感
2015/06/15 职场文书
读《解忧杂货店》有感:请相信一切都是最好的安排
2019/11/07 职场文书
vue使用节流函数的踩坑实例指南
2021/05/20 Vue.js
微信告警的zabbix监控系统 监控整个NGINX集群
2022/04/18 Servers
MySQL导致索引失效的几种情况
2022/06/25 MySQL
zabbix 代理服务器的部署与 zabbix-snmp 监控问题
2022/07/15 Servers