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 相关文章推荐
第4章 数据处理-php正则表达式-郑阿奇(续)
Jul 04 PHP
基于PHP文件操作的详解
Jun 05 PHP
destoon实现不同会员组公司名称显示不同的颜色的方法
Aug 22 PHP
PHP闭包实例解析
Sep 08 PHP
php查询ip所在地的方法
Dec 05 PHP
Yii2实现同时搜索多个字段的方法
Aug 10 PHP
使用Yii2实现主从数据库设置
Nov 20 PHP
Laravel学习教程之View模块详解
Sep 18 PHP
ThinkPHP实现转换数据库查询结果数据到对应类型的方法
Nov 16 PHP
搜索附近的人PHP实现代码
Feb 11 PHP
laravel返回统一格式错误码问题
Nov 04 PHP
laravel excel 上传文件保存到本地服务器功能
Nov 14 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安装扩展mcrypt以及相关依赖项(PHP安装PECL扩展的方法)
2013/07/05 PHP
2个比较经典的PHP加密解密函数分享
2014/07/01 PHP
php实现的简易扫雷游戏实例
2015/07/09 PHP
Js四则运算函数代码
2012/07/21 Javascript
正则表达式中特殊符号及正则表达式的几种方法总结(replace,test,search)
2013/11/26 Javascript
js判断字符是否是汉字的两种方法小结
2014/01/03 Javascript
通过复制Table生成word和excel的javascript代码
2014/01/20 Javascript
解决jquery插件冲突的问题
2014/01/23 Javascript
jQuery模拟360浏览器切屏效果幻灯片(附demo源码下载)
2016/01/29 Javascript
jQuery插件实现图片轮播特效
2016/06/16 Javascript
JavaScript动态添加css样式和script标签
2016/07/19 Javascript
vue-cli3脚手架的配置及使用教程
2018/08/28 Javascript
微信小程序webview 脚手架使用详解
2019/07/22 Javascript
javascript实现视频弹幕效果(两个版本)
2019/11/28 Javascript
vue+elementUI(el-upload)图片压缩,默认同比例压缩操作
2020/08/10 Javascript
python中django框架通过正则搜索页面上email地址的方法
2015/03/21 Python
Flask入门之上传文件到服务器的方法示例
2018/07/18 Python
Python编程在flask中模拟进行Restful的CRUD操作
2018/12/28 Python
PyQt 实现使窗口中的元素跟随窗口大小的变化而变化
2019/06/18 Python
python tkinter窗口最大化的实现
2019/07/15 Python
使用TensorFlow实现简单线性回归模型
2019/07/19 Python
Python编程学习之如何判断3个数的大小
2019/08/07 Python
python常用数据重复项处理方法
2019/11/22 Python
pygame实现飞机大战
2020/03/11 Python
如何在Anaconda中打开python自带idle
2020/09/21 Python
使用CSS3的rem属性制作响应式页面布局的要点解析
2016/05/24 HTML / CSS
阿玛尼化妆品美国官网:Giorgio Armani Beauty
2017/02/02 全球购物
便利店投资的创业计划书
2014/01/12 职场文书
《小山羊和小灰兔》教学反思
2014/02/19 职场文书
在校实习生求职信
2014/06/18 职场文书
我们的节日国庆活动方案
2014/08/19 职场文书
学校开学标语
2014/10/06 职场文书
投资公司董事长岗位职责
2015/04/16 职场文书
2015年教师业务工作总结
2015/05/26 职场文书
2015新员工工作总结范文
2015/10/15 职场文书
大学三好学生主要事迹范文
2015/11/03 职场文书