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优化的8条经验
Mar 24 PHP
PHP通过iconv将字符串从GBK转换为UTF8字符集
Jul 18 PHP
PHP输入流php://input介绍
Sep 18 PHP
php加速器eAccelerator的配置参数、API详解
May 05 PHP
CI框架自动加载session出现报错的解决办法
Jun 17 PHP
推荐25款php中非常有用的类库
Sep 29 PHP
深入理解PHP类的自动载入机制
Sep 16 PHP
php  单例模式详细介绍及实现源码
Nov 05 PHP
php file_get_contents取文件中数组元素的方法
Apr 01 PHP
php魔法函数与魔法常量使用介绍
Jul 23 PHP
laravel ORM 只开启created_at的几种方法总结
Jan 29 PHP
2020最新版 PhpStudy V8.1版本下载安装使用详解
Oct 30 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
完美解决:Apache启动问题―(OS 10022)提供了一个无效的参数
2013/06/08 PHP
解析php利用正则表达式解决采集内容排版的问题
2013/06/20 PHP
php二维数组转成字符串示例
2014/02/17 PHP
WordPress中制作导航菜单的PHP核心方法讲解
2015/12/11 PHP
PHP模板引擎Smarty中变量的使用方法示例
2016/04/11 PHP
php实现微信支付之现金红包
2018/05/30 PHP
gearman中worker常驻后台,导致MySQL server has gone away的解决方法
2020/02/27 PHP
Laravel5.3+框架定义API路径取消CSRF保护方法详解
2020/04/06 PHP
JS提交并解析后台返回的XML的代码
2008/11/03 Javascript
JS获取并操作iframe中元素的方法
2013/03/21 Javascript
jquery的相对父元素和相对文档定位示例代码
2013/08/02 Javascript
JS实现保留n位小数的四舍五入问题示例
2016/08/03 Javascript
AngularJS入门教程之XHR和依赖注入详解
2016/08/18 Javascript
Javascript从数组中随机取出不同元素的两种方法
2016/09/22 Javascript
js实现图片切换(动画版)
2016/12/25 Javascript
Angular.js自动化测试之protractor详解
2017/07/07 Javascript
取消Bootstrap的dropdown-menu点击默认关闭事件方法
2018/08/10 Javascript
基于游标的分页接口实现代码示例
2018/11/12 Javascript
小程序文字跑马灯效果
2018/12/28 Javascript
[02:15]2015国际邀请赛选手档案IG.Ferrari 430
2015/07/30 DOTA
天翼开放平台免费短信验证码接口使用实例
2013/12/18 Python
win系统下为Python3.5安装flask-mongoengine 库
2016/12/20 Python
Python面向对象基础入门之设置对象属性
2018/12/11 Python
解决pycharm工程启动卡住没反应的问题
2019/01/19 Python
Python3.6实现带有简单界面的有道翻译小程序
2019/04/16 Python
使用python分析统计自己微信朋友的信息
2019/07/19 Python
Django文件上传与下载(FileFlid)
2019/10/06 Python
python画图时设置分辨率和画布大小的实现(plt.figure())
2021/01/08 Python
HTML5触摸事件演化tap事件介绍
2016/03/25 HTML / CSS
英国知名衬衫品牌美国网站:Charles Tyrwhitt美国
2016/08/28 全球购物
Kangol帽子官网:坎戈尔袋鼠
2018/09/26 全球购物
英国伦敦的睡衣品牌:Asceno
2019/10/06 全球购物
环卫工人先进事迹材料
2014/06/02 职场文书
2015最新婚礼司仪主持词
2015/06/30 职场文书
详解Redis集群搭建的三种方式
2021/05/31 Redis
我国拿下天问一号火星着陆区附近 22 个地理实体命名:平乐、西柏坡、古田、漠河等
2022/04/29 数码科技