PHP面向对象简易验证码类

Posted clubs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP面向对象简易验证码类相关的知识,希望对你有一定的参考价值。

php简易验证码类

  1 <?php
  2 
  3 class authCode
  4 
  5     private static $instance = null;     #实例对象
  6     private $width = 120;                #图片宽度
  7     private $height = 40;                #图片高度
  8     private $font = ‘font/elephant.ttf‘; #字体文件路径
  9     private $fontSize = 14;              #字体大小
 10     private $strLen = 6;                 #字符个数
 11     private $auth_code_str = null;       #验证码结果
 12     private $imgResult = null;           #图片资源
 13 
 14     #入口文件 静态方法调用 实例化对象 可用 对象方法调用
 15     public static function img()
 16     
 17         if (!(self::$instance instanceof self)) 
 18             self::$instance = new self();
 19         
 20         return self::$instance;
 21     
 22 
 23      #随机颜色
 24     private function randomColor($img = null, $min = 0, $max = 255)
 25     
 26         $rgb = [];
 27         for ($i = 1; $i <= 3; $i++) 
 28             $rgb[] = str_pad(rand($min, $max), 3, 0, STR_PAD_LEFT);
 29         
 30         return imagecolorallocate($img, $rgb[0], $rgb[1], $rgb[2]);
 31     
 32 
 33     #随机字符串
 34     private function randomStr($num = 4)
 35     
 36         if ($num > 0) 
 37             $string = array_merge(range(‘a‘, ‘z‘), range(0, 9), range(‘A‘, ‘Z‘), range(0, 9));
 38             for ($i = 1; $i <= $num; $i++) 
 39                 shuffle($string);
 40                 $this->auth_code_str .= array_pop($string);
 41             
 42         
 43         return $this;
 44     
 45 
 46      #创建验证码
 47     public function createAuthCode(&$codeStr = false)
 48     
 49         if (!$this->auth_code_str) 
 50             $this->randomStr($this->strLen);
 51         
 52 
 53         if ($codeStr !== false && empty($codeStr)) 
 54             $codeStr = $this->auth_code_str;
 55          else if (!empty($codeStr) && $codeStr !== false) 
 56             $this->auth_code_str = $codeStr;
 57         
 58 
 59         $this->imgResult = imagecreatetruecolor($this->width, $this->height);
 60 
 61         $background = $this->randomColor($this->imgResult, 200);
 62 
 63         imagefilledrectangle($this->imgResult, 0, 0, $this->width, $this->height, $background);
 64 
 65         $y = ($this->height - $this->fontSize);
 66 
 67         $string = str_split($this->auth_code_str, 1);
 68 
 69         for ($i = 0; $i < count($string); $i++) 
 70             $frontColor = $this->randomColor($this->imgResult, 0, 200);
 71             imagefttext($this->imgResult, $this->fontSize, rand(0, 10), ($this->fontSize + 2) * $i + 10, $y, $frontColor, $this->font, $string[$i]);
 72         
 73         return $this;
 74     
 75 
 76     #生成线
 77     public function line($line = 3)
 78     
 79         $line = $line ?: 3;
 80         for ($i = 1; $i <= $line; $i++) 
 81             $lineColor = $this->randomColor($this->imgResult, 0, 200);
 82             imageline($this->imgResult, rand(0, $this->width / 5), rand(5, $this->height - 5), rand($this->width / 1.3, $this->width), rand(5, $this->height - 5), $lineColor);
 83         
 84         return $this;
 85     
 86 
 87     #噪点
 88     public function pixel($num = 50)
 89         $num = $num ?: 3;
 90         for ($i = 1; $i <= $num; $i++) 
 91             $lineColor = $this->randomColor($this->imgResult, 0, 100);
 92             imagesetpixel($this->imgResult, rand(0, $this->width), rand(0, $this->height), $lineColor);
 93         
 94         return $this;
 95     
 96 
 97     #设置大小
 98     public function size($width = null, $height = null)
 99     
100         $this->width = $width ?: 120;
101         $this->height = $height ?: 40;
102         return $this;
103     
104 
105     #设置字体大小
106     public function fontSize($fontsize = 14)
107     
108         $this->fontSize = $fontsize ?: 14;
109         return $this;
110     
111 
112     #设置字体
113     public function font($file = null)
114     
115         if (is_null($file) === true) 
116             $this->font = ‘font/elephant.ttf‘;
117          else 
118             $this->font = $file;
119         
120         return $this;
121     
122 
123     #设置长度
124     public function strlen($num = null)
125     
126         $this->strLen = $num ?: 6;
127         return $this;
128     
129      
130     public function display()
131     
132         ob_end_flush();
133         header("content-type:image/jpeg");
134         imagejpeg($this->imgResult, null, 100);
135         imagedestroy($this->imgResult);
136         exit;
137     
138 
139 
140 #简单调用方法
141 authCode::img()->createAuthCode()->display();
142 
143 
144 #指定字符串调用
145 // $string = ‘abc123‘;
146 // authCode::img()->createAuthCode($string)->display();
147 
148 
149 #设置图片大小、字数、字体大小
150 // authCode::img()->strlen(8)->size(300,100)->fontSize(30)->createAuthCode()->display();
151 
152 
153 #添加噪点
154 // authCode::img()->createAuthCode()->line()->pixel()->display();
155 
156 
157     ?>

以上便是关于验证码类的封装过程,可以直接使用。

 

链接:https://mp.weixin.qq.com/s/7RrGadoaN-If72N30LGEEw

以上是关于PHP面向对象简易验证码类的主要内容,如果未能解决你的问题,请参考以下文章

PHP验证码类

基于PHP实现用户注册登录功能

PHP生成图片验证码demoOOP面向对象版本

PHP面向对象之命令模式

PHP基础入门---PHP面向对象

php面向对象之一