个人写的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 has encountered an Access Violation
Jan 15 PHP
PHP Mysql编程之高级技巧
Aug 27 PHP
php中在PDO中使用事务(Transaction)
May 14 PHP
php中防止伪造跨站请求的小招式
Sep 02 PHP
PHP基础知识回顾
Aug 16 PHP
Php中使用Select 查询语句的实例
Feb 19 PHP
php输入流php://input使用浅析
Sep 02 PHP
php简单实现快速排序的方法
Apr 04 PHP
PHP中Enum(枚举)用法实例详解
Dec 07 PHP
thinkPHP使用post方式查询时分页失效的解决方法
Dec 09 PHP
PHP获取数组中指定的一列实例
Dec 27 PHP
PHP生成zip压缩包的常用方法示例
Aug 22 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
《魔兽世界》惊魂幻象将获得调整
2020/03/08 其他游戏
Smarty+QUICKFORM小小演示
2007/02/25 PHP
在PHP中PDO解决中文乱码问题的一些补充
2010/09/06 PHP
PHP循环结构实例讲解
2014/02/10 PHP
php中json_encode UTF-8中文乱码的更好解决方法
2014/09/28 PHP
php使用curl获取https请求的方法
2015/02/11 PHP
一个简单的php MVC留言本实例代码(必看篇)
2016/09/22 PHP
PHP 多任务秒级定时器的实现方法
2018/05/13 PHP
PHP实现的pdo连接数据库并插入数据功能简单示例
2019/03/30 PHP
利用javaScript实现点击输入框弹出窗体选择信息
2013/12/11 Javascript
jQuery获取节点和子节点文本的方法
2014/07/22 Javascript
详细解读AngularJS中的表单验证编程
2015/06/19 Javascript
基于jquery实现ajax无刷新评论
2020/08/19 Javascript
Bootstrap模态框使用详解
2017/02/15 Javascript
深入浅析Node.js单线程模型
2017/07/10 Javascript
基于es6三点运算符的使用方法(实例讲解)
2017/10/12 Javascript
es6中class类静态方法,静态属性,实例属性,实例方法的理解与应用分析
2020/02/15 Javascript
js实现飞机大战小游戏
2020/08/26 Javascript
[01:59]翻天覆地,因你而变,7.20版本地图更新速览
2018/11/24 DOTA
[01:38:19]夜魇凡尔赛茶话会 第五期
2021/03/11 DOTA
Python实现简单网页图片抓取完整代码实例
2017/12/15 Python
python3学生名片管理v2.0版
2018/11/29 Python
python遍历小写英文字母的方法
2019/01/02 Python
Macbook安装Python最新版本、GUI开发环境、图像处理、视频处理环境详解
2020/02/17 Python
爱他美官方海外旗舰店:Aptamil奶粉
2017/12/22 全球购物
土耳其国际性时尚购物网站:Modanisa
2018/01/19 全球购物
新浪微博实习心得体会
2014/01/27 职场文书
职业女性的职业规划
2014/03/04 职场文书
欢迎横幅标语
2014/06/17 职场文书
处级领导班子全部召开专题民主生活会情况汇报
2014/09/27 职场文书
工伤死亡理赔协议书
2014/10/20 职场文书
男方婚前保证书
2015/02/28 职场文书
2016大学自主招生推荐信范文
2015/03/23 职场文书
客服专员岗位职责范本
2015/04/07 职场文书
详解Python 3.10 中的新功能和变化
2021/04/28 Python
如何给HttpServletRequest增加消息头
2021/06/30 Java/Android