个人写的PHP验证码生成类分享


Posted in PHP onAugust 21, 2014

此验证码类直接拿去就可以用,也可以参考!

其中类成员codestr是生成的验证码字符串:

<?php
/**
 * 验证码
 */
class Code{
 
  // 1. 定义各个成员 有宽、高、画布、字数、类型、画类型
   
  private $width; //宽度
  private $height; //高度
  private $num; //验证码字数
  private $imgType; //生成图片类型
  private $Type; //字串类型 1,2,3 三个选项 1 纯数字 2 纯小写字母 3 大小写数字混合
  private $hb; //画布
  public $codestr; // 验证码字串
 
  public function __construct($height=20,$num=4,$imgType="jpeg",$Type=1){
    $this->width = $num*20;
    $this->height = $height;
    $this->num = $num;
    $this->imgType = $imgType;  
    $this->Type = $Type; 
    $this->codestr = $this->codestr();
    $this->zuhe();
  }
 
  // 2. 定义随机获取字符串函数
  private function codestr(){
    switch($this->Type){
     
      case 1:   // 类型为1 获取1-9随机数
        $str = implode("",array_rand(range(0,9),$this->num));
        break;
      case 2:   // 类型为2 获取a-z随机小写字母
        $str = implode("",array_rand(array_flip(range(a,z)),$this->num));
        break;
      case 3:   // 类型为3 获取数字,小写字母,大写字母 混合
        for($i=0;$i<$this->num;$i++){
          $m = rand(0,2);
          switch($m){
            case 0:
              $o = rand(48,57);
              break;
            case 1:
              $o = rand(65,90);
              break;
            case 2:
              $o = rand(97,122);
              break; 
          }
          $str .= sprintf("%c",$o);
        }
        break;     
    }
 
     
    return $str;  
  }
 
 
  // 3. 初始化画布图像资源
  private function Hb(){
    $this->hb = imagecreatetruecolor($this->width,$this->height); 
  }
 
  // 4. 生成背景颜色
  private function Bg(){
    return imagecolorallocate($this->hb,rand(130,250),rand(130,250),rand(130,250)); 
  }
 
  // 5. 生成字体颜色
  private function Font(){
    return imagecolorallocate($this->hb,rand(0,100),rand(0,100),rand(0,100));  
  }
 
  // 6. 填充背景颜色
  private function BgColor(){
    imagefilledrectangle($this->hb,0,0,$this->width,$this->height,$this->Bg()); 
  }
 
  // 7. 干扰点
  private function ganrao(){
    $sum=floor(($this->width)*($this->height)/3);
    for($i=0;$i<$sum;$i++){
      imagesetpixel($this->hb,rand(0,$this->width),rand(0,$this->height),$this->Bg());  
    }
  }
 
  // 8. 随机直线 弧线
  private function huxian(){
    for($i=0;$i<$this->num;$i++){
      imageArc($this->hb,rand(0,$this->width),rand(0,$this->height),rand(0,$this->width),rand(0,$this->height),rand(0,360),rand(0,360),$this->Bg());    
    }  
  }
 
  // 9. 写字
  private function xiezi(){
    for($i=0;$i<$this->num;$i++){
      $x=ceil($this->width/$this->num)*$i; 
      $y=rand(1,$this->height-15);
      imagechar($this->hb,5,$x+4,$y,$this->codestr[$i],$this->Font());
    }  
  }
 
  // 10. 输出
  private function OutImg(){
    $shuchu="image".$this->imgType; 
    $header="Content-type:image/".$this->imgType;
    if(function_exists($shuchu)){
      header($header);
      $shuchu($this->hb); 
    }else{
      exit("GD库没有此类图像"); 
    }
  }
 
  // 11. 拼装
  private function zuhe(){
    $this->Hb();
    $this->BgColor();
    $this->ganrao();
    $this->huxian();
    $this->xiezi();
    $this->OutImg(); 
  }  
 
  public function getCodeStr(){
    return $this->codestr;    
  }
}
?>
PHP 相关文章推荐
PHP与SQL注入攻击[一]
Apr 17 PHP
快速开发一个PHP扩展图文教程
Dec 12 PHP
php 生成唯一id的几种解决方法
Mar 08 PHP
php socket通信(tcp/udp)实例分析
Feb 14 PHP
PHP目录与文件操作技巧总结(创建,删除,遍历,读写,修改等)
Sep 11 PHP
PHP请求远程地址设置超时时间的解决方法
Oct 29 PHP
php 截取utf-8格式的字符串实例代码
Oct 30 PHP
PHP经典实用正则表达式小结
May 04 PHP
Windows下php+mysql5.7配置教程
May 16 PHP
TP(thinkPHP)框架多层控制器和多级控制器的使用示例
Jun 13 PHP
Laravel6.2中用于用户登录的新密码确认流程详解
Oct 16 PHP
laravel ajax curd 搜索登录判断功能的实现
Apr 17 PHP
PHP中使用sleep造成mysql读取失败的案例和解决方法
Aug 21 #PHP
从零开始学YII2框架(六)高级应用程序模板
Aug 20 #PHP
ThinkPHP登录功能的实现方法
Aug 20 #PHP
从零开始学YII2框架(五)快速生成代码工具 Gii 的使用
Aug 20 #PHP
PHP面向对象程序设计之类常量用法实例
Aug 20 #PHP
从零开始学YII2框架(四)扩展插件yii2-kartikgii
Aug 20 #PHP
PHP面向对象程序设计之接口用法
Aug 20 #PHP
You might like
php学习笔记 [预定义数组(超全局数组)]
2011/06/09 PHP
php+Memcached实现简单留言板功能示例
2017/02/15 PHP
php如何修改SESSION的生存存储时间的实例代码
2017/07/05 PHP
修改yii2.0用户登录使用的user表为其它的表实现方法(推荐)
2017/08/01 PHP
Yii框架学习笔记之应用组件操作示例
2019/11/13 PHP
Javascript 表单之间的数据传递代码
2008/12/04 Javascript
javascript 打印页面代码
2009/03/24 Javascript
JS实现鼠标经过好友列表中的好友头像时显示资料卡的效果
2014/07/02 Javascript
jQuery实现简易的天天爱消除小游戏
2015/10/16 Javascript
JS &amp; JQuery 动态添加 select option
2016/06/08 Javascript
Bootstrap基本插件学习笔记之Tooltip提示工具(18)
2016/12/08 Javascript
bootstrap table实现点击翻页功能 可记录上下页选中的行
2017/09/28 Javascript
vue在手机中通过本机IP地址访问webApp的方法
2018/08/15 Javascript
React事件处理的机制及原理
2018/12/03 Javascript
js页面加载后执行的几种方式小结
2020/01/30 Javascript
手机浏览器唤起微信分享(JS)
2020/10/11 Javascript
Vue路由权限控制解析
2020/11/09 Javascript
[48:53]2014 DOTA2华西杯精英邀请赛 5 25 LGD VS VG第一场
2014/05/26 DOTA
[03:48]显微镜下的DOTA2第四期——TP动作
2014/06/20 DOTA
python函数返回多个值的示例方法
2013/12/04 Python
python多线程编程方式分析示例详解
2013/12/06 Python
python数据结构之图的实现方法
2015/07/08 Python
python基础练习之几个简单的游戏
2017/11/10 Python
ubuntu中配置pyqt4环境教程
2017/12/27 Python
matplotlib实现区域颜色填充
2019/03/18 Python
Python集成开发工具Pycharm的安装和使用详解
2020/03/18 Python
Sneaker Studio匈牙利:购买运动鞋
2018/03/26 全球购物
会计应聘求职信范文
2013/12/17 职场文书
大学生就业自我推荐信
2014/05/10 职场文书
房屋所有权证明
2014/10/20 职场文书
2014年少先队工作总结
2014/12/03 职场文书
2015年电厂工作总结范文
2015/05/13 职场文书
升学宴学生致辞
2015/07/27 职场文书
2016大学先进团支部事迹材料
2016/03/01 职场文书
导游词之潮音寺
2019/09/26 职场文书
Python人工智能之混合高斯模型运动目标检测详解分析
2021/11/07 Python