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 相关文章推荐
mysql建立外键
Nov 25 PHP
用PHP的ob_start();控制您的浏览器cache!
Feb 14 PHP
PHP用mysql数据库存储session的代码
Mar 05 PHP
php开发文档 会员收费1期
Aug 14 PHP
CodeIgniter使用phpcms模板引擎
Nov 12 PHP
PHP 数据结构队列(SplQueue)和优先队列(SplPriorityQueue)简单使用实例
May 12 PHP
php验证码实现代码(3种)
Sep 07 PHP
laravel学习教程之关联模型
Jul 30 PHP
利用php_imagick实现复古效果的方法
Oct 18 PHP
thinkPHP自定义类实现方法详解
Nov 30 PHP
Laravel自动生成UUID,从建表到使用详解
Oct 24 PHP
laravel框架中控制器的创建和使用方法分析
Nov 23 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 导出数据到淘宝助手CSV的方法分享
2010/02/27 PHP
CodeIgniter框架中_remap()使用方法2例
2014/03/10 PHP
php 伪静态之IIS篇
2014/06/02 PHP
yii2中使用Active Record模式的方法
2016/01/09 PHP
Javascript 检测、添加、移除样式(className)函数代码
2009/09/08 Javascript
document.getElementById的简写方式(获取id对象的简略写法)
2010/09/10 Javascript
jquery下jstree简单应用 - v1.0
2011/04/14 Javascript
javascript中onclick(this)用法介绍
2013/04/19 Javascript
jQuery+ajax实现鼠标单击修改内容的思路
2014/06/29 Javascript
最全面的JS倒计时代码
2016/09/17 Javascript
Javascript实现倒计时时差效果
2017/05/18 Javascript
Vue实现购物车场景下的应用
2017/11/27 Javascript
nodeJS服务器的创建和重新启动的实现方法
2018/05/12 NodeJs
[10:21]DOTA2-DPC中国联赛 正赛 PSG.LGD vs Aster 选手采访
2021/03/11 DOTA
实例讲解python函数式编程
2014/06/09 Python
好用的Python编辑器WingIDE的使用经验总结
2016/08/31 Python
python列表的增删改查实例代码
2018/01/30 Python
python实现淘宝秒杀聚划算抢购自动提醒源码
2020/06/23 Python
Python把对应格式的csv文件转换成字典类型存储脚本的方法
2019/02/12 Python
Python产生一个数值范围内的不重复的随机数的实现方法
2019/08/21 Python
DJANGO-URL反向解析REVERSE实例讲解
2019/10/25 Python
解决python replace函数替换无效问题
2020/01/18 Python
35款精致的 CSS3 和 HTML5 网页模板 推荐
2012/08/03 HTML / CSS
美国最大的半成品净菜电商:Blue Apron(蓝围裙)
2018/04/27 全球购物
大学生在校学习的自我评价
2014/02/18 职场文书
大学生新学期计划书
2014/04/28 职场文书
消防工作实施方案
2014/06/09 职场文书
课外访万家心得体会
2014/09/03 职场文书
党的群众路线教育实践活动整改落实情况自查报告
2014/10/28 职场文书
二年级学生期末评语
2014/12/26 职场文书
北京故宫导游词
2015/01/31 职场文书
2015毕业生简历自我评价
2015/03/02 职场文书
求职意向书范本
2015/05/11 职场文书
2016公务员年度考核评语
2015/12/01 职场文书
七年级作文之下雨天
2019/12/23 职场文书
使用css样式设计一个简单的html登陆界面的实现
2021/03/30 HTML / CSS