PHP面向?qū)ο蠛?jiǎn)易驗(yàn)證碼類如何實(shí)現(xiàn)?這個(gè)問題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見到的。希望通過(guò)這個(gè)問題能讓你收獲頗深。下面是小編給大家?guī)?lái)的參考內(nèi)容,讓我們一起來(lái)看看吧!
專注于為中小企業(yè)提供網(wǎng)站建設(shè)、成都網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)淶源免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了近千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
<?php class authCode { private static $instance = null; #實(shí)例對(duì)象 private $width = 120; #圖片寬度 private $height = 40; #圖片高度 private $font = 'font/elephant.ttf'; #字體文件路徑 private $fontSize = 14; #字體大小 private $strLen = 6; #字符個(gè)數(shù) private $auth_code_str = null; #驗(yàn)證碼結(jié)果 private $imgResult = null; #圖片資源 #入口文件 靜態(tài)方法調(diào)用 實(shí)例化對(duì)象 可用 對(duì)象方法調(diào)用 public static function img() { if (!(self::$instance instanceof self)) { self::$instance = new self(); } return self::$instance; } #隨機(jī)顏色 private function randomColor($img = null, $min = 0, $max = 255) { $rgb = []; for ($i = 1; $i <= 3; $i++) { $rgb[] = str_pad(rand($min, $max), 3, 0, STR_PAD_LEFT); } return imagecolorallocate($img, $rgb[0], $rgb[1], $rgb[2]); } #隨機(jī)字符串 private function randomStr($num = 4) { if ($num > 0) { $string = array_merge(range('a', 'z'), range(0, 9), range('A', 'Z'), range(0, 9)); for ($i = 1; $i <= $num; $i++) { shuffle($string); $this->auth_code_str .= array_pop($string); } } return $this; } #創(chuàng)建驗(yàn)證碼 public function createAuthCode(&$codeStr = false) { if (!$this->auth_code_str) { $this->randomStr($this->strLen); } if ($codeStr !== false && empty($codeStr)) { $codeStr = $this->auth_code_str; } else if (!empty($codeStr) && $codeStr !== false) { $this->auth_code_str = $codeStr; } $this->imgResult = imagecreatetruecolor($this->width, $this->height); $background = $this->randomColor($this->imgResult, 200); imagefilledrectangle($this->imgResult, 0, 0, $this->width, $this->height, $background); $y = ($this->height - $this->fontSize); $string = str_split($this->auth_code_str, 1); for ($i = 0; $i < count($string); $i++) { $frontColor = $this->randomColor($this->imgResult, 0, 200); imagefttext($this->imgResult, $this->fontSize, rand(0, 10), ($this->fontSize + 2) * $i + 10, $y, $frontColor, $this->font, $string[$i]); } return $this; } #生成線 public function line($line = 3) { $line = $line ?: 3; for ($i = 1; $i <= $line; $i++) { $lineColor = $this->randomColor($this->imgResult, 0, 200); imageline($this->imgResult, rand(0, $this->width / 5), rand(5, $this->height - 5), rand($this->width / 1.3, $this->width), rand(5, $this->height - 5), $lineColor); } return $this; } #噪點(diǎn) public function pixel($num = 50){ $num = $num ?: 3; for ($i = 1; $i <= $num; $i++) { $lineColor = $this->randomColor($this->imgResult, 0, 100); imagesetpixel($this->imgResult, rand(0, $this->width), rand(0, $this->height), $lineColor); } return $this; } #設(shè)置大小 public function size($width = null, $height = null) { $this->width = $width ?: 120; $this->height = $height ?: 40; return $this; } #設(shè)置字體大小 public function fontSize($fontsize = 14) { $this->fontSize = $fontsize ?: 14; return $this; } #設(shè)置字體 public function font($file = null) { if (is_null($file) === true) { $this->font = 'font/elephant.ttf'; } else { $this->font = $file; } return $this; } #設(shè)置長(zhǎng)度 public function strlen($num = null) { $this->strLen = $num ?: 6; return $this; } public function display() { ob_end_flush(); header("content-type:image/jpeg"); imagejpeg($this->imgResult, null, 100); imagedestroy($this->imgResult); exit; } } #簡(jiǎn)單調(diào)用方法 authCode::img()->createAuthCode()->display(); /* #指定字符串調(diào)用 $string = 'abc123'; authCode::img()->createAuthCode($string)->display(); #設(shè)置圖片大小、字?jǐn)?shù)、字體大小 authCode::img()->strlen(8)->size(300,100)->fontSize(30)->createAuthCode()->display(); #添加噪點(diǎn) authCode::img()->createAuthCode()->line()->pixel()->display(); */ ?>
感謝各位的閱讀!看完上述內(nèi)容,你們對(duì)PHP面向?qū)ο蠛?jiǎn)易驗(yàn)證碼類如何實(shí)現(xiàn)大概了解了嗎?希望文章內(nèi)容對(duì)大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章標(biāo)題:PHP面向?qū)ο蠛?jiǎn)易驗(yàn)證碼類如何實(shí)現(xiàn)
文章鏈接:http://muchs.cn/article38/pjjjpp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、關(guān)鍵詞優(yōu)化、App設(shè)計(jì)、自適應(yīng)網(wǎng)站、網(wǎng)站收錄、微信公眾號(hào)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)