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 多个submit提交表单 处理方法
Jul 07 PHP
php知道与问问的采集插件代码
Oct 12 PHP
PHP 函数执行效率的小比较
Oct 17 PHP
《PHP编程最快明白》第五讲:php目录、文件操作
Nov 01 PHP
使用dump函数,给php加断点测试
Jun 25 PHP
PHP用身份证号获取星座和生肖的方法
Nov 07 PHP
PHP5.5和之前的版本empty函数的不同之处
Jun 13 PHP
php截取中文字符串函数实例
Feb 23 PHP
PHP编译安装时常见错误解决办法
May 28 PHP
PHP图形计数器程序显示网站用户浏览量
Jul 20 PHP
Smarty模板常见的简单应用分析
Nov 15 PHP
php基于数组函数实现关联表的编辑操作示例
Jul 04 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 数学运算验证码实现代码
2009/10/11 PHP
php目录操作函数之获取目录与文件的类型
2010/12/29 PHP
FirePHP 推荐一款PHP调试工具
2011/04/23 PHP
php中把美国时间转为北京时间的自定义函数分享
2014/07/28 PHP
thinkphp中ajax与php响应过程详解
2014/12/08 PHP
PHP的Yii框架中行为的定义与绑定方法讲解
2016/03/18 PHP
Zero Clipboard js+swf实现的复制功能使用方法
2010/03/07 Javascript
Extjs EditorGridPanel中ComboBox列的显示问题
2011/07/04 Javascript
jquery通过a标签删除table中的一行的代码
2013/12/02 Javascript
javascript Deferred和递归次数限制实例
2014/10/21 Javascript
javascript 动态修改css样式方法汇总(四种方法)
2015/08/27 Javascript
牛叉的Jquery——Jquery与DOM对象的互相转换及DOM的三种操作
2015/10/29 Javascript
实例详解AngularJS实现无限级联动菜单
2016/01/15 Javascript
学做Bootstrap的第一个页面
2016/05/15 HTML / CSS
基于BootStrap的图片轮播效果展示实例代码
2016/05/23 Javascript
Bootstrap基本插件学习笔记之Popover提示框(19)
2016/12/08 Javascript
javascript中的深复制详解及实例分析
2016/12/29 Javascript
H5基于iScroll实现下拉刷新和上拉加载更多
2017/07/18 Javascript
通过js实现压缩图片上传功能
2020/02/25 Javascript
如何在node环境实现“get数据解析”代码实例
2020/07/03 Javascript
python实现根据用户输入从电影网站获取影片信息的方法
2015/04/07 Python
Python脚本文件打包成可执行文件的方法
2015/06/02 Python
详解Django中Request对象的相关用法
2015/07/17 Python
python+selenium+autoit实现文件上传功能
2017/08/23 Python
canvas实现漂亮的下雨效果的示例
2018/04/18 HTML / CSS
英国Zoro工具:手动工具,电动工具和个人防护用品
2016/11/02 全球购物
奥林匹亚体育:Olympia Sports
2020/12/30 全球购物
采购类个人求职的自我评价
2014/02/18 职场文书
我们的节日国庆活动方案
2014/08/19 职场文书
团队会宣传标语
2014/10/09 职场文书
2014年社区工作总结
2014/11/18 职场文书
公务员考察材料
2014/12/23 职场文书
2015年幼儿园个人工作总结
2015/04/25 职场文书
英语演讲开场白
2015/05/29 职场文书
机器人总动员观后感
2015/06/09 职场文书
MySQL Server层四个日志的实现
2022/03/31 MySQL