PHP簡單驗證碼類,可直接使用,替換字體路徑
<?php class authCode { private static $instance = null; #实例对象 private $width = 120; #图片宽度 private $height = 40; #图片高度 private $font = 'font/elephant.ttf'; #字体文件路径 private $fontSize = 14; #字体大小 private $strLen = 6; #字符个数 private $auth_code_str = null; #验证码结果 private $imgResult = null; #图片资源 #入口文件 静态方法调用 实例化对象 可用 对象方法调用 public static function img() { if (!(self::$instance instanceof self)) { self::$instance = new self(); } return self::$instance; } #随机颜色 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]); } #随机字符串 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; } #创建验证码 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; } #噪点 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; } #设置大小 public function size($width = null, $height = null) { $this->width = $width ?: 120; $this->height = $height ?: 40; return $this; } #设置字体大小 public function fontSize($fontsize = 14) { $this->fontSize = $fontsize ?: 14; return $this; } #设置字体 public function font($file = null) { if (is_null($file) === true) { $this->font = 'font/elephant.ttf'; } else { $this->font = $file; } return $this; } #设置长度 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; } } #简单调用方法 authCode::img()->createAuthCode()->display(); /* #指定字符串调用 $string = 'abc123'; authCode::img()->createAuthCode($string)->display(); #设置图片大小、字数、字体大小 authCode::img()->strlen(8)->size(300,100)->fontSize(30)->createAuthCode()->display(); #添加噪点 authCode::img()->createAuthCode()->line()->pixel()->display(); */ ?>
以上便是關於驗證碼類的封裝過程,可以直接使用。若有錯誤請指出。
更多相關問題請造訪PHP中文網:PHP影片教學
以上是PHP物件導向簡易驗證碼類的詳細內容。更多資訊請關注PHP中文網其他相關文章!

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

phpisusedforsendendemailsduetoitsignegrationwithservermailservicesand andexternalsmtpproviders,自動化intifications andMarketingCampaigns.1)設置設置yourphpenvenvironnvironnvironmentwithaweberswithawebserverserververandphp,確保themailfunctionisenabled.2)useabasicscruct

發送電子郵件的最佳方法是使用PHPMailer庫。 1)使用mail()函數簡單但不可靠,可能導致郵件進入垃圾郵件或無法送達。 2)PHPMailer提供更好的控制和可靠性,支持HTML郵件、附件和SMTP認證。 3)確保正確配置SMTP設置並使用加密(如STARTTLS或SSL/TLS)以增強安全性。 4)對於大量郵件,考慮使用郵件隊列系統來優化性能。

CustomHeadersheadersandAdvancedFeaturesInphpeMailenHanceFunctionalityAndreliability.1)CustomHeadersheadersheadersaddmetadatatatatataatafortrackingandCategorization.2)htmlemailsallowformattingandttinganditive.3)attachmentscanmentscanmentscanbesmentscanbestmentscanbesentscanbesentingslibrarieslibrarieslibrariesliblarikelikephpmailer.4)smtppapapairatienticationaltication enterticationallimpr

使用PHP和SMTP發送郵件可以通過PHPMailer庫實現。 1)安裝並配置PHPMailer,2)設置SMTP服務器細節,3)定義郵件內容,4)發送郵件並處理錯誤。使用此方法可以確保郵件的可靠性和安全性。

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

使用依賴注入(DI)的原因是它促進了代碼的松耦合、可測試性和可維護性。 1)使用構造函數注入依賴,2)避免使用服務定位器,3)利用依賴注入容器管理依賴,4)通過注入依賴提高測試性,5)避免過度注入依賴,6)考慮DI對性能的影響。

phpperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovessetimes.2)優化


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

WebStorm Mac版
好用的JavaScript開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能