濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > Laravel下生成驗(yàn)證碼的類

Laravel下生成驗(yàn)證碼的類

熱門標(biāo)簽:隨州銷售電銷機(jī)器人公司 商丘外呼系統(tǒng)好處 百度地圖標(biāo)注類型是酒店 周口網(wǎng)絡(luò)回?fù)芡夂粝到y(tǒng) 外呼系統(tǒng)人工客服 網(wǎng)絡(luò)電話400申請(qǐng) 全國(guó)各省地圖標(biāo)注點(diǎn) 400電話申請(qǐng)辦理 福建高頻外呼防封系統(tǒng)哪家好

本文實(shí)例為大家分享了Laravel生成驗(yàn)證碼的類,供大家參考,具體內(nèi)容如下

?php
 
namespace App\Tool\Validate;
 
//驗(yàn)證碼類
class ValidateCode {
  private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//隨機(jī)因子
  private $code;//驗(yàn)證碼
  private $codelen = 4;//驗(yàn)證碼長(zhǎng)度
  private $width = 130;//寬度
  private $height = 50;//高度
  private $img;//圖形資源句柄
  private $font;//指定的字體
  private $fontsize = 20;//指定字體大小
  private $fontcolor;//指定字體顏色
 
  //構(gòu)造方法初始化
  public function __construct()
  {
    $this->font = public_path() . '/fonts/Elephant.ttf';//注意字體路徑要寫對(duì),否則顯示不了圖片
    $this->createCode();
  }
  //生成隨機(jī)碼
  private function createCode()
  {
    $_len = strlen($this->charset) - 1;
    for ($i = 0;$i  $this->codelen;++$i) {
      $this->code .= $this->charset[mt_rand(0, $_len)];
    }
  }
  //生成背景
  private function createBg()
  {
    $this->img = imagecreatetruecolor($this->width, $this->height);
    $color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));
    imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color);
  }
  //生成文字
  private function createFont()
  {
    $_x = $this->width / $this->codelen;
    for ($i = 0;$i  $this->codelen;++$i) {
      $this->fontcolor = 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(1, 5), $this->height / 1.4, $this->fontcolor, $this->font, $this->code[$i]);
    }
  }
  //生成線條、雪花
  private function createLine()
  {
    //線條
    for ($i = 0;$i  6;++$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->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);
    }
    //雪花
    for ($i = 0;$i  100;++$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->height), '*', $color);
    }
  }
  //輸出
  private function outPut()
  {
    header('Content-type:image/png');
    imagepng($this->img);
    imagedestroy($this->img);
  }
  //對(duì)外生成
  public function doimg()
  {
    $this->createBg();
    $this->createLine();
    $this->createFont();
    $this->outPut();
  }
  //獲取驗(yàn)證碼
  public function getCode()
  {
    return strtolower($this->code);
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家?! ?/p>

您可能感興趣的文章:
  • Laravel5.2使用Captcha生成驗(yàn)證碼實(shí)現(xiàn)登錄(session巨坑)
  • 用Laravel Sms實(shí)現(xiàn)laravel短信驗(yàn)證碼的發(fā)送的實(shí)現(xiàn)
  • laravel中短信發(fā)送驗(yàn)證碼的實(shí)現(xiàn)方法
  • 使用 laravel sms 構(gòu)建短信驗(yàn)證碼發(fā)送校驗(yàn)功能
  • laravel5.4生成驗(yàn)證碼的代碼
  • laravel5.4生成驗(yàn)證碼的實(shí)例講解
  • Laravel8 使用圖形驗(yàn)證碼做登錄功能的實(shí)現(xiàn)

標(biāo)簽:迪慶 海南 十堰 六安 樂(lè)山 南寧 佛山 定西

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Laravel下生成驗(yàn)證碼的類》,本文關(guān)鍵詞  Laravel,下,生成,驗(yàn)證,碼,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Laravel下生成驗(yàn)證碼的類》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Laravel下生成驗(yàn)證碼的類的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    通化市| 寻乌县| 安达市| 湘西| 新营市| 融水| 平昌县| 梨树县| 陇川县| 嵊泗县| 安康市| 淮滨县| 桑日县| 九台市| 松江区| 绥德县| 溧水县| 赤壁市| 新密市| 绥阳县| 泸溪县| 胶南市| 枣庄市| 建水县| 青川县| 钦州市| 讷河市| 静乐县| 呈贡县| 济源市| 大石桥市| 拜泉县| 葵青区| 林西县| 天津市| 德保县| 清丰县| 云南省| 大兴区| 托克托县| 闽清县|