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最常用的ini函数分析 针对PHP.ini配置文件
Apr 22 PHP
在PHP中PDO解决中文乱码问题的一些补充
Sep 06 PHP
解析关于wamp启动是80端口被占用的问题
Jun 21 PHP
php中常量DIRECTORY_SEPARATOR用法深入分析
Nov 14 PHP
php使用mysqli向数据库添加数据的方法
Mar 20 PHP
php使用NumberFormatter格式化货币的方法
Mar 21 PHP
CodeIgniter与PHP5.6的兼容问题
Jul 16 PHP
php二维码生成
Oct 19 PHP
前端必学之PHP语法基础
Jan 01 PHP
php并发加锁示例
Oct 17 PHP
php中static 静态变量和普通变量的区别
Dec 01 PHP
PHP实现驼峰样式字符串(首字母大写)转换成下划线样式字符串的方法示例
Aug 10 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 define函数的使用说明
2008/08/27 PHP
php下保存远程图片到本地的办法
2010/08/08 PHP
php 生成短网址原理及代码
2014/01/23 PHP
php修改文件上传限制方法汇总
2015/04/07 PHP
Joomla使用Apache重写模式的方法
2016/05/04 PHP
PHP session会话操作技巧小结
2016/09/27 PHP
php+Memcached实现简单留言板功能示例
2017/02/15 PHP
Js 弹出框口并返回值的两种常用方法
2010/12/30 Javascript
各情景下元素宽高的获取实现代码
2011/09/13 Javascript
jQuery学习笔记 更改jQuery对象
2012/09/19 Javascript
JavaScript 模式之工厂模式(Factory)应用介绍
2012/11/15 Javascript
JS实现一个按钮的方法
2015/02/05 Javascript
jQuery实现大转盘抽奖活动仿QQ音乐代码分享
2015/08/21 Javascript
javascript判断网页是关闭还是刷新
2015/09/12 Javascript
手机移动端实现 jquery和HTML5 Canvas的幸运大奖盘特效
2016/12/06 Javascript
Vue2.0权限树组件实现代码
2017/08/29 Javascript
基于vue组件实现猜数字游戏
2020/05/28 Javascript
Koa项目搭建过程详细记录
2018/04/12 Javascript
React SSR样式及SEO的实践
2018/10/22 Javascript
VSCode使用之Vue工程配置eslint
2019/04/30 Javascript
[06:24]DOTA2 2015国际邀请赛中国区预选赛第二日TOP10
2015/05/27 DOTA
Python 中迭代器与生成器实例详解
2017/03/29 Python
Python中文分词工具之结巴分词用法实例总结【经典案例】
2017/04/15 Python
python opencv调用笔记本摄像头
2019/08/28 Python
浅谈pytorch中的BN层的注意事项
2020/06/23 Python
利用python 下载bilibili视频
2020/11/13 Python
移动端适配 使px自动转换rem
2019/08/26 HTML / CSS
意大利体育用品和运动服网上商店:Maxi Sport
2019/09/14 全球购物
params有什么用
2016/03/01 面试题
半年思想汇报
2013/12/30 职场文书
小学三年级数学教学反思
2014/01/31 职场文书
财务工作疏忽检讨书
2014/09/11 职场文书
故意伤害人身损害赔偿协议书
2014/11/19 职场文书
幼儿园大班教师个人工作总结
2015/02/05 职场文书
青年教师个人总结
2015/02/11 职场文书
python 实现德洛内三角剖分的操作
2021/04/22 Python