一個較完整的PHP驗證碼類


 

 

具體目錄如下:

fontStyle 存放字體文件(window系統有字體文件)

securitCode.class.php代碼如下:

 

 

<?php
class securityCode{
    private $width;//驗證碼的寬
    private $height;//驗證碼的高    
    private $codeNum;//驗證碼中的字符數
    private $type;//驗證碼中字符的類型,1=數字,2=字母,3=字母加數字(默認)
    private $fontStyle;//驗證碼字體樣式,需引入字體文件    
    private $dot;//驗證碼雪花點數    
    private $line;//驗證碼干擾線條
    private $image;//驗證碼圖片
    private $chars;//驗證碼字符串
    
    function __construct($width=100,$height=40,$codeNum=4,$type=3,$session='securityCode',$fontStyle='fontStyle/MSYH.TTC',$dot=50,$line=4){
        session_start();
        $this->width=$width;
        $this->height=$height;
        $this->codeNum=$codeNum;
        $this->type=$type;
        $this->session=$session;
        $this->fontStyle=$fontStyle;
        $this->dot=$dot;
        $this->line=$line;
        $this->image=$this->createImage();    
        $this->chars=$this->createChar();
        $_SESSION [$this->session] = $this->chars;//將驗證碼的值放入session中,以便驗證登陸    
    }
    //展現圖片
    public function showImage($red=232,$green=155,$blue=55){
        $color=imagecolorallocate($this->image, $red, $green, $blue);
        imagefilledrectangle($this->image, 0, 0, $this->width, $this->height, $color);
        for($i = 0; $i <$this->codeNum; $i++) {
            $size = mt_rand ( 16, 18 );//取值有待完善
            $angle = mt_rand ( - 15, 15 );
            $x = 10 + $i * $size;
            $y = mt_rand ( 20, 26 );
            $color=imagecolorallocate($this->image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
            $text = substr ( $this->chars, $i, 1 );
            imagettftext ( $this->image, $size, $angle, $x, $y, $color, $this->fontStyle, $text );
        }
        $this->interferon();
        header ( "content-type:image/gif" );
        imagegif ( $this->image );//顯示圖片
        imagedestroy ( $this->image );//銷毀圖片
    }
    //生成圖片
    private function createImage(){        
        $image=imagecreatetruecolor($this->width, $this->height);                
        return $image;    
    }
    //生成驗證碼字符
    private function createChar(){
        if($this->type==1){
            $chars=implode('', range(0, 9));//生成數字
        }else if($this->type==2){
            $chars=implode('', array_merge(range('A','Z'),range('a', 'z')));//生成字母
        }elseif ($this->type==3) {
            $chars=implode('', array_merge(range(0, 9),range('A','Z'),range('a', 'z')));//生成數字字母
        }
        $chars=str_shuffle($chars);//打亂字符串的排序
        if($this->codeNum>strlen($chars)){
            exit('數字過大');//判斷截取的個數是不是超過字符串的長度
        }
        $chars=substr($chars, 0,$this->codeNum);//截取字符
        return $chars;
    }
    //生成干擾元素
    private function interferon(){
        for ($i=0; $i <$this->line ; $i++) { 
            $color = imagecolorallocate ($this->image, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) );
            imageline($this->image,mt_rand(0, $this->width-1),mt_rand(0, $this->height-1), mt_rand(0, $this->width-1), mt_rand(0, $this->height-1), $color);
        }
        for ($i=0; $i <$this->dot ; $i++) { 
            $color = imagecolorallocate ($this->image, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) );    
            imagesetpixel($this->image,mt_rand(0, $this->width-1) , mt_rand(0, $this->height-1), $color);        
        }
    }
    
}
$a =new securityCode();
$a->showImage();

 

效果圖

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM