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 相关文章推荐
第四节 构造函数和析构函数 [4]
Oct 09 PHP
数据库中排序的对比及使用条件详解
Feb 23 PHP
php读取excel文件示例分享(更新修改excel)
Feb 27 PHP
php使用curl抓取qq空间的访客信息示例
Feb 28 PHP
从PHP的源码中深入了解stdClass类
Apr 18 PHP
PHP 记录访客的浏览信息方法
Jan 29 PHP
PHP实现正则表达式分组捕获操作示例
Feb 03 PHP
PHP使用Redis长连接的方法详解
Feb 12 PHP
详解php中生成标准uuid(guid)的方法
Apr 28 PHP
Yii2框架加载css和js文件的方法分析
May 25 PHP
php5.3/5.4/5.5/5.6/7常见新增特性汇总整理
Feb 27 PHP
PHP 自动加载类原理与用法实例分析
Apr 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下用GD生成生成缩略图的两个选择和区别
2007/04/17 PHP
php数组合并array_merge()函数使用注意事项
2014/06/19 PHP
Laravel 5.4前后台分离,通过不同的二级域名访问方法
2019/10/13 PHP
使用Apache的rewrite
2021/03/09 Servers
javascript Zifa FormValid 0.1表单验证 代码打包下载
2007/06/08 Javascript
JavaScript 面向对象的之私有成员和公开成员
2010/05/04 Javascript
JavaScript动态调整TextArea高度的代码
2010/12/28 Javascript
jQuery中prev()方法用法实例
2015/01/08 Javascript
JavaScript数据结构和算法之二叉树详解
2015/02/11 Javascript
Jquery全选与反选点击执行一次的解决方案
2015/08/14 Javascript
nodejs初步体验篇
2015/11/23 NodeJs
用JS动态改变表单form里的action值属性的两种方法
2016/05/25 Javascript
聊一聊JS中this的指向问题
2016/06/17 Javascript
webpack+vue.js快速入门教程
2016/10/12 Javascript
node(koa2) web应用模块介绍详解
2019/03/29 Javascript
微信小程序列表时间戳转换实现过程解析
2019/10/12 Javascript
javascript实现的图片预览和上传功能示例【兼容IE 9】
2020/05/01 Javascript
Vue通过provide inject实现组件通信
2020/09/03 Javascript
[05:40]DOTA2荣耀之路6:Wings最后进攻
2018/05/30 DOTA
[52:31]VP vs Serenity 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
Python实现破解12306图片验证码的方法分析
2017/12/29 Python
Python实现爬虫抓取与读写、追加到excel文件操作示例
2018/06/27 Python
Python 十六进制整数与ASCii编码字符串相互转换方法
2018/07/09 Python
Django实战之用户认证(初始配置)
2018/07/16 Python
利用Python模拟登录pastebin.com的实现方法
2019/07/12 Python
python连接打印机实现打印文档、图片、pdf文件等功能
2020/02/07 Python
金融专业个人求职信范文
2013/11/28 职场文书
研究生求职推荐信范文
2013/11/30 职场文书
技术总监岗位职责
2013/12/05 职场文书
函授毕业个人自我评价
2014/02/20 职场文书
《雪地里的小画家》教学反思
2014/02/22 职场文书
陈欧广告词
2014/03/14 职场文书
国庆促销活动总结
2014/08/29 职场文书
网络工程专业大学生求职信
2014/10/01 职场文书
2016寒假假期总结
2015/10/10 职场文书
Redis+Lua脚本实现计数器接口防刷功能(升级版)
2022/02/12 Redis