欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP封裝的驗(yàn)證碼工具類(lèi)定義與用法示例

 更新時(shí)間:2018年08月22日 14:27:51   作者:敲出別樣人生  
這篇文章主要介紹了PHP封裝的驗(yàn)證碼工具類(lèi)定義與用法,結(jié)合完整實(shí)例形式詳細(xì)分析了php封裝的驗(yàn)證碼工具類(lèi)相關(guān)圖片創(chuàng)建、隨機(jī)字符串、驗(yàn)證碼驗(yàn)證等功能定義與使用技巧,需要的朋友可以參考下

本文實(shí)例講述了PHP封裝的驗(yàn)證碼工具類(lèi)定義與用法。分享給大家供大家參考,具體如下:

下面分享的是我自己封裝的驗(yàn)證碼工具類(lèi),在平時(shí)的項(xiàng)目中會(huì)比較經(jīng)常用到的工具類(lèi),目前封裝的這個(gè)工具類(lèi)簡(jiǎn)易版的,如果有需要的伙伴可以拿去用,當(dāng)然我建議用之前在配置文件里增加一些選項(xiàng)信息

//驗(yàn)證碼寬度
private $width;
//驗(yàn)證碼高度
private $height;
//驗(yàn)證的個(gè)數(shù)
private $length;
//干擾點(diǎn)個(gè)數(shù)
private $dots;
//干擾點(diǎn)的類(lèi)型
private $type;
//干擾線(xiàn)個(gè)數(shù)
private $lines;
//文字
private $font;

方便在項(xiàng)目中對(duì)驗(yàn)證碼的要求進(jìn)行更改,以方便項(xiàng)目邏輯的需求,而且驗(yàn)證碼所選用的字體需要和驗(yàn)證碼工具類(lèi)放在同一目錄下,否則就要在配置文件中修改字體的路徑才能實(shí)現(xiàn)驗(yàn)證碼的顯示

<?php
//創(chuàng)建驗(yàn)證碼工具類(lèi)
class captcha {
 //驗(yàn)證碼的各種參數(shù)
 //驗(yàn)證碼寬度
 private $width;
 //驗(yàn)證碼高度
 private $height;
 //驗(yàn)證的個(gè)數(shù)
 private $length;
 //干擾點(diǎn)個(gè)數(shù)
 private $dots;
 //干擾點(diǎn)的類(lèi)型
 private $type;
 //干擾線(xiàn)個(gè)數(shù)
 private $lines;
 //文字
 private $font;
 //驗(yàn)證碼屬性的構(gòu)造方法
 public function __construct($arr = array ()) {
  //將屬性賦值
  $this->width = isset ($arr['width']) ? trim($arr['width']) : '270';
  $this->height = isset ($arr['height']) ? trim($arr['height']) : '30';
  $this->length = isset ($arr['length']) ? trim($arr['length']) : '4';
  $this->dots = isset ($arr['dots']) ? trim($arr['dots']) : '81';
  $this->type = isset ($arr['type']) ? trim($arr['type']) : '*';
  $this->lines = isset ($arr['lines']) ? trim($arr['lines']) : '5';
  $this->font = isset ($arr['font']) ? trim($arr['font']) : './cambriab.ttf';
 }
 //創(chuàng)建驗(yàn)證碼的方法
 public function captcha() {
  //創(chuàng)建畫(huà)布
  $img = imagecreatetruecolor($this->width, $this->height);
  //填充顏色
  //顏色資源
  $color = imagecolorallocate($img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
  //填充背景
  imagefill($img, 0, 0, $color);
  //添加干擾點(diǎn)
  for ($i = 0; $i < $this->dots; $i++) {
   //顏色資源
   $dots_color = imagecolorallocate($img, mt_rand(150, 200), mt_rand(150, 200), mt_rand(150, 200));
   //插入干擾點(diǎn)
   imagestring($img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), $this->type, $dots_color);
  }
  //添加干擾線(xiàn)
  for ($i = 0; $i < $this->lines; $i++) {
   //顏色資源
   $line_color = imagecolorallocate($img, mt_rand(150, 200), mt_rand(150, 200), mt_rand(150, 200));
   //插入干擾線(xiàn)
   imageline($img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $line_color);
  }
  //首先獲取驗(yàn)證碼,然后插入驗(yàn)證文字
  //文字高度
  $size = mt_rand(18, 20);
  //獲取驗(yàn)證碼
  $str = $this->captchastring();
  for ($i = 0; $i < strlen($str); $i++) {
   //顏色資源
   $str_color = imagecolorallocate($img, mt_rand(50, 150), mt_rand(50, 150), mt_rand(50, 150));
   //插入驗(yàn)證碼
   imagettftext($img, $size, mt_rand(-45, 45), $this->width / ($this->length + 2) * ($i +1), (($this->height - $size) / 2) + $size, $str_color, $this->font, $str[$i]);
  }
  //將得到的驗(yàn)證碼存入SESSION中,便于以后的驗(yàn)證碼判斷
  @ session_start();
  $_SESSION['captcha'] = $str;
  //輸出圖片
  header("content-type:image/png");
  imagepng($img);
  //清除資源
  imagedestroy($img);
 }
 //獲取隨機(jī)的驗(yàn)證內(nèi)容:A-Z,a-z,1-9
 private function captchaString() {
  //獲取四個(gè)隨機(jī)的字符串
  $str = "";
  for ($i = 0; $i < $this->length; $i++) {
   switch (mt_rand(1, 3)) {
    case 1 :
     $str .= chr(mt_rand(49, 57));
     break;
    case 2 :
     $str .= chr(mt_rand(97, 122));
     break;
    case 3 :
     $str .= chr(mt_rand(65, 90));
     break;
   }
  }
  return $str;
 }
 //判斷驗(yàn)證碼
 public static function checkCaptcha($captcha) {
  @ session_start();
  return strtoupper($captcha) === strtoupper($_SESSION['captcha']);
 }
}
//使用方法:
$img = new captcha();//這里采用默認(rèn)參數(shù)
$img->captcha();
?>

運(yùn)行結(jié)果:

注:代碼中用到的字體為cambriab.ttf可完整實(shí)例代碼點(diǎn)擊此處本站下載

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《PHP圖形與圖片操作技巧匯總》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《PHP基本語(yǔ)法入門(mén)教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《PHP數(shù)組(Array)操作技巧大全》及《php字符串(string)用法總結(jié)

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論