直接引入該類檔案並建立該類的執行個體、就可以使用驗證碼了,驗證碼類檔案vcode.class.php代碼如下
//驗證碼類class Vcode{private $width;//圖片寬度private $height;//圖片高度private $num;//驗證碼個數private $img;//圖片資源private $code;//驗證碼private $pointNum;//幹擾點個數private $lineNum;//幹擾線個數private $fontFile;//字型檔 //建構函式初始化相關資料function __construct($width=85,$height=34,$num=4){$this->width=$width;$this->height=$height;$this->num=$num;$this->code=$this->createCode();$this->pointNum=100;$this->lineNum=10;$this->fontFile="<a href="http://dwtedx.com/font/無" target="_blank" class="keylink">STL</a>ITI.TTF";} //用於設定成員屬性//@param string $key 成員屬性名//@param mixed $value 成員屬性值//@return object 返回自己對象$this,可用於連貫操作public function set($key,$val){//get_class_vars() 擷取類中的屬性群組成的數組//get_class() 返回對象的類名if(array_key_exists($key,get_class_vars(get_class($this)))){$this->setOption($key,$val);}return $this;}//設定參數private function setOption($key,$value){$this->$key=$value;}//擷取驗證碼public function getCode(){return $this->code;} //輸出映像public function outImg(){//建立映像$this->createImage();//畫驗證碼$this->drawCode();//畫幹擾元素$this->drawDisturbColor();//輸出映像$this->printImg();} //畫驗證碼private function drawCode(){$this->fontFile="./font/".$this->fontFile;for($i=0;$i<$this->num;$i++){//設定隨機顏色$randColor=imagecolorallocate($this->img,rand(0,128),rand(0,128),rand(0,128));//字型大小$fontSize=rand(20,23);//字型水平位置$x=($this->width/$this->num)*$i;//水平方向的位置$y=rand($fontSize,imagefontheight($fontSize)+3);//畫字型imagettftext($this->img,$fontSize,0,$x,$y,$randColor,$this->fontFile,$this->code{$i});}}//畫幹擾元素private function drawDisturbColor(){//畫幹擾點for($i=0;$i<$this->pointNum;$i++){//設定隨機顏色$randColor=imagecolorallocate($this->img,rand(0,255),rand(0,255),rand(0,255));//畫點imagesetpixel($this->img,rand(1,$this->width-2),rand(1,$this->height-2),$randColor);}//畫幹擾線for($i=0;$i<$this->lineNum;$i++){//設定隨機顏色$randColor=imagecolorallocate($this->img,rand(0,200),rand(0,200),rand(0,200));//畫線imageline($this->img,rand(1,$this->width-2),rand(1,$this->height-2),rand(1,$this->height-2),rand(1,$this->width-2),$randColor);}} //建立映像private function createImage(){//建立一個真彩色映像$this->img=imagecreatetruecolor($this->width,$this->height);//設定背景色$bgColor=imagecolorallocate($this->img,rand(200,255),rand(200,255),rand(200,255));//填充背景色imagefill($this->img,0,0,$bgColor);//設定邊框顏色$borderColor=imagecolorallocate($this->img,0,0,0);//畫一個邊框imagerectangle($this->img,0,0,$this->width-1,$this->height-1,$borderColor);} //輸出映像private function printImg(){if(imagetypes() & IMG_PNG){//針對pngheader("Content-Type:image/png");imagepng($this->img);}else if(imagetypes() & IMG_JPG){//針對jpgheader("Content-Type:image/jpeg");imagejpeg($this->img,null,100);}else if(imagetypes() & IMG_GIF){//針對Gifheader("Content-Type:image/gif");imagegif($this->img);}else if(imagetypes() & IMG_WBMP){// 針對 WBMPheader(′Content-Type: image/vnd.wap.wbmp′);imagewbmp($this->img);}else{die(′No image support in this PHP server′);}} //建立驗證碼private function createCode(){//預設字串$codes="123456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXY";//產生驗證碼$code="";for($i=0;$i<$this->num;$i++){$code.=$codes{rand(0,strlen($codes)-1)};}return $code;} //解構函式用於銷毀映像資源function __destruct(){imagedestroy($this->img);}}
這裡我使用的畫字型的函數是imagettftext()、因為這個函數可以自訂字型樣式、從代碼中也能看出來
傳入的參數有個字型檔屬性、如果不喜歡用這個函數可以使用imagestring()函數也行
只不過個人覺得這個函數的預設字型大小、也不好看、還是自訂字型看著舒服點
調用驗證碼類image_002.php代碼如下
//開啟Sessionsession_start();//引入驗證碼類include("vcode.class.php");//建立驗證碼類$vcode=new Vcode();//將擷取的驗證碼存入到session中$_SESSION[′code′]=$vcode->getCode();//$vcode->set("pointNum",10);//自訂幹擾點個數//$vcode->set("lineNum",10);//自訂幹擾線個數//$vcode->set("fontFile","wawa.ttf");//自訂字型檔//輸出映像$vcode->outImg();
HTML代碼如下
<img src="image_002.php" onclick="this.src=′image_002.php?Math.random()′"/>
到這裡整個功能就做完了、希望對一些哥們有用、同時也當自己做個筆記