php中使用GD库做验证码


Posted in PHP onMarch 31, 2016
<?php 
require_once 'string.func.php';
//通过GD库做验证码
/**
 *添加验证文字
 * @param int $type
 * @param int $length
*/
function buildRandomString($type=1,$length=4){
  $row='';
  if($type==1){
    $row=join('',range(0, 9));
  }else if($type==2){
    $row=join('', array_merge(range('a','z'),range('A', 'Z')));
  }else if($type==3){
    $row=join('', array_merge(range('a','z'),range('A', 'Z'),range(0, 9)));
  };
  $row=str_shuffle($row);
  
  $row=substr($row,0,$length);
  return $row;
}
/**
 * 生成缩略图
 * @param int $type //包含数字或者英文
 * @param int $length 多少个字符
 * @param int $pixel 干扰小点的密度
 * @param int $dst_h 干扰线的密度
 * @param string //验证码在$_SESSION中的名字
 */

function verifyImage($type=1,$length=4,$pixel=0,$line=0,$sess_name = "verify"){
  //session_start();
  //创建画布
  $width = 100;
  $height = 40;
  $image = imagecreatetruecolor ( $width, $height );
  $white = imagecolorallocate ( $image, 255, 255, 255 );
  $black = imagecolorallocate ( $image, 0, 0, 0 );
  //用填充矩形填充画布
  imagefilledrectangle ( $image, 1, 1, $width - 2, $height - 2, $white );
  $chars = buildRandomString ( $type, $length );
  $_SESSION [$sess_name] = $chars;
  //$fontfiles = array ("MSYH.TTF", "MSYHBD.TTF", "SIMLI.TTF", "SIMSUN.TTC", "SIMYOU.TTF", "STZHONGS.TTF" );
  $fontfiles = array ("SIMKAI.TTF" );
  //由于字体文件比较大,就只保留一个字体,如果有需要的同学可以自己添加字体,字体在你的电脑中的fonts文件夹里有,直接运行输入fonts就能看到相应字体
  for($i = 0; $i < $length; $i ++) {
    $size = mt_rand ( 14, 18 );
    $angle = mt_rand ( - 15, 15 );
    $x = 5 + $i * $size;
    $y = mt_rand ( 20, 26 );
    $fontfile = "../fonts/" . $fontfiles [mt_rand ( 0, count ( $fontfiles ) - 1 )];
    $color = imagecolorallocate ( $image, mt_rand ( 50, 90 ), mt_rand ( 80, 200 ), mt_rand ( 90, 180 ) );
    $text = substr ( $chars, $i, 1 );
    imagettftext ( $image, $size, $angle, $x, $y, $color, $fontfile, $text );
  }
  if ($pixel) {
    for($i = 0; $i < 50; $i ++) {
      imagesetpixel ( $image, mt_rand ( 0, $width - 1 ), mt_rand ( 0, $height - 1 ), $black );
    }
  }
  if ($line) {
    for($i = 1; $i < $line; $i ++) {
      $color = imagecolorallocate ( $image, mt_rand ( 50, 90 ), mt_rand ( 80, 200 ), mt_rand ( 90, 180 ) );
      imageline ( $image, mt_rand ( 0, $width - 1 ), mt_rand ( 0, $height - 1 ), mt_rand ( 0, $width - 1 ), mt_rand ( 0, $height - 1 ), $color );
    }
  }
  header ( "content-type:image/gif" );
  imagegif ( $image );
  imagedestroy ( $image );
}

主要要点:

1、如果前面没有申明session_start();则需要申明;
2、字体可以在cmf输入fonts下载到自己定义的fonts文件夹;
3、$_SESSION [$sess_name]可以通过$_POST获得用户输入的验证码进行比较。

PHP 相关文章推荐
用 php 编写的日历
Oct 09 PHP
PHP 加密/解密函数 dencrypt(动态密文,带压缩功能,支持中文)
Jan 30 PHP
关于页面优化和伪静态
Oct 11 PHP
一步一步学习PHP(1) php开发环境配置
Feb 15 PHP
浅析PHP递归函数返回值使用方法
Feb 18 PHP
解析PHP中VC6 X86和VC9 X86的区别及 Non Thread Safe的意思
Jun 28 PHP
PHP中substr()与explode()函数用法分析
Nov 24 PHP
php自动获取关键字的方法
Jan 06 PHP
PHP多线程编程之管道通信实例分析
Mar 07 PHP
php基本函数汇总
Jul 09 PHP
微信公众号开发之语音消息识别php代码
Aug 08 PHP
PhpStorm本地断点调试的方法步骤
May 21 PHP
php实现搜索类封装示例
Mar 31 #PHP
PHP-FPM实现性能优化
Mar 31 #PHP
PHP实现通过URL提取根域名
Mar 31 #PHP
PHP的PDO操作简单示例
Mar 30 #PHP
PHP Smarty模版简单使用方法
Mar 30 #PHP
PHP格式化MYSQL返回float类型的方法
Mar 30 #PHP
PHP获取网页所有连接的方法(附demo源码下载)
Mar 30 #PHP
You might like
德生S2000南麂列岛台湾FM收听记录
2021/03/02 无线电
php实现mysql同步的实现方法
2009/10/21 PHP
使用Smarty 获取当前日期时间和格式化日期时间的方法详解
2013/06/18 PHP
PhpStorm本地断点调试的方法步骤
2018/05/21 PHP
PHP实现的微信APP支付功能示例【基于TP5框架】
2019/09/16 PHP
解决php用mysql方式连接数据库出现Deprecated报错问题
2019/12/25 PHP
PHP 8新特性简介
2020/08/18 PHP
List the Stored Procedures in a SQL Server database
2007/06/20 Javascript
(currentStyle)javascript为何有时用style得不到已设定的CSS的属性
2007/08/15 Javascript
Jquery.LazyLoad.js修正版下载,实现图片延迟加载插件
2011/03/12 Javascript
js动态生成指定行数的表格
2013/07/11 Javascript
简介JavaScript中valueOf()方法的使用
2015/06/05 Javascript
原生JS实现多个小球碰撞反弹效果示例
2018/01/31 Javascript
element上传组件循环引用及简单时间倒计时的实现
2018/10/01 Javascript
从0到1构建vueSSR项目之路由的构建
2019/03/07 Javascript
详解在Javascript中进行面向切面编程
2019/04/28 Javascript
JavaScript仿京东秒杀倒计时
2020/03/17 Javascript
Python操作MySQL简单实现方法
2015/01/26 Python
一个基于flask的web应用诞生 组织结构调整(7)
2017/04/11 Python
python放大图片和画方格实现算法
2018/03/30 Python
浅析Python与Mongodb数据库之间的操作方法
2019/07/01 Python
opencv中图像叠加/图像融合/按位操作的实现
2020/04/01 Python
浅谈JupyterNotebook导出pdf解决中文的问题
2020/04/22 Python
keras.layer.input()用法说明
2020/06/16 Python
全球最大的服务市场:Fiverr
2017/01/03 全球购物
GNC健安喜官方海外旗舰店:美国著名保健品牌
2017/01/04 全球购物
社会实践自我鉴定
2013/11/07 职场文书
留学自荐信写作方法
2014/01/27 职场文书
《猴子种树》教学反思
2014/02/14 职场文书
承诺书格式
2014/06/03 职场文书
小学“向国旗敬礼”网上签名寄语活动总结
2014/09/27 职场文书
施工现场安全管理制度
2015/08/05 职场文书
2019求职信:应届生求职信范文
2019/04/24 职场文书
2020年元旦祝福语录,总有适合你的
2019/12/31 职场文书
Python读取文件夹下的所有文件实例代码
2021/04/02 Python
react使用antd的上传组件实现文件表单一起提交功能(完整代码)
2021/06/29 Javascript