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代码
Sep 14 PHP
PHPMailer使用教程(PHPMailer发送邮件实例分析)
Dec 06 PHP
有关PHP性能优化的介绍
Jun 20 PHP
PHP滚动日志的代码实现
Jun 10 PHP
PHP的Yii框架中创建视图和渲染视图的方法详解
Mar 29 PHP
php实现的中文分词类完整实例
Feb 06 PHP
php使用flock阻塞写入文件和非阻塞写入文件的实例讲解
Jul 10 PHP
php实现简单的权限管理的示例代码
Aug 25 PHP
PHP PDOStatement::getAttribute讲解
Feb 01 PHP
Laravel推荐使用的十个辅助函数
May 10 PHP
解决laravel5.4下的group by报错的问题
Oct 16 PHP
PhpStorm+xdebug+postman调试技巧分享
Sep 15 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中的正则表达式以及模式匹配
2013/06/19 PHP
PHP实现原生态图片上传封装类方法
2016/11/08 PHP
Yii2下session跨域名共存的解决方案
2017/02/04 PHP
PDO::exec讲解
2019/01/28 PHP
JQuery 选项卡效果(JS与HTML的分离)
2010/04/01 Javascript
js判断请求的url是否可访问,支持跨域判断的实现方法
2016/09/17 Javascript
过期软件破解办法实例详解
2017/01/04 Javascript
js实现音频控制进度条功能
2017/04/01 Javascript
Angularjs中的验证input输入框只能输入数字和小数点的写法(推荐)
2017/08/16 Javascript
浅谈react-router HashRouter和BrowserRouter的使用
2017/12/29 Javascript
基于Vuex无法观察到值变化的解决方法
2018/03/01 Javascript
一次Webpack配置文件的分离实战记录
2018/11/30 Javascript
Vue-Cli项目优化操作的实现
2019/10/27 Javascript
vue中选中多个选项并且改变选中的样式的实例代码
2020/09/16 Javascript
[42:32]DOTA2上海特级锦标赛B组资格赛#2 Fnatic VS Spirit第二局
2016/02/27 DOTA
python基础教程之获取本机ip数据包示例
2014/02/10 Python
Python实现对字符串的加密解密方法示例
2017/04/29 Python
Python中easy_install 和 pip 的安装及使用
2017/06/05 Python
Python中修改字符串的四种方法
2018/11/02 Python
用Python读取几十万行文本数据
2018/12/24 Python
Python配置文件处理的方法教程
2019/08/29 Python
python 输出列表元素实例(以空格/逗号为分隔符)
2019/12/25 Python
解决Jupyter notebook中.py与.ipynb文件的import问题
2020/04/21 Python
Python如何实现后端自定义认证并实现多条件登陆
2020/06/22 Python
如何在python中处理配置文件代码实例
2020/09/27 Python
详解HTML5中rel属性的prefetch预加载功能使用
2016/05/06 HTML / CSS
加拿大花店:1800Flowers.ca
2016/11/16 全球购物
I.T集团香港官方商城:ITeSHOP.com Hong Kong
2019/02/15 全球购物
经理助理岗位职责
2014/03/05 职场文书
战友聚会主持词
2014/04/02 职场文书
大学生求职计划书
2014/04/30 职场文书
工作目标责任书
2014/07/23 职场文书
先进基层党组织事迹材料
2014/12/25 职场文书
个人培训总结
2015/03/05 职场文书
Nginx下配置Https证书详细过程
2021/04/01 Servers
MySQL REVOKE实现删除用户权限
2021/06/18 MySQL