本文給大家分享的是一個php封裝的驗證碼類的代碼和原理及思路,非常的清晰詳細,有需要的小夥伴可以參考下
驗證碼是我們開發的時候經常用到的功能,所以在此本人封裝了一個驗證碼類,應該可以作為php的類外掛程式用,在此分享給各位讀友。
實現的原理也是很簡單,就是利用畫布的幾個函數,再加上一些字串的擷取,東湊西湊就構成了,呵呵。
這裡大概寫一下思路吧,其實這個類已經注釋的非常清楚了,不過,個人還是在行文前囉嗦一下。
首先是關於一些函數的解釋,這裡的解釋純屬個人體會,有什麼錯誤的地方,還請讀者指正。
1、建立畫布函數:imagecreatetruecolor(w,h);
說明:用於建立一個畫布。
w 畫布的寬
h 畫布的高
此函數的傳回值資源類(gd)
2、為畫布建立一種顏色:imagecolorallocate(img,red,green,blue)
說明:
img畫布資源
red,green,blue 是0~255的範圍
3、為畫布添加背景色
imagefill(img,x,y,color);
說明:
在 image 映像的座標 x,y(映像左上方為 0, 0)
4、畫邊框
imagerectangle($img,x1,y1,x2,y2,color);
說明:
其左上方座標為 x1, y1,右下角座標為 x2, y2。映像的左上方座標為 0, 0。
3、繪製內容(字元)
imagestring(img ,size,x,y,string,color);
說明:
img畫布
size是字大小 1至5
x,y是起始點
string是所要畫的內容
color是顏色
4、告訴瀏覽器圖片格式
header("Content-type:image/png");可為image/gif等等
5、輸出(或儲存),也可以使用第2個參數實現儲存
imagepng(img【,filename】)
imagejpeg(img【,filename】)
imagegif(img【,filename】)
6、添加幹擾線,本質就是直線
imageline(img,x1,y1,x2,y2,color);
說明:
img 畫布
x1,y1 起點
x2,y2 終點
color 顏色
7、imagettftext ( img,size, angle, x, y, color, fontfile,text )
說明:
img 畫布
size 字型大小,預設單位像素
angle 角度
x,y 座標點
color 顏色
fontfile 字型檔,必須是中文字型
text 內容
特別說明:這裡的color參數都是imagecolorallocate()函數建立的顏色
下面是思路:
這裡最先產生畫布,之後就是為畫布添加字串,直線,噪點,邊框,來產生驗證碼的,最後類返回的兩個公用介面是:可供外面調用的產生驗證碼的畫布和驗證碼的字串構成,為的是給外界輸出驗證碼畫布,以及儲存字串,作為驗證用
下面是代碼:
<?phpnamespace captcha;/**驗證碼類*verify方法產生驗證碼字串*entry方法產生驗證碼*特別提醒:這裡要先用entry產生驗證碼,再用verify產生驗證碼的字串,也就是必須先調用entry,然後才能夠調用verify產生驗證碼的字串,原因代碼已經說明問題了,因為驗證碼的字串是在entry方法調用captchaImage產生的,所以必須先調用它才行*有的地方對中文的字型要求比較高,所以,有的地方不支援中文驗證碼*/class Captcha{ //配置參數 private $config = array(); //驗證碼 private $verifyCode = ''; //擷取設定檔的配置資訊,給類傳參數就行,例如new Captcha($config);$config是你的設定檔資訊 public function __construct($config=array('width'=>100,'height'=>40,'length'=>4,'size'=>7,'lines'=>0,'dots'=>0,'font'=>'simfang.ttf','rectangle'=>array(255,55,122),'charset'=>true,'chinese'=>'來到新機場主航站樓建設在婚姻關係存續期間所負債務她在收到要求她償還前夫在婚姻關係存續期間所欠債務的法院傳票後要精益求精善始善終')){ $this->config = $config; } //建立驗證碼 private function captchaImage(){ //畫布 $img = imagecreatetruecolor($this->config['width'],$this->config['height']); //填充畫布顏色 imagefill($img,0,0,imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255))); //需要邊框則畫邊框 if($this->config['rectangle'] && is_array($this->config['rectangle']) && count($this->config['rectangle']) == 3){ $this->tangle($img); } $this->verifyCode = $this->code($img,$this->config['charset'],$this->config['chinese']); //存在則添加幹擾線 if($this->config['lines']){ $this->codeLines($img); } //存在則添加幹擾點 if($this->config['dots']){ $this->codeDots($img); } return $img; } private function codeLines($img){ //繪製幹擾線 for($i=0;$i<$this->config['lines'];$i++){ imageline($img,mt_rand(0,$this->config['width'] / 10),mt_rand(0,$this->config['height']),mt_rand($this->config['width'] * 7/ 10,$this->config['width'] * 9/ 10),mt_rand(0,$this->config['height']),imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255))); } } private function codeDots($img){ //添加噪點 for($i=0;$i<$this->config['dots'];$i++){ //噪點顏色 $color = imagecolorallocate($img,mt_rand(0,180),mt_rand(0,180),mt_rand(0,180)); imagestring($img,mt_rand(1,3),mt_rand(0,170),mt_rand(0,30),'*',$color); } } /*畫布邊框*/ private function tangle($img){ imagerectangle($img,0,0,$this->config['width']-1,$this->config['height']-1,imagecolorallocate($img,$this->config['rectangle'][0],$this->config['rectangle'][1],$this->config['rectangle'][2])); } /*產生驗證碼,預設英文,$ch為true則為中文*/ private function code($img,$ch=false,$set=''){ $str = ""; //計算間隔 $span = ceil($this->config['width']/($this->config['length']+1)); if($ch && !empty($set)){ //隨機產生字元 $set = $this->config['chinese']; for($i=0;$i<$this->config['length'];$i++){ $end = strlen($set)/3; $pos = mt_rand(0,$end-1); $str .= substr($set,$pos*3,3); } //每次繪製一個字元 for($i=1;$i<=$this->config['length'];$i++){ imagettftext($img,16,mt_rand(-30,60),$i*$span,$this->config['height']*3/5,imagecolorallocate($img,mt_rand(0,180),mt_rand(0,180),mt_rand(0,180)),$this->config['font'],substr($str,($i-1)*3,3)); } }else{ //隨機產生字母或者數字 for($i=0;$i<$this->config['length'];$i++){ switch(mt_rand(0,2)){ case 0: $str .= chr(mt_rand(65,90)); break; case 1: $str .= chr(mt_rand(97,122)); break; case 2: $str .= chr(mt_rand(48,57)); } } //每次繪製一個字元 for($i=1;$i<=$this->config['length'];$i++){ imagestring($img,$this->config['size'],$i*$span,0,$str[$i-1],imagecolorallocate($img,mt_rand(0,180),mt_rand(0,180),mt_rand(0,180))); } } return $str; } //擷取驗證碼 public function verify(){ return $this->verifyCode; } //產生驗證碼 public function entry(){ header("content-type:image/png"); imagepng($this->captchaImage()); }}$ob = new Captcha;$ob->entry();
最後,為了不誤人子弟,還是再強調一遍:
這裡必須先用entry產生驗證碼,再用verify產生驗證碼的字串,也就是必須先調用entry,然後才能夠調用verify產生驗證碼的字串,原因代碼已經說明問題了,因為驗證碼的字串是在entry方法的方法captchaImage中產生的,所以必須先調用它才行 有的地方對中文的字型要求比較高,所以,有的地方不支援中文驗證碼
以上就是本文的全部內容,希望對大家的學習有所協助。