溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PHP驗(yàn)證碼類

發(fā)布時(shí)間:2020-07-11 11:16:18 來(lái)源:網(wǎng)絡(luò) 閱讀:427 作者:woaijorden 欄目:web開發(fā)
class Captcha{
    //驗(yàn)證碼寬
    private $width;
    //驗(yàn)證碼高度
    private $height;
    //驗(yàn)證碼長(zhǎng)度
    private $len;
    //驗(yàn)證碼范圍
    private $str =  "abcdefghijkmnpqrstuvwxyz23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
    //字體地址
    private $font_dir = 'F:\phpStudy\PHPTutorial\WWW\php\code\4.ttf';
    //驗(yàn)證碼資源
    private $img;
    //驗(yàn)證碼字符串
    private $code;

    public function __construct($config=['width'=>200,'height'=>50,'len'=>4]){
        $this->width=$config['width'];
        $this->height=$config['height'];
        $this->len=$config['len'];

    }

    public function entry(){
        $this->createBg();
        $this->getCode();
        $this->setCode();
        $this->setLine();
        $this->setDot();
        $this->setBg();

    }

    //生成畫布
    private function createBg(){
        $this->img = imagecreate($this->width,$this->height);
        //分配驗(yàn)證碼的背景顏色
        imagecolorallocate($this->img,255,255,255);

    }

    //生成隨機(jī)字符串
    private function getCode(){
        for($i=0;$i<$this->len;$i++){
            $key = mt_rand(0,strlen($this->str)-1);
            $this->code .= $this->str[$key];
        }
        $this->setSession();
    }

    //把驗(yàn)證碼存入session之后和用戶輸入的驗(yàn)證碼做比較
    private function setSession(){
        session_start();
        $_SESSION['code']=strtolower($this->code);
    }

    //輸出驗(yàn)證碼到畫布
    private function setCode(){
        for($i=0;$i<$this->len;$i++){
            $font_color = imagecolorallocate($this->img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
            $x = ($this->width/$this->len)*$i+5;
            $fontSize = mt_rand(18,28);
            $fontHeight=imagefontheight($fontSize);
            $y=  mt_rand($fontHeight+5,$this->height);
            imagettftext($this->img, $fontSize, mt_rand(-25,25), $x, $y, $font_color, $this->font_dir,$this->code[$i]);
        }
    }

    //畫干擾線
    private function setLine(){
        for ($i=0; $i <$this->len ; $i++) { 
            $line_color = imagecolorallocate($this->img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
            imageline($this->img, mt_rand(0,$this->width/$this->len), mt_rand(0,$this->height), mt_rand($this->width-$this->width/$this->len,$this->width),  mt_rand(0,$this->height), $line_color);
        }
    }

    //畫干擾點(diǎn)到畫布
    private function setDot(){
        for ($i=0; $i < $this->width ; $i++) { 
            $dot_color = imagecolorallocate($this->img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
            imagesetpixel($this->img, mt_rand(0,$this->width), mt_rand(0,$this->height), $dot_color);
        }
    }

    //輸出成圖像
    private function setBg(){
        ob_end_clean();
        header('content-type:image/jpeg');
        imagejpeg($this->img);
    }

    public function __destruct(){
        imagedestroy($this->img);
    }
}

//輸出驗(yàn)證碼
$captcha = new Captcha();
$captcha->entry();

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI