溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

php中封裝驗證碼類的示例分析

發(fā)布時間:2021-08-07 10:41:55 來源:億速云 閱讀:88 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細講解有關(guān)php中封裝驗證碼類的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

  1、創(chuàng)建畫布函數(shù):imagecreatetruecolor(w,h);

    說明:用于創(chuàng)建一個畫布。

    w 畫布的寬

    h 畫布的高

    此函數(shù)的返回值資源類(gd)

  2、為畫布創(chuàng)建一種顏色:imagecolorallocate(img,red,green,blue)

    說明:

    img畫布資源

    red,green,blue  是0~255的范圍

  3、為畫布添加背景色

    imagefill(img,x,y,color);

    說明:

    在 image 圖像的坐標 x,y(圖像左上角為 0, 0)

  4、畫邊框

    imagerectangle($img,x1,y1,x2,y2,color);

    說明:

    其左上角坐標為 x1, y1,右下角坐標為 x2, y2。圖像的左上角坐標為 0, 0。

  3、繪制內(nèi)容(字符)

    imagestring(img ,size,x,y,string,color);

    說明:

    img畫布

    size是字大小 1至5

    x,y是起始點

    string是所要畫的內(nèi)容

    color是顏色

  4、告訴瀏覽器圖片格式

    header("Content-type:image/png");可為image/gif等等

  5、輸出(或保存),也可以使用第2個參數(shù)實現(xiàn)保存

    imagepng(img【,filename】)

    imagejpeg(img【,filename】)

    imagegif(img【,filename】)

  6、添加干擾線,本質(zhì)就是直線

    imageline(img,x1,y1,x2,y2,color);

    說明:

      img 畫布

      x1,y1 起點

      x2,y2 終點

      color 顏色

  7、imagettftext ( img,size, angle, x, y, color, fontfile,text )

      說明:

        img 畫布

        size 字體大小,缺省單位像素

        angle 角度

        x,y 坐標點

        color 顏色

        fontfile 字體文件,必須是中文字體

        text 內(nèi)容

  特別說明:這里的color參數(shù)都是imagecolorallocate()函數(shù)創(chuàng)建的顏色

  下面是思路:

  這里最先生成畫布,之后就是為畫布添加字符串,直線,噪點,邊框,來生成驗證碼的,最后類返回的兩個公用接口是:可供外面調(diào)用的生成驗證碼的畫布和驗證碼的字符串構(gòu)成,為的是給外界輸出驗證碼畫布,以及存儲字符串,作為驗證用

  下面是代碼:

<?php
namespace captcha;
/*
*驗證碼類
*verify方法生成驗證碼字符串
*entry方法生成驗證碼
*特別提醒:這里要先用entry生成驗證碼,再用verify生成驗證碼的字符串,也就是必須先調(diào)用entry,然后才能夠調(diào)用verify生成驗證碼的字符串,原因代碼已經(jīng)說明問題了,因為驗證碼的字符串是在entry方法調(diào)用captchaImage生成的,所以必須先調(diào)用它才行
*有的地方對中文的字體要求比較高,所以,有的地方不支持中文驗證碼
*/
class Captcha{
  //配置參數(shù)
  private $config = array();
  //驗證碼
  private $verifyCode = '';
  //獲取配置文件的配置信息,給類傳參數(shù)就行,例如new Captcha($config);$config是你的配置文件信息
  public function __construct($config=array('width'=>100,'height'=>40,'length'=>4,'size'=>7,'lines'=>0,'dots'=>0,'font'=>'simfang.ttf','rectangle'=>array(255,55,122),'charset'=>true,'chinese'=>'來到新機場主航站樓建設(shè)在婚姻關(guān)系存續(xù)期間所負債務她在收到要求她償還前夫在婚姻關(guān)系存續(xù)期間所欠債務的法院傳票后要精益求精善始善終')){
    $this->config = $config;
  }
  //創(chuàng)建驗證碼
  private function captchaImage(){
    //畫布
    $img = imagecreatetruecolor($this->config['width'],$this->config['height']);
    //填充畫布顏色
    imagefill($img,0,0,imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)));
    //需要邊框則畫邊框
    if($this->config['rectangle'] && is_array($this->config['rectangle']) && count($this->config['rectangle']) == 3){
      $this->tangle($img);
    }
    $this->verifyCode = $this->code($img,$this->config['charset'],$this->config['chinese']);
    //存在則添加干擾線
    if($this->config['lines']){
      $this->codeLines($img);
    }
    //存在則添加干擾點
    if($this->config['dots']){
      $this->codeDots($img);
    }
    return $img;
  }
  private function codeLines($img){
    //繪制干擾線
    for($i=0;$i<$this->config['lines'];$i++){
      imageline($img,mt_rand(0,$this->config['width'] / 10),mt_rand(0,$this->config['height']),mt_rand($this->config['width'] * 7/ 10,$this->config['width'] * 9/ 10),mt_rand(0,$this->config['height']),imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)));
    }
  }
  private function codeDots($img){
    //添加噪點
    for($i=0;$i<$this->config['dots'];$i++){
      //噪點顏色
      $color = imagecolorallocate($img,mt_rand(0,180),mt_rand(0,180),mt_rand(0,180));
      imagestring($img,mt_rand(1,3),mt_rand(0,170),mt_rand(0,30),'*',$color);
    
    }
  }
  /*畫布邊框*/
  private function tangle($img){
    imagerectangle($img,0,0,$this->config['width']-1,$this->config['height']-1,imagecolorallocate($img,$this->config['rectangle'][0],$this->config['rectangle'][1],$this->config['rectangle'][2]));
  }
  /*生成驗證碼,默認英文,$ch為true則為中文*/
  private function code($img,$ch=false,$set=''){
    $str = "";
    //計算間隔
    $span = ceil($this->config['width']/($this->config['length']+1));
    if($ch && !empty($set)){
      //隨機產(chǎn)生字符
      $set = $this->config['chinese'];
      for($i=0;$i<$this->config['length'];$i++){
        $end = strlen($set)/3;
        $pos = mt_rand(0,$end-1);
        $str .= substr($set,$pos*3,3);
      }
      //每次繪制一個字符
      for($i=1;$i<=$this->config['length'];$i++){
        imagettftext($img,16,mt_rand(-30,60),$i*$span,$this->config['height']*3/5,imagecolorallocate($img,mt_rand(0,180),mt_rand(0,180),mt_rand(0,180)),$this->config['font'],substr($str,($i-1)*3,3));
      }
    }else{
      //隨機生成字母或者數(shù)字
      for($i=0;$i<$this->config['length'];$i++){
        switch(mt_rand(0,2)){
          case 0:
          $str .= chr(mt_rand(65,90));
          break;
        case 1:
          $str .= chr(mt_rand(97,122));
          break;
        case 2:
          $str .= chr(mt_rand(48,57));
        }
      }
      //每次繪制一個字符
      for($i=1;$i<=$this->config['length'];$i++){
        imagestring($img,$this->config['size'],$i*$span,0,$str[$i-1],imagecolorallocate($img,mt_rand(0,180),mt_rand(0,180),mt_rand(0,180)));
      }
    }
    return $str;
  }
  //獲取驗證碼
  public function verify(){
    return $this->verifyCode;
  }
  //生成驗證碼
  public function entry(){
    header("content-type:image/png");
    imagepng($this->captchaImage());
  }
}
$ob = new Captcha;
$ob->entry();

最后,為了不誤人子弟,還是再強調(diào)一遍:
這里必須先用entry生成驗證碼,再用verify生成驗證碼的字符串,也就是必須先調(diào)用entry,然后才能夠調(diào)用verify生成驗證碼的字符串,原因代碼已經(jīng)說明問題了,因為驗證碼的字符串是在entry方法的方法captchaImage中生成的,所以必須先調(diào)用它才行 有的地方對中文的字體要求比較高,所以,有的地方不支持中文驗證碼

關(guān)于“php中封裝驗證碼類的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節(jié)

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

php
AI