个人写的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 开源AJAX框架14种
Aug 24 PHP
php ftp文件上传函数(基础版)
Jun 03 PHP
PHP备份数据库生成SQL文件并下载的函数代码
Feb 05 PHP
php 定义404页面的实现代码
Nov 19 PHP
CodeIgniter安全相关设置汇总
Jul 03 PHP
PHP合并discuz用户脚本的方法
Aug 04 PHP
PHP实现的简单分页类及用法示例
May 06 PHP
Yii2实现增删改查后留在当前页的方法详解
Jan 13 PHP
PHP5.5安装PHPRedis扩展及连接测试方法
Jan 22 PHP
PHP实现微信小程序人脸识别刷脸登录功能
May 24 PHP
thinkPHP框架整合tcpdf插件操作示例
Aug 07 PHP
PHP addslashes()函数讲解
Feb 03 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
又拍云异步上传实例教程详解
2016/04/19 PHP
PHP 验证身份证是否合法的函数
2017/02/09 PHP
详解thinkphp中的volist标签
2018/01/15 PHP
ExtJs Excel导出并下载IIS服务器端遇到的问题
2011/09/16 Javascript
jquery仿QQ商城带左右按钮控制焦点图片切换滚动效果
2013/06/27 Javascript
用原生JS获取CLASS对象(很简单实用)
2014/10/15 Javascript
js模拟淘宝网的多级选择菜单实现方法
2015/08/18 Javascript
基于jquery实现最简单的选项卡切换效果
2016/05/08 Javascript
纯JS单页面赛车游戏制作代码分享
2017/03/03 Javascript
PHP自动加载autoload和命名空间的应用小结
2017/12/01 Javascript
浅析Visual Studio Code断点调试Vue
2018/02/27 Javascript
Vue实现固定定位图标滑动隐藏效果
2019/05/30 Javascript
js中offset,client , scroll 三大元素知识点总结
2019/09/11 Javascript
ES6使用 Array.includes 处理多重条件用法实例分析
2020/03/02 Javascript
微信小程序实现下拉加载更多商品
2020/12/29 Javascript
js面向对象方式实现拖拽效果
2021/03/03 Javascript
剖析Python的Twisted框架的核心特性
2016/05/25 Python
Python中如何获取类属性的列表
2016/12/26 Python
Python基于matplotlib绘制栈式直方图的方法示例
2017/08/09 Python
Python内存读写操作示例
2018/07/18 Python
Django重置migrations文件的方法步骤
2019/05/01 Python
探秘TensorFlow 和 NumPy 的 Broadcasting 机制
2020/03/13 Python
Python根据指定文件生成XML的方法
2020/06/29 Python
Orlebar Brown官网:设计师泳裤和泳装
2020/12/08 全球购物
受欢迎的大学生自我评价
2013/12/05 职场文书
好矿嫂事迹材料
2014/01/21 职场文书
护士岗位求职应聘自荐书范文
2014/02/12 职场文书
小学生元旦感言
2014/02/26 职场文书
岗位廉政承诺书
2014/03/27 职场文书
煤矿安全生产责任书
2014/04/15 职场文书
心理咨询专业自荐信
2014/07/07 职场文书
求职意向书
2014/07/29 职场文书
2015新学期校长寄语(3篇)
2015/03/25 职场文书
k8s部署redis cluster集群的实现
2021/06/24 Redis
JS数组去重详情
2021/11/07 Javascript
Python实现日志实时监测的示例详解
2022/04/06 Python