php封装的验证码工具类完整实例


Posted in PHP onOctober 19, 2016

本文实例讲述了php封装的验证码工具类。分享给大家供大家参考,具体如下:

<?php
//验证码工具类
class Captcha{
    //属性
    private $width;
    private $height;
    private $fontsize;
    private $pixes;
    private $lines;
    private $str_len;
    /*
     * 构造方法
     * @param1 array $arr = array(),初始化属性的关联数组
    */
    public function __construct($arr = array()){
      //初始化
      $this->width = isset($arr['width']) ? $arr['width'] : $GLOBALS['config']['captcha']['width'];
      $this->height = isset($arr['height']) ? $arr['height'] : $GLOBALS['config']['captcha']['height'];
      $this->fontsize = isset($arr['fontsize']) ? $arr['fontsize'] : $GLOBALS['config']['captcha']['fontsize'];
      $this->pixes = isset($arr['pixes']) ? $arr['pixes'] : $GLOBALS['config']['captcha']['pixes'];
      $this->lines = isset($arr['lines']) ? $arr['lines'] : $GLOBALS['config']['captcha']['lines'];
      $this->str_len = isset($arr['str_len']) ? $arr['str_len'] : $GLOBALS['config']['captcha']['str_len'];
    }
    /*
     * 产生验证码图片
    */
    public function generate(){
      //制作画布
      $img = imagecreatetruecolor($this->width,$this->height);
      //给定背景色
      $bg_color = imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
      imagefill($img,0,0,$bg_color);
      //制作干扰线
      $this->getLines($img);
      //增加干扰点
      $this->getPixels($img);
      //增加验证码文字
      $captcha = $this->getCaptcha();
      //文字颜色
      $str_color = imagecolorallocate($img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
      //写入文字
      //计算文字应该出现的起始位置
      $start_x = ceil($this->width/2) - 25;
      $start_y = ceil($this->height/2) - 8;
      if(imagestring($img,$this->fontsize,$start_x,$start_y,$captcha,$str_color)){
        //成功:输出验证码
        header('Content-type:image/png');
        imagepng($img);
      }else{
        //失败
        return false;
      }
    }
    /*
     * 获取验证码随机字符串
     * @return string $captcha,随机验证码文字
    */
    private function getCaptcha(){
      //获取随机字符串
      $str = implode('',array_merge(range('a','z'),range('A','Z'),range(1,9)));
      //随机取
      $captcha = '';  //保存随机字符串
      for($i = 0,$len = strlen($str);$i < $this->str_len;$i++){
        //每次随机取一个字符
        $captcha .= $str[mt_rand(0,$len - 1)] . ' ';
      }
      //将数据保存到session
      $_SESSION['captcha'] = str_replace(' ','',$captcha);
      //返回值
      return $captcha;
    }
    /*
     * 增加干扰点
     * @param1 resource $img
    */
    private function getPixels($img){
      //增加干扰点
      for($i = 0;$i < $this->pixes;$i++){
        //分配颜色
        $pixel_color = imagecolorallocate($img,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150));
        //画点
        imagesetpixel($img,mt_rand(0,$this->width),mt_rand(0,$this->height),$pixel_color);
      }
    }
    /*
     * 增加干扰线
     * @param1 resource $img,要增加干扰线的图片资源
    */
    private function getLines($img){
      //增加干扰线
      for($i = 0;$i < $this->lines;$i++){
        //分配颜色
        $line_color = imagecolorallocate($img,mt_rand(150,200),mt_rand(150,200),mt_rand(150,200));
        //画线
        imageline($img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$line_color);
      }
    }
    /*
     * 验证验证码
     * @param1 string $captcha,用户提交的验证码
     * @return bool,成功返回true,失败返回false
    */
    public static function checkCaptcha($captcha){
      //验证码不区分大小写
      return (strtolower($captcha) === strtolower($_SESSION['captcha']));
    }
}

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
PHP 5.0对象模型深度探索之对象复制
Mar 27 PHP
用sql命令修改数据表中的一个字段为非空(not null)的语句
Jun 04 PHP
用PHP实现小写金额转换大写金额的代码(精确到分)
Jan 10 PHP
PHP系列学习之日期函数使用介绍
Aug 18 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(八)
Jun 23 PHP
php使用sql server验证连接数据库的方法
Dec 25 PHP
php导出中文内容excel文件类实例
Jul 06 PHP
php获取当前页面完整URL地址
Dec 30 PHP
PHP实现微信网页授权开发教程
Jan 19 PHP
3种方法轻松处理php开发中emoji表情的问题
Jul 18 PHP
PHP ob缓存以及ob函数原理实例解析
Nov 13 PHP
php png失真的原因及解决办法
Oct 24 PHP
php封装的图片(缩略图)处理类完整实例
Oct 19 #PHP
php封装的表单验证类完整实例
Oct 19 #PHP
php魔术方法功能与用法实例分析
Oct 19 #PHP
php封装的smartyBC类完整实例
Oct 19 #PHP
php封装的smarty类完整实例
Oct 19 #PHP
PHP内存缓存功能memcached示例
Oct 19 #PHP
PHP实现上传图片到 zimg 服务器
Oct 19 #PHP
You might like
php disk_free_space 返回目录可用空间
2010/05/10 PHP
JavaScript Base64编码和解码,实现URL参数传递。
2006/09/18 Javascript
innertext , insertadjacentelement , insertadjacenthtml , insertadjacenttext 等区别
2007/06/29 Javascript
关于IE BUG与字符串截取substr的解决办法
2013/04/10 Javascript
通过javascript把图片转化为字符画
2013/10/24 Javascript
使用JavaScript获取电池状态的方法
2014/05/03 Javascript
AngularJS中的模块详解
2015/01/29 Javascript
kindeditor修复会替换script内容的问题
2015/04/03 Javascript
javascript用函数实现对象的方法
2015/05/14 Javascript
JavaScript实现的Tween算法及缓冲特效实例代码
2015/11/03 Javascript
jQuery插件datatables使用教程
2016/04/21 Javascript
关于js函数解释(包括内嵌,对象等)
2016/11/20 Javascript
canvas绘图不清晰的解决方案
2017/02/28 Javascript
js实现时间轴自动排列效果
2017/03/09 Javascript
实现两个文本框同时输入的实例
2017/09/25 Javascript
JS实现图片转换成base64的各种应用场景实例分析
2018/06/22 Javascript
vue中锚点的三种方法
2018/07/06 Javascript
对angular 监控数据模型变化的事件方法$watch详解
2018/10/09 Javascript
vue引入静态js文件的方法
2020/06/20 Javascript
[00:12]2018DOTA2亚洲邀请赛 Sccc亮相SOLO赛,今年他又会有什么样的战绩?
2018/04/06 DOTA
浅谈python中截取字符函数strip,lstrip,rstrip
2015/07/17 Python
python中urllib.unquote乱码的原因与解决方法
2017/04/24 Python
DataFrame中去除指定列为空的行方法
2018/04/08 Python
python使用pip安装SciPy、SymPy、matplotlib教程
2019/11/20 Python
Python如何实现邮件功能
2020/05/27 Python
Python 操作 MySQL数据库
2020/09/18 Python
Canvas多边形绘制的实现方法
2019/08/05 HTML / CSS
GoDaddy英国:全球排名第一的域名注册商
2018/06/08 全球购物
美国全球旅游运营商:Pacific Holidays
2018/06/18 全球购物
Nobody Denim官网:购买高级女士牛仔裤
2021/03/15 全球购物
模范家庭事迹材料
2014/02/10 职场文书
李敖北大演讲稿
2014/05/24 职场文书
检讨书怎么写
2015/05/07 职场文书
2016年校长新年寄语
2015/08/17 职场文书
人为什么会“幸灾乐祸”?
2019/08/06 职场文书
读《茶花女》有感:山茶花的盛开与凋零
2020/01/17 职场文书