程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 驗證碼類,php驗證碼類

驗證碼類,php驗證碼類

編輯:關於PHP編程

驗證碼類,php驗證碼類


熟悉相關圖像處理函數,簡單的驗證碼類    


1 //code class 2 class ValidateCode { 3 private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789'; //random effects 4 private $code; //code 5 private $codelen = 4 ; //code length 6 private $width = 130; //width 7 private $heigth = 50; //height 8 private $img; //image handle 9 private $font; //font file 10 private $fontsize = 20; //font size 11 private $fontcolor; //font color 12 13 //the construct initialization 14 public function __construct(){ 15 $this->font = ROOT_PATH.'/font/elephant.ttf'; 16 } 17 18 //create random code from $charset 19 private function createCode(){ 20 $_len = strlen($this->charset); 21 for($i=1;$i<=$this->codelen;$i++){ 22 $this->code .= $this->charset[mt_rand(0,$_len)]; 23 } 24 } 25 26 //create background 27 private function createBg(){ 28 $this->img = imagecreatetruecolor($this->width, $this->heigth); 29 $_color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255)); 30 imagefilledrectangle($this->img,0,0,$this->width,$this->heigth,$_color); 31 } 32 33 //create font 34 private function createFont(){ 35 $_x = $this->width / $this->codelen; 36 for($i=0;$i<$this->codelen;$i++){ 37 $this->fontcolor = imagecolorallocate($this->img, mt_rand(0,156), mt_rand(0,156), mt_rand(0,156)); 38 imagettftext($this->img, $this->fontsize, mt_rand(-30,30), $_x*$i+mt_rand(1,5), $this->heigth/1.4, $this->fontcolor, $this->font, $this->code[$i]); 39 } 40 } 41 42 //create line,snowflake 43 private function createLine(){ 44 for($i=0;$i<6;$i++){ 45 $_color = imagecolorallocate($this->img, mt_rand(0,156), mt_rand(0,156), mt_rand(0,156)); 46 imageline($this->img, mt_rand(0,$this->width), mt_rand(0, $this->heigth),mt_rand(0,$this->width), mt_rand(0, $this->heigth), $_color); 47 } 48 for($i=0;$i<100;$i++){ 49 $_color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255)); 50 imagestring($this->img, mt_rand(1, 5), mt_rand(0,$this->width), mt_rand(0,$this->heigth), '*', $_color); 51 } 52 } 53 54 //export image 55 private function outPut(){ 56 header('Content-type:image/png'); 57 imagepng($this->img); 58 imagedestroy($this->img); 59 } 60 61 //display 62 public function doimg(){ 63 $this->createBg(); 64 $this->createCode(); 65 $this->createLine(); 66 $this->createFont(); 67 $this->outPut(); 68 } 69 70 //get code 71 public function getCode(){ 72 return strtolower($this->code); 73 } 74 75 } 76

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved