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迅雷、快车、旋风下载专用链转换代码
Jun 15 PHP
基于php实现长连接的方法与注意事项的问题
May 10 PHP
php生成随机颜色的方法
Nov 13 PHP
php批量删除cookie的简单实现方法
Jan 26 PHP
php的crc32函数使用时需要注意的问题(不然就是坑)
Apr 21 PHP
PHP数组游标实现对数组的各种操作详解
Jan 26 PHP
PHP设计模式之观察者模式实例
Feb 22 PHP
mysql desc(DESCRIBE)命令实例讲解
Sep 24 PHP
php文件上传、下载和删除示例
Aug 28 PHP
使用php自动备份数据库表的实现方法
Jul 28 PHP
PHP7匿名类的用法示例
Apr 05 PHP
关于Laravel参数验证的一些疑与惑
Nov 19 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 session 写入数据库
2016/02/13 PHP
基于PHP实现短信验证码接口(容联运通讯)
2016/09/06 PHP
PHP读取Excel内的图片(phpspreadsheet和PHPExcel扩展库)
2019/11/19 PHP
清空上传控件input file的值
2010/07/03 Javascript
javascript陷阱 一不小心你就中招了(字符运算)
2013/11/10 Javascript
javascript实现简单的全选和反选功能
2016/01/05 Javascript
前端js弹出框组件使用方法
2020/08/24 Javascript
Vue.js 插件开发详解
2017/03/29 Javascript
php register_shutdown_function函数详解
2017/07/23 Javascript
JSON 数据格式详解
2017/09/13 Javascript
详解如何在angular2中获取节点
2017/11/23 Javascript
layer弹出层全屏及关闭方法
2018/08/17 Javascript
创建echart多个联动的示例代码
2018/11/23 Javascript
jquery获取img的src值实例介绍
2019/01/16 jQuery
JS实现的全选、全不选及反选功能【案例】
2019/02/19 Javascript
浅谈python可视化包Bokeh
2018/02/07 Python
使用python编写监听端
2018/04/12 Python
用python标准库difflib比较两份文件的异同详解
2018/11/16 Python
Python使用Turtle库绘制一棵西兰花
2019/11/23 Python
Python 使用 prettytable 库打印表格美化输出功能
2019/12/26 Python
Tensorflow分批量读取数据教程
2020/02/07 Python
Python列表如何更新值
2020/05/27 Python
html5中canvas图表实现柱状图的示例
2017/11/13 HTML / CSS
Ryderwear澳洲官网:澳大利亚高端健身训练装备品牌
2018/09/18 全球购物
华为的Java面试题
2014/03/07 面试题
经济与贸易专业应届生求职信
2013/11/19 职场文书
聘任书的写作格式及范文
2014/03/29 职场文书
感恩教育月活动总结
2014/07/07 职场文书
火锅店的活动方案
2014/08/15 职场文书
授权委托书怎么写
2014/09/25 职场文书
文员转正自我鉴定怎么写
2014/09/29 职场文书
党员考试作弊检讨书1000字
2015/02/16 职场文书
会计简历自我评价
2015/03/10 职场文书
培训班开班主持词
2015/07/02 职场文书
2019最新婚庆对联集锦!
2019/07/10 职场文书
canvas绘制折线路径动画实现
2021/05/12 Javascript