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序列号生成函数和字符串替换函数代码
Jun 07 PHP
FireFox浏览器使用Javascript上传大文件
Oct 30 PHP
PHP不用递归实现无限分级的例子分享
Apr 18 PHP
PHP把MSSQL数据导入到MYSQL的方法
Dec 27 PHP
PHP7+Nginx的配置与安装教程详解
May 10 PHP
Yii2框架实现注册和登录教程
Sep 30 PHP
Yii2框架可逆加密简单实现方法
Aug 25 PHP
PHP的PDO大对象(LOBs)
Jan 27 PHP
Laravel框架验证码类用法实例分析
Sep 11 PHP
PHP code 验证码生成类定义和简单使用示例
May 27 PHP
PHP延迟静态绑定使用方法实例解析
Sep 05 PHP
浅谈如何提高PHP代码质量之单元测试
May 28 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
php 图片加水印与上传图片加水印php类
2010/05/12 PHP
php清空(删除)指定目录下的文件,不删除目录文件夹的实现代码
2014/09/04 PHP
php + ajax 实现的写入数据库操作简单示例
2020/05/16 PHP
使用Rancher在K8S上部署高性能PHP应用程序的教程
2020/07/10 PHP
js 获取子节点函数 (兼容FF与IE)
2010/04/18 Javascript
jquery插件制作 手风琴Panel效果实现
2012/08/17 Javascript
jQuery模仿阿里云购买服务器选择购买时间长度的代码
2016/04/29 Javascript
JS实现的驼峰式和连字符式转换功能分析
2016/12/21 Javascript
js实现交通灯效果
2017/01/13 Javascript
原生JS实现隐藏显示图片 JS实现点击切换图片效果
2021/01/27 Javascript
深入理解ES6 Promise 扩展always方法
2017/09/26 Javascript
JavaScript简单实现合并两个Json对象的方法示例
2017/10/16 Javascript
vue中导出Excel表格的实现代码
2018/10/18 Javascript
layui-laydate时间日历控件使用方法详解
2018/11/15 Javascript
微信小程序网络请求实现过程解析
2019/11/06 Javascript
微信小程序点击滚动到指定位置的实现
2020/05/22 Javascript
移动端JS实现拖拽两种方法解析
2020/10/12 Javascript
TensorFlow实现Batch Normalization
2018/03/08 Python
python如何实现反向迭代
2018/03/20 Python
python函数式编程学习之yield表达式形式详解
2018/03/25 Python
Python目录和文件处理总结详解
2019/09/02 Python
Python hashlib模块加密过程解析
2019/11/05 Python
Python魔法方法 容器部方法详解
2020/01/02 Python
Python文本文件的合并操作方法代码实例
2020/03/31 Python
Python对excel的基本操作方法
2021/02/18 Python
HTML5 Blob对象的具体使用
2020/05/22 HTML / CSS
Stubhub英国:购买体育、演唱会和剧院门票
2018/06/10 全球购物
医学生求职自荐信
2013/10/25 职场文书
银行介绍信范文
2014/01/10 职场文书
投资协议书范本
2014/04/21 职场文书
欢迎领导标语
2014/06/27 职场文书
个人师德师风自我剖析材料
2014/09/29 职场文书
房屋产权共有协议书范本
2014/11/03 职场文书
暑期工社会实践报告
2015/07/13 职场文书
2015年秋季开学典礼校长致辞
2015/07/16 职场文书
mysql性能优化以及配置连接参数设置
2022/05/06 MySQL