首頁  >  文章  >  後端開發  >  PHP驗證碼類別ValidateCode

PHP驗證碼類別ValidateCode

不言
不言原創
2018-05-04 11:43:052464瀏覽

這篇文章主要為大家詳細解析了PHP驗證碼類別ValidateCode,具有一定的參考價值,有興趣的小夥伴們可以參考一下

PHP解析驗證碼類別

1.開始

在網路上看到使用PHP寫的ValidateCode產生驗證碼類,感覺不錯,特拿來分析學習一下。

2.類別圖

 

#3.驗證碼類別部分程式碼

3.1  定義變數

  //随机因子
  private $charset = 'abcdefghjkmnprstuvwxyzABCDEFGJKMNPRSTUVWXYZ23456789';
  private $code;
  private $codeLen = 4;

  private $width = 130;
  private $heigh = 50;
  private $img;//图像

  private $font;//字体
  private $fontsize = 20;

$charset 是隨機因子,這裡是去掉了幾個不容易區分的字元,如字母"i,l,o,q ",數字"0,1"。有必要可以加入一些中文或其他字元或算式等。

$codeLen表示驗證碼長度,常見4位元。

3.2建構函數,設定驗證碼字體,產生一個真彩色圖像img

public function __construct()
  {
    $this->font = ROOT_PATH.'/font/Chowderhead.ttf';
    $this->img = imagecreatetruecolor($this->width, $this->heigh);
  }

3.3從隨機因子中隨機抽取4個字符,作為$code驗證碼.


#
//生成随机码
  private function createCode()
  {
    $_len = strlen($this->charset) - 1;
    for ($i = 0; $i < $this->codeLen; $i++) {
      $this->code .= $this->charset[mt_rand(0, $_len)];
    }
  }

3.4產生驗證碼背景色.

//生成背景
  private function createBg()
  {
$color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));
    imagefilledrectangle($this->img, 0, $this->heigh, $this->width, 0, $color);

  }

其中mt_rand(157, 255),目的是隨機取比較淺的顏色。

3.5在圖像上產生文字.

//生成文字
  private function createFont()
  {
    $_x = $this->width / $this->codeLen;
    $_y = $this->heigh / 2;
    for ($i = 0; $i < $this->codeLen; $i++) {
      $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
      imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(3, 5), $_y + mt_rand(2, 4), $color, $this->font, $this->code[$i]);
    }
  }

在圖像上產生驗證碼文字,主要考慮文字在圖像上的位置和每一個文字顏色。

控制第n個文字的x軸位置=  (影像寬度/ 驗證碼長度) * (n-1)    隨機的偏移數;  其中n = {d1....n}

控制第n個文字的y軸位置=  影像高度/ 2   隨機的偏移數;

mt_rand(0, 156) 隨機取文字色,0-156目的是取較深的顏色。

mt_rand(-30, 30) 隨機的文字旋轉。

3.6在圖像上產生線條和雪花

//生成线条,雪花
  private function createLine()
  {
    for ($i = 0; $i < 15; $i++) {
      $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
      imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->heigh), mt_rand(0, $this->width), mt_rand(0, $this->heigh), $color);
    }
    for ($i = 0; $i < 150; $i++) {
      $color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
      imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->heigh), &#39;#&#39;, $color);
    }
  }

畫線條的時候,取比較深的顏色值,而畫雪花的時候取比較淡的顏色值,目的是盡可能的不影響人眼辨識驗證碼,又能幹擾自動辨識驗證碼機制。

3.7對外產生驗證碼映像,供外部呼叫。

//对外生成
  public function doImg()
  {

    $this->createBg();   //1.创建验证码背景
    $this->createCode();  //2.生成随机码
    $this->createLine();  //3.生成线条和雪花
    $this->createFont();  //4.生成文字
    $this->outPut();    //5.输出验证码图像
  }

3.8完整程式碼:

#
img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));
    imagefilledrectangle($this->img, 0, $this->heigh, $this->width, 0, $color);

  }

  //生成文字
  private function createFont()
  {
    $_x = $this->width / $this->codeLen;
    $_y = $this->heigh / 2;
    for ($i = 0; $i < $this->codeLen; $i++) {
      $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
      imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(3, 5), $_y + mt_rand(2, 4), $color, $this->font, $this->code[$i]);
    }
  }

  //生成线条,雪花
  private function createLine()
  {
    for ($i = 0; $i < 15; $i++) {
      $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
      imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->heigh), mt_rand(0, $this->width), mt_rand(0, $this->heigh), $color);
    }
    for ($i = 0; $i < 150; $i++) {
      $color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
      imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->heigh), &#39;#&#39;, $color);
    }
  }

  //输出图像
  private function outPut()
  {
    header('Content-Type: image/png');
    imagepng($this->img);
    imagedestroy($this->img);
  }

  //对外生成
  public function doImg()
  {

    $this->createBg();   //1.创建验证码背景
    $this->createCode();  //2.生成随机码
    $this->createLine();  //3.生成线条和雪花
    $this->createFont();  //4.生成文字
    $this->outPut();    //5.输出验证码图像
  }

  //获取验证码
  public function getCode()
  {
    return strtolower($this->code);
  }

}

##4.測試

測試程式碼:

<?php
/**
 * Created by PhpStorm.
 * User: andy
 * Date: 16-12-22
 * Time: 下午1:20
 */

define(&#39;ROOT_PATH&#39;, dirname(__FILE__));
require_once ROOT_PATH.&#39;/includes/ValidateCode.class.php&#39;;

$_vc=new ValidateCode();
echo $_vc->doImg();

產生驗證碼:

#5.應用 


##

 <label>
<img src="../config/code.php" onclick="javascript:this.src=&#39;../config/code.php?tm=&#39;+Math.random();" />
</label>

上面onclick程式碼是點選驗證碼圖片,可以自動刷新驗證碼。

code.php:

<?php
/**
 * Created by PhpStorm.
 * User: andy
 * Date: 16-12-22
 * Time: 下午3:43
 */
require substr(dirname(__FILE__),0,-7).&#39;/init.inc.php&#39;;

$_vc=new ValidateCode();
echo $_vc->doImg();
$_SESSION[&#39;ValidateCode&#39;]=$_vc->getCode();

6.小結

在獨立測試過程,沒發現什麼問題;但應用到專案的時候,剛開始發現無法產生驗證碼圖片,網路找了一下,有的說是在outPut()函數中,

在header('Content-Type: image/png'); 這行程式碼前面增加了一行ob_clean()程式碼,可以解決驗證碼這塊問題。雖然此方法簡單,但這可能會造成其他緩衝資料問題,因為db_clean()功能就是要丟棄輸出緩衝區中的內容。

相關推薦:


php驗證碼類別實例分享

###php驗證碼的實例與想法分析### #########################

以上是PHP驗證碼類別ValidateCode的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn