開發一個本身的驗證碼類

在網絡上尤爲是須要登陸的網站,驗證碼簡直是到處可見,驗證碼的形式也是五花八門:靜態圖片,動態圖片,語音,滑塊.
而如今我就打算作一個本身的生成圖片形式的驗證碼類,固然,用的是我吃飯的語言php.
gd庫是必須的,對應php.ini裏的gd2擴展必須打開
先建立個類,好比我起了個名字gcudCaptcha,而後聲明幾個基礎屬性php

<?php


namespace gcud;


class gcudCaptcha
{
//單位彷佛是像素
    private $Weight=150,$Height=50;
}


之前我是用過gd庫,藉此機會也正好熟悉下
首先須要建立一個指定寬高的畫布,因而寫個方法瀏覽器

private function CreateImage(){
//gd庫用於建立畫布的方法
    imagecreate($this->Weight,$this->Height);
}


同時再加個$Image屬性,用於接收建立的圖像網絡

private $Image;
    private function CreateImage(){
        $this->Image=imagecreate($this->Weight,$this->Height);
    }


因爲建立的圖像須要直接輸出在瀏覽器上,再寫個輸出的方法函數

private function Output(){
    //讓瀏覽器識別圖片
    header('Content-Type:image/jpeg');
    //設定輸出類型爲jpeg圖像
    imagejpeg($this->Image);
    //記得釋放資源
    imagedestroy($this->Image);
}


另外須要對外暴露一個生成的方法字體

    public function Generate(){
        $this->Output();
    }


並且在初始化時要調用建立圖像網站

    public function __construct()
    {
        $this->CreateImage();
    }


因而如今整個代碼變成了ui

<?php


namespace gcud;


class gcudCaptcha
{
    private $Weight = 150, $Height = 50, $Image;

    public function __construct()
    {
        $this->CreateImage();
    }

    private function CreateImage()
    {
        $this->Image = imagecreate($this->Weight, $this->Height);
    }

    private function Output()
    {
        header('Content-Type:image/jpeg');
        imagejpeg($this->Image);
        imagedestroy($this->Image);
    }

    public function Generate()
    {
        $this->Output();
    }
}


隨便在一個php中調用this

<?php
//個人類是放到這裏的
require_once 'Vendor\gcud\gcudCaptcha.php';
$gcudCaptcha=new \gcud\gcudCaptcha();
$gcudCaptcha->Generate();


能夠看到瀏覽器輸出了一個黑色的圖片,接下來設定一個背景色,設定背景色RGB屬性,並寫在建立圖像函數裏面spa

private $BackgroundColorRGB=[127,127,127];
    private function CreateImage()
    {
        $this->Image = imagecreate($this->Weight, $this->Height);
        imagecolorallocate($this->Image,$this->BackgroundColorRGB[0],$this->BackgroundColorRGB[1],$this->BackgroundColorRGB[2]);
    }

再運行代碼就能看到灰色的圖片code

接下來就要畫驗證碼了,添加一個驗證碼屬性和驗證碼顏色屬性並在構造函數裏初始化

private $Captcha,$CaptchaRGB=[255,100,37];
    public function __construct($Captcha)
    {
        $this->Captcha=$Captcha;
        $this->CreateImage();
        $this->CaptchaRGB=imagecolorallocate($this->Image,$this->CaptchaRGB[0],$this->CaptchaRGB[1],$this->CaptchaRGB[2]);
    }

還要指定一個字體,並畫驗證碼

private $Font='C:'.DIRECTORY_SEPARATOR.'Windows'.DIRECTORY_SEPARATOR.'Fonts'.DIRECTORY_SEPARATOR.'simhei.ttf';
private function DrawCaptcha(){
//參數:圖片,字體大小,角度(彷佛是0-360),x座標,y座標,驗證碼顏色,字體,驗證碼
        imagettftext($this->Image,20,0,10,30,$this->CaptchaRGB,$this->Font,$this->Captcha);
    }

在生成驗證碼方法進行調用,並在實例化時傳入驗證碼

public function Generate()
    {
        $this->DrawCaptcha();
        $this->Output();
    }
//驗證碼類無論驗證碼字符串的生成,驗證
$gcudCaptcha=new \gcud\gcudCaptcha('123456');
$gcudCaptcha->Generate();

再運行一遍就能看橙色的123456出現,仔細看還能看到花花綠綠的失真現象,不想看到這種狀況能夠把imagejpeg換成imagepng函數,header類型也能夠換下,但其實除了下載圖片的後綴名瀏覽器同樣能正確解析

到這裏其實算是實現了驗證碼,但想起驗證碼的初衷,爲了防止被快速破[這也和諧]解就必須加大難度

先看看目前的全部代碼

<?php


namespace gcud;


class gcudCaptcha
{
    private $Weight = 150, $Height = 50, $Image;
    private $BackgroundColorRGB=[127,127,127],$CaptchaRGB=[255,100,37];
    private $Captcha,$Font='C:'.DIRECTORY_SEPARATOR.'Windows'.DIRECTORY_SEPARATOR.'Fonts'.DIRECTORY_SEPARATOR.'simhei.ttf';
    public function __construct($Captcha)
    {
        $this->Captcha=$Captcha;
        $this->CreateImage();
        $this->CaptchaRGB=imagecolorallocate($this->Image,$this->CaptchaRGB[0],$this->CaptchaRGB[1],$this->CaptchaRGB[2]);
    }

    private function CreateImage()
    {
        $this->Image = imagecreate($this->Weight, $this->Height);
        imagecolorallocate($this->Image,$this->BackgroundColorRGB[0],$this->BackgroundColorRGB[1],$this->BackgroundColorRGB[2]);
    }

    private function Output()
    {
        header('Content-Type:image/png');
        imagepng($this->Image);
        imagedestroy($this->Image);
    }
    private function DrawCaptcha(){
        imagettftext($this->Image,20,0,10,30,$this->CaptchaRGB,$this->Font,$this->Captcha);
    }

    public function Generate()
    {
        $this->DrawCaptcha();
        $this->Output();
    }
}

因此第一個方法是把每一個字符隨機旋轉繪製,因而繪製方法更改以下

private function DrawCaptcha(){
        $Length=strlen($this->Captcha);
        $X=0;
        for ($I=0;$I<$Length;$I++){
            imagettftext($this->Image,20,mt_rand(0,360),$X,30,$this->CaptchaRGB,$this->Font,$this->Captcha[$I]);
            $X+=20;
        }
    }

再次運行就能看到東倒西歪的驗證碼,但僅僅如此還不夠,還須要把其它幾個參數利用起來,能夠嘗試隨機的字體大小,也能夠使用不一樣字體,不一樣顏色,用中文作驗證碼等等

再寫個常見的畫線條,先加個屬性$LineRGB=[37,32,98]並在構造函數裏初始化,而後把畫線條的下在畫驗證碼那裏

private function DrawCaptcha(){
        $Length=strlen($this->Captcha);
        $X=0;
        for ($I=0;$I<$Length;$I++){
            imagettftext($this->Image,mt_rand(10,20),mt_rand(0,360),$X,30,$this->CaptchaRGB,$this->Font,$this->Captcha[$I]);
//參數圖片,x起點,y起點,x終點,y終點,顏色
            imageline($this->Image,mt_rand(0,$this->Weight),mt_rand(0,$this->Height),mt_rand(0,$this->Weight),mt_rand(0,$this->Height),$this->LineRGB);
            $X+=20;
        }
    }

更多的就不弄了,有興趣的本身研究,最後奉上完整代碼

<?php


namespace gcud;


class gcudCaptcha
{
    private $Weight = 150, $Height = 50, $Image;
    private $BackgroundColorRGB=[127,127,127],$CaptchaRGB=[255,100,37],$LineRGB=[37,32,98];
    private $Captcha,$Font='C:'.DIRECTORY_SEPARATOR.'Windows'.DIRECTORY_SEPARATOR.'Fonts'.DIRECTORY_SEPARATOR.'simhei.ttf';
    public function __construct($Captcha)
    {
        $this->Captcha=$Captcha;
        $this->CreateImage();
        $this->CaptchaRGB=imagecolorallocate($this->Image,$this->CaptchaRGB[0],$this->CaptchaRGB[1],$this->CaptchaRGB[2]);
        $this->LineRGB=imagecolorallocate($this->Image,$this->LineRGB[0],$this->LineRGB[1],$this->LineRGB[2]);
    }

    private function CreateImage()
    {
        $this->Image = imagecreate($this->Weight, $this->Height);
        imagecolorallocate($this->Image,$this->BackgroundColorRGB[0],$this->BackgroundColorRGB[1],$this->BackgroundColorRGB[2]);
    }

    private function Output()
    {
        header('Content-Type:image/png');
        imagepng($this->Image);
        imagedestroy($this->Image);
    }
    private function DrawCaptcha(){
        $Length=strlen($this->Captcha);
        $X=0;
        for ($I=0;$I<$Length;$I++){
            imagettftext($this->Image,mt_rand(10,20),mt_rand(0,360),$X,30,$this->CaptchaRGB,$this->Font,$this->Captcha[$I]);
            imageline($this->Image,mt_rand(0,$this->Weight),mt_rand(0,$this->Height),mt_rand(0,$this->Weight),mt_rand(0,$this->Height),$this->LineRGB);
            $X+=20;
        }
    }

    public function Generate()
    {
        $this->DrawCaptcha();
        $this->Output();
    }
}
相關文章
相關標籤/搜索