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 相关文章推荐
透析PHP的配置文件php.ini
Oct 09 PHP
php 防止单引号,双引号在接受页面转义
Jul 10 PHP
php动态生成JavaScript代码
Mar 09 PHP
PHP图片处理类 phpThumb参数用法介绍
Mar 11 PHP
PHP实现把数字ID转字母ID
Aug 12 PHP
PHP图片裁剪函数(保持图像不变形)
May 04 PHP
php rsa加密解密使用详解
Jan 14 PHP
浅谈PHP定义命令空间的几个注意点(推荐)
Oct 29 PHP
PHP单例模式与工厂模式详解
Aug 29 PHP
PHP生成(支持多模板)二维码海报代码
Apr 30 PHP
PHP安装BCMath扩展的方法
Feb 13 PHP
Laravel相关的一些故障解决
Aug 19 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 根据url自动生成缩略图并处理高并发问题
2014/01/23 PHP
PHP开源开发框架ZendFramework使用中常见问题说明及解决方案
2014/06/12 PHP
php短网址和数字之间相互转换的方法
2015/03/13 PHP
php array_merge_recursive 数组合并
2016/10/26 PHP
jQuery 位置插件
2008/12/25 Javascript
JQuery自动触发事件的方法
2015/06/13 Javascript
JavaScript Length 属性的总结
2015/11/02 Javascript
通用无限极下拉菜单的实现代码
2016/05/31 Javascript
jQuery和hwSlider实现内容响应式可触控滑动切换效果附源码下载(二)
2016/06/22 Javascript
jQuery搜索框效果实现代码(百度关键词联想)
2021/02/25 Javascript
AngularJS  ng-repeat遍历输出的用法
2017/06/19 Javascript
JavaScript作用域、闭包、对象与原型链概念及用法实例总结
2018/08/20 Javascript
axios实现文件上传并获取进度
2020/03/25 Javascript
vue(2.x,3.0)配置跨域代理
2019/11/27 Javascript
JavaScript Reflect Metadata实现详解
2019/12/12 Javascript
python使用range函数计算一组数和的方法
2015/05/07 Python
使用python3.5仿微软记事本notepad
2016/06/15 Python
Python排序搜索基本算法之希尔排序实例分析
2017/12/09 Python
详解Python 协程的详细用法使用和例子
2018/06/15 Python
python爬虫获取百度首页内容教学
2018/12/23 Python
Python Numpy库datetime类型的处理详解
2019/07/13 Python
django修改models重建数据库的操作
2020/03/31 Python
关于jupyter打开之后不能直接跳转到浏览器的解决方式
2020/04/13 Python
python 等差数列末项计算方式
2020/05/03 Python
QT5 Designer 打不开的问题及解决方法
2020/08/20 Python
CSS3+DIV实现漂亮的动画彩色标签
2016/06/16 HTML / CSS
美国办公用品购物网站:Quill.com
2016/09/01 全球购物
阿迪达斯德国官方网站:adidas德国
2017/07/12 全球购物
构造方法和其他方法的区别
2016/04/26 面试题
法学毕业生自我鉴定
2014/01/31 职场文书
祖国在我心中的演讲稿
2014/05/04 职场文书
英文感谢信范文
2015/01/21 职场文书
2015最新学生自我评价范文
2015/03/03 职场文书
社区义诊通知
2015/04/24 职场文书
辞职信格式范文
2015/05/13 职场文书
体育部部长竞选稿
2015/11/21 职场文书