驗證碼這樣的功能可以說是無處不在了、那使用php來實現驗證碼這樣的功能呢?接下來我就將驗證碼實現封裝到一個類里面獨立出來、那么后面如果再使用到驗證碼功能。
直接引入該類文件并創建該類的實例、就可以使用驗證碼了,驗證碼類文件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){
- //針對png
- header("Content-Type:image/png");
- imagepng($this->img);
- }else if(imagetypes() & IMG_JPG){
- //針對jpg
- header("Content-Type:image/jpeg");
- imagejpeg($this->img,null,100);
- }else if(imagetypes() & IMG_GIF){
- //針對Gif
- header("Content-Type:image/gif");
- imagegif($this->img);
- }else if(imagetypes() & IMG_WBMP){
- // 針對 WBMP
- header(′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)};
- } //Vevb.com
- 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()′"/>
到這里整個功能就做完了、希望對一些哥們有用、同時也當自己做個筆記
新聞熱點
疑難解答