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 相关文章推荐
关于文本留言本的分页代码
Oct 09 PHP
PHP中str_replace函数使用小结
Oct 11 PHP
PHP array_multisort()函数的使用札记
Jul 03 PHP
file_get_contents(&quot;php://input&quot;, &quot;r&quot;)实例介绍
Jul 01 PHP
php 判断服务器操作系统的类型
Feb 17 PHP
php获取Google机器人访问足迹的方法
Apr 15 PHP
PHP SOCKET编程详解
May 22 PHP
php判断对象是派生自哪个类的方法
Jun 20 PHP
PHP CURL post数据报错 failed creating formpost data
Oct 16 PHP
利用php + Laravel如何实现部署自动化详解
Oct 11 PHP
php中关于换行的实例写法
Sep 26 PHP
使用php的mail()函数实现发送邮件功能
Jun 03 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创建基本身份认证站点的方法详解
2013/06/08 PHP
PHP实现数字补零功能的2个函数介绍
2014/05/12 PHP
ThinkPHP3.1新特性之多层MVC的支持
2014/06/19 PHP
php实现ip白名单黑名单功能
2015/03/12 PHP
php中文繁体和简体相互转换的方法
2015/03/21 PHP
THINKPHP在添加数据的时候获取主键id的值方法
2017/04/03 PHP
windows下的WAMP环境搭建图文教程(推荐)
2017/07/27 PHP
利用毫秒减值计算时长的js代码
2013/09/22 Javascript
js简单实现让文本框内容逐个字的显示出来
2013/10/22 Javascript
jquery使用append(content)方法注意事项分享
2014/01/06 Javascript
JavaScript页面模板库handlebars的简单用法
2015/03/02 Javascript
使用JavaScript实现旋转的彩圈特效
2015/06/23 Javascript
微信小程序 UI与容器组件总结
2017/02/21 Javascript
jQuery中clone()函数实现表单中增加和减少输入项
2017/05/13 jQuery
JavaScript使用Ajax上传文件的示例代码
2017/08/10 Javascript
nodejs使用http模块发送get与post请求的方法示例
2018/01/08 NodeJs
快速解决select2在bootstrap模态框中下拉框隐藏的问题
2018/08/10 Javascript
vue使用require.context实现动态注册路由
2020/12/25 Vue.js
[01:51]历届DOTA2国际邀请赛举办地回顾 TI9落地上海
2018/08/26 DOTA
基于python的字节编译详解
2017/09/20 Python
OpenCV2.3.1+Python2.7.3+Numpy等的配置解析
2018/01/05 Python
python搭建服务器实现两个Android客户端间收发消息
2018/04/12 Python
python实现Windows电脑定时关机
2018/06/20 Python
Python Pillow Image Invert
2019/01/22 Python
python用opencv批量截取图像指定区域的方法
2019/01/24 Python
Python PyQt5整理介绍
2020/04/01 Python
python 中的命名空间,你真的了解吗?
2020/08/19 Python
CHARLES & KEITH加拿大官网:新加坡时尚品牌
2020/03/26 全球购物
会计应聘求职信范文
2013/12/17 职场文书
银行职业规划书范文
2013/12/28 职场文书
品牌宣传方案
2014/03/21 职场文书
领导班子四风对照检查材料范文
2014/09/27 职场文书
2015年老干部工作总结
2015/04/23 职场文书
大学生先进个人主要事迹材料
2015/11/04 职场文书
解决Maven项目中 Invalid bound statement 无效的绑定问题
2021/06/15 Java/Android
ORACLE查看当前账号的相关信息
2021/06/18 Oracle