簡單驗(yàn)證碼封裝類

看到了一篇文章PHP面向?qū)ο?OOP)——自封裝<驗(yàn)證碼類> 寫得不錯(cuò),就拿來模仿學(xué)習(xí)。感謝作者 @Demoer

veriy_code.jpg
  1. 復(fù)制代碼作者代碼運(yùn)行出現(xiàn)了錯(cuò)誤,仔細(xì)檢查了一下,發(fā)現(xiàn)個(gè)別手誤,
      //開始填充
      private function fillBg
      {
        imagefill($this->res,0,0$this->setDarkColor());
      }
少了()
      //開始填充
      private function fillBg()
      {
        imagefill($this->res,0,0$this->setDarkColor());
      }

以及個(gè)別處的 this , 漏了$ 符號。

  1. 修改完畢之后, 網(wǎng)頁可以正常打開生成的驗(yàn)證碼圖片。
    但是由于,想在表單處,如注冊頁面加入,卻沒辦法直接插入。作者代碼是直接生成了圖片輸出來。經(jīng)和作者交流,得到一些建議。
    a) 將實(shí)例化寫在 封裝好的類 verify.class.php 內(nèi) , 然后 在其他頁面通過
    <img src= 'verify.class.php' />

實(shí)現(xiàn)圖片生成在指定位置, 此方法可行,但是如何驗(yàn)證? 再次咨詢作者,得到建議是可以將生成驗(yàn)證碼的字符串放到session里面,其他頁面可以通過獲取用戶輸入和session內(nèi)存儲(chǔ)的驗(yàn)證碼比較。方法可行,但是個(gè)人覺得還是不在類內(nèi)實(shí)例對象。于是,本人對代碼進(jìn)行了簡單修改。

  1. 想法:通過圖片轉(zhuǎn)成二進(jìn)制,然后在html 頁面指定位置渲染。
    格式如下:
data:[<MIME-type>][;charset=<encoding>][;base64],<data>

如:

base64.png

于是修改了兩個(gè)方法
作者原來的方法是

  //輸出圖片資源
    private function outPutImg()
    {
        //header('Content-type:image/圖片類型')
        header('Content-type:image/'.$this->imgType);
        //根據(jù)圖片類型,調(diào)用不同的方法輸出圖片            
        //imagepng($img)/imagejpg($img)
        $func = 'image'.$this->imgType;
        $func($this->res);
    }
    //開始準(zhǔn)備生成圖片
    /*方法名:show()
    *功能  :調(diào)用生成圖片的所有方法
    */
    public function show()
    {
        $this->createImg();//創(chuàng)建圖片資源
        $this->fillBg();   //填充背景顏色
        $this->fillPix();  //填充干擾點(diǎn)
        $this->fillArc();  //填充干擾弧線
        $this->writeFont();//寫字
        $this->outPutImg();//輸出圖片
    }

本人將 outPutImg() 方法改成 outPutImgStr() 返回 二進(jìn)制的字符串。 在show() 方法內(nèi) 返回 從outPutImgStr() 得到的二進(jìn)制字符串。

private function outPutImgStr()
    {
        //header('Content-type:image/圖片類型')
        //header('Content-type:image/'.$this->imgType);
        ob_start();
        //根據(jù)圖片類型,調(diào)用不同的方法輸出圖片            
        //imagepng($img)/imagejpg($img)
        $func = 'image'.$this->imgType;
        $func($this->res);
        
        // Capture the output
        $imagedata = ob_get_contents();
        // Clear the output buffer
        ob_end_clean();
        //$base64 = base64_encode($imagedata);
        $base64 = 'data:image/' . $this->imgType . ';base64,' . base64_encode($imagedata);

        return $base64;
    }

//開始準(zhǔn)備生成圖片
    /*方法名:show()
    *功能  :調(diào)用生成圖片的所有方法
    */
    public function show()
    {
        $this->createImg();//創(chuàng)建圖片資源
        $this->fillBg();   //填充背景顏色
        $this->fillPix();  //填充干擾點(diǎn)
        $this->fillArc();  //填充干擾弧線
        $this->writeFont();//寫字
        return $this->outPutImgStr();//輸出圖片
    }

修改后完整代碼如下:
verify.class.php

//verify.class.php
<?php

//創(chuàng)建一個(gè)名為Verify的驗(yàn)證碼類
class Verify
{    
    /*
    定義驗(yàn)證碼屬性      
    */
    //圖片的寬度
    public  $width;
    //圖片的高度
    public  $height;
    //私有化驗(yàn)證碼字符串,避免生成后被修改    
    private $verifyCode;
    //存儲(chǔ)驗(yàn)證碼字符的個(gè)數(shù)
    public  $verifyNums;
    //存儲(chǔ)驗(yàn)證碼的字符類型 1->純數(shù)字 2->純字母 3->混合類
    public  $verifyType;
    //背景顏色
    public  $bgColor;
    //文字顏色
    public  $fontColor;
    //驗(yàn)證碼的圖片類型jpg,png,bmp……
    public  $imgType;

    //圖片資源
    private  $res;

    /*
    功能
    */
    //功能函數(shù),初始化一些可以被初始化的參數(shù)
    public function __construct($width = 100,$height = 50,$imgType = 'jpg',$verifyNums = 4,$verifyType = 1)
    {
        $this->width       = $width;
        $this->height     = $height;
        $this->imgType    = $imgType;
        $this->verifyNums = $verifyNums;
        $this->verifyType = $verifyType;

        //初始化一個(gè)可以隨機(jī)生成驗(yàn)證碼的函數(shù),將生成的驗(yàn)證碼春初到verifyCode屬性里
        $this->verifyCode = $this->createVerifyCode(); 
    }

    //隨機(jī)生成驗(yàn)證碼的函數(shù),因?yàn)椴粚ν夤迹O(shè)置為私有的
    private function createVerifyCode()
    {
        //通過判斷驗(yàn)證的類型來確定生成哪一種驗(yàn)證碼
        //verifyType=1生成純數(shù)字,為2生成純字母,為3生成混合
          switch ($this->verifyType) {
            case 1:
        /*生成純數(shù)字,首先使用range(0,9)生成數(shù)組
        *通過$this->verifyNums確定字符串的個(gè)數(shù)
        *使用array_rand()從數(shù)組中隨機(jī)獲取相應(yīng)個(gè)數(shù)
        *使用join將數(shù)字拼接成字符串,存儲(chǔ)到$str中
        */
        $str = join('',array_rand(range(0,9),$this->verifyNums));
        break;
        case 2:
        /*生成純字母,
        *小寫字母數(shù)組range('a','z')
        *大寫字母數(shù)組range('A','Z')
        *合并兩個(gè)數(shù)組array_merge()
        *更換鍵和值  array_filp()
        *隨機(jī)獲取數(shù)組中的特定個(gè)數(shù)的元素 array_rand()
        *拼接成字符串 implode()
        */
        $str = implode(array_rand(array_filp(array_merge(range('a','z'),range('A','Z'))),$this->verifyNums));
        break;
        case 3:
        //混合類型
        $words = str_shuffle('abcdefghjkmpopqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXY3456789');
        $str = substr($words,0,$this->verifyNums);
        break;
        }
        return $str;
    }


    //開始準(zhǔn)備生成圖片
    /*方法名:show()
    *功能  :調(diào)用生成圖片的所有方法
    */
    public function show()
    {
        $this->createImg();//創(chuàng)建圖片資源
        $this->fillBg();   //填充背景顏色
        $this->fillPix();  //填充干擾點(diǎn)
        $this->fillArc();  //填充干擾弧線
        $this->writeFont();//寫字
        return $this->outPutImgStr();//輸出圖片
    }

    //創(chuàng)建圖片資源:imagecreatetruecolor($width,$height)
    private function createImg()
    {
        $this->res = imagecreatetruecolor($this->width,$this->height);
    }

    //填充背景顏色:imagefill($res,$x,$y,$color)
    //隨機(jī)生成深色--->imagecolorallocate($res,$r,$g,$b)
    private function setDarkColor()
    {
      return imagecolorallocate($this->res,mt_rand(130,255),mt_rand(130,255),mt_rand(130,255));
    }    
    //隨機(jī)生成淺色
    private function setLightColor()
    {
      return imagecolorallocate($this->res,mt_rand(0,130),mt_rand(0,130),mt_rand(0,130));
    }
    //開始填充
    private function fillBg()
    {
      imagefill($this->res,0,0,$this->setDarkColor());
    }

    //隨機(jī)生成干擾點(diǎn)-->imagesetpixel
    private function fillPix()
    {
        //計(jì)算產(chǎn)生多少個(gè)干擾點(diǎn),這里設(shè)置每20個(gè)像素產(chǎn)生一個(gè)
        $num = ceil(($this->width * $this->height) / 20);
        for($i = 0; $i < $num; $i++){
            imagesetpixel($this->res,mt_rand(0,$this->width),mt_rand(0,$this->height),$this->setDarkColor());
        }
    }

    //隨機(jī)畫10條弧線->imagearc()
    private function fillArc()
    {
      for($i = 0;$i < 10;$i++){
            imagearc($this->res,
            mt_rand(10,$this->width-10),
            mt_rand(5,$this->height-5),
            mt_rand(0,$this->width),
            mt_rand(0,$this->height),
            mt_rand(0,180),
            mt_rand(181,360),
            $this->setDarkColor());
      }
    }

    /*在畫布上寫文字
    *根據(jù)字符的個(gè)數(shù),將畫布橫向分成相應(yīng)的塊
    $every = ceil($this->width/$this->verifyNums);
    *每一個(gè)小塊的隨機(jī)位置畫上對應(yīng)的字符
    imagechar();
    */

    private function writeFont()
    {
        $every = ceil($this->width / $this->verifyNums);
        for($i = 0;$i < $this->verifyNums;$i++){
            $x = mt_rand(($every * $i) + 5,$every * ($i + 1) - 5);
            $y = mt_rand(5,$this->height - 10);

            imagechar($this->res,6,$x,$y,$this->verifyCode[$i],$this->setLightColor());
        }
    }

    //輸出圖片資源

    private function outPutImgStr()
    {
        //header('Content-type:image/圖片類型')
        //header('Content-type:image/'.$this->imgType);
        ob_start();
        //根據(jù)圖片類型,調(diào)用不同的方法輸出圖片            
        //imagepng($img)/imagejpg($img)
        $func = 'image'.$this->imgType;
        $func($this->res);
        
        // Capture the output
        $imagedata = ob_get_contents();
        // Clear the output buffer
        ob_end_clean();
        //$base64 = base64_encode($imagedata);
        $base64 = 'data:image/' . $this->imgType . ';base64,' . base64_encode($imagedata);

        return $base64;
    }

    //設(shè)置驗(yàn)證碼字符只能調(diào)用,不能修改,用來驗(yàn)證驗(yàn)證碼書否輸入正確
    public function __get($name){
      if($name = 'verifyCode'){
        return $this->verifyCode;
      }
    }

    //析構(gòu)方法,自動(dòng)銷毀圖片資源
    public function __destruct()
    {
      imagedestroy($this->res);
    }
}

?>

項(xiàng)目目錄結(jié)構(gòu)
verifyCode
-- verify.class.php
-- index.php

index.php

<?php

    //引用
    include('verify.class.php');
    //實(shí)例化
    $verify = new Verify(100,40,'png',4,3);
    //得到的二進(jìn)制圖片
    $a= $verify->show();
    //得到生成的驗(yàn)證碼字符串,后面用于比較
    $b= $verify->__get('verifyCode');

?>

<html>
<head>
    
    <style type="text/css">
        .here{
            width:500px;
            height:500px;
            margin: 50px auto;
        }
        .here > input, image{
            margin: 20px;
        }
        .here > input{
            height: 40px;
        }
        .here > img{
            margin-left: 60px;
        }
    </style>
</head>
<body>
    <div class="here">
        name:<input type="text" name="name"> <br>
        <img src="<?php echo $a ?>"> <br>
        code:<input type="text" name="code">
        <p>correct code : <?php echo $b;?></p>
    </div>

 </body>
</html>

效果如下:

verify_code.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,694評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,323評論 25 708
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,828評論 18 399
  • 本來旅行中的人,忙著享受旅途的生活 忙著用眼睛觀風(fēng)景,雙腳踏土地 讓心去感受周圍,沒時(shí)間記錄文字 於是這樣的淺薄,...
    _行走中的蝸牛_閱讀 239評論 0 0
  • 有些東西掉了就不要再撿了,人與人之間也根本就沒有誰真正離不開誰,只有誰不珍惜誰,無論是故友還是紅顏,一個(gè)轉(zhuǎn)...
    南北有路閱讀 970評論 1 6

友情鏈接更多精彩內(nèi)容