里面有兩個方法,分別是用內(nèi)置字體和自定義字體生成驗證碼。
<?php /** * 驗證碼生成類 * @example * $pic = new uImage(); * $code = $pic->getVerifyCode(); * header("Content-type:image/png"); * $pic->captchaFromFont($font='RAVIE.TTF'); or $pic->captcha(); */ class uImage { /** * 驗證碼字符 * @access protected */ protected $code; /** * 生成圖片驗證碼,直接輸出的是圖片,字體大小是內(nèi)置字體,最大是5 * @access public * @param int $width 驗證碼圖片寬度 * @param int $height 驗證碼圖片的高度 * @param int $snow 背景雪花的數(shù)量 * @param int $line 干擾線的條數(shù) */ public function captcha($width=100, $height=30, $snow=80, $line=3) { $pic = imagecreatetruecolor($width, $height); $backageColor = imagecolorallocate($pic, 0xFF, 0xFF, 0xFF); imagefill($pic, 0, 0, $backageColor); //打雪花 for($i=0; $i<=$snow;$i++) { $color = imagecolorallocate($pic, mt_rand(150, 230), mt_rand(150, 230), mt_rand(150, 230)); imagechar($pic, 1, mt_rand(0, $width), mt_rand(0, $height), "*", $color); imagecolordeallocate ($pic, $color); } //畫干擾線 for($i=0; $i<=$line; $i++) { $x1 = mt_rand(2, $width * 0.2); $x2 = mt_rand($width * 0.8, $width - 2); $y1 = mt_rand(2, $height - 2); $y2 = mt_rand(2, $height - 2); $color = imagecolorallocate($pic, mt_rand(130, 250), mt_rand(130, 250), mt_rand(130, 250)); imageline($pic, $x1, $y1, $x2, $y2, $color); imagecolordeallocate ($pic, $color); } //畫字符 $code = $this->code; $eachW = $width / strlen($code); //圖片依據(jù)字符個數(shù)分配等份數(shù) $fontWidth = imagefontwidth(5); //取得字體寬度 $fontHeight = imagefontheight(5); //取得字體高度 for($i=0; $i<strlen($code);$i++) { $color = imagecolorallocate($pic, mt_rand(30, 155), mt_rand(30, 155), mt_rand(30, 150)); $x = mt_rand($eachW * $i, $eachW * ($i+1)-$fontWidth); $y = mt_rand(3, $height-$fontHeight); imagechar($pic, 5, $x, $y, $code{$i}, $color); //水平畫字符 imagecolordeallocate ($pic, $color); } //輸出 ob_start(); ob_clean(); imagepng($pic); imagedestroy($pic); } /** * 根據(jù)自定義字體生成驗證碼 * @access public * @param string $font 字符文件, TrueType 字體文件,.ttf字體 * @param int $fontWeight 字符大小 * @param int $width 圖片寬 * @param int $height 圖片高 * @param int $snow 背景雪花個數(shù) * @param int $line 干擾線條數(shù) * @param int $padding 圖片內(nèi)邊距 */ public function captchaFromFont($font, $fontWeight=16, $width=100, $height=30, $snow=80, $line=3, $padding=3){ if(!isset($font)){ return false; } $pic = imagecreatetruecolor($width, $height); $backageColor = imagecolorallocate($pic, 0xFF, 0xFF, 0xFF); imagefill($pic, 0, 0, $backageColor); imagecolordeallocate ($pic, $backageColor); //打雪花 for($i=0; $i<=$snow;$i++) { $color = imagecolorallocate($pic, mt_rand(150, 230), mt_rand(150, 230), mt_rand(150, 230)); imagechar($pic, 1, mt_rand(0, $width), mt_rand(0, $height), "*", $color); imagecolordeallocate ($pic, $color); } //畫干擾線 for($i=0; $i<=$line; $i++) { $x1 = mt_rand(2, $width * 0.2); $x2 = mt_rand($width * 0.8, $width - 2); $y1 = mt_rand(2, $height - 2); $y2 = mt_rand(2, $height - 2); $color = imagecolorallocate($pic, mt_rand(130, 250), mt_rand(130, 250), mt_rand(130, 250)); imageline($pic, $x1, $y1, $x2, $y2, $color); imagecolordeallocate ($pic, $color); } //畫字符 $code = $this->code; $eachW = $width / strlen($code); //圖片依據(jù)字符個數(shù)分配等份數(shù) $codeArray = str_split($code); for($i=0; $i<count($codeArray); $i++){ //取得字符寬高 $fontbox = imagettfbbox($fontWeight, 0, $font, $codeArray[$i]); $fontWidth = $fontbox[2] - $fontbox[0]; $fontHeight = $fontbox[1] - $fontbox[7]; $color = imagecolorallocate($pic, mt_rand(30, 155), mt_rand(30, 155), mt_rand(30, 150)); //字符顏色 $angle = mt_rand(-20, 20); //字符角度 if($i==0){ $start = $eachW * $i+$padding; $end = $eachW * ($i+1)-$fontWidth; } elseif($i == count($codeArray)){ $start = $eachW * $i; $end = $eachW * ($i+1)-$fontWidth-$padding; } else{ $start = $eachW * $i; $end = $eachW * ($i+1)-$fontWidth-$padding; } $x = $start < $end ? mt_rand($start, $end) : $start; $y = ($fontHeight+$padding) > $height ? $padding : mt_rand($fontHeight+$padding, $height-$padding); imagettftext($pic, $fontWeight, $angle, $x, $y, $color, $font, $codeArray[$i]); //用 TrueType 字體向圖像寫入文本 imagecolordeallocate ($pic, $color); } //輸出 ob_start(); ob_clean(); imagepng($pic); imagedestroy($pic); } /** * 獲取驗證碼 * @access public * @param int $len 驗證碼字符的長度 * @return strint 生成的驗證碼字符 */ public function getVerifyCode($len=4){ if(!isset($this->code)){ $this->code = $this->getCode($len); } return $this->code; } /** * 生成驗證碼 * @access protected * @param int $len 驗證碼字符的長度 * @return strint 生成的驗證碼字符 */ protected function getCode($len) { $str = "23456789abcdefghijklmnqrstuvwxyz"; $code = ""; for($i=0; $i<$len; $i++) { $code .= $str{mt_rand(0, strlen($str)-1)}; } $this->code = $code; return $code; } }
?以上就是PHP驗證碼類的內(nèi)容,更多相關(guān)內(nèi)容請關(guān)注PHP中文網(wǎng)(miracleart.cn)!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

TosettherighttimezoneinPHP,usedate_default_timezone_set()functionatthestartofyourscriptwithavalididentifiersuchas'America/New_York'.1.Usedate_default_timezone_set()beforeanydate/timefunctions.2.Alternatively,configurethephp.inifilebysettingdate.timez

TovalidateuserinputinPHP,usebuilt-invalidationfunctionslikefilter_var()andfilter_input(),applyregularexpressionsforcustomformatssuchasusernamesorphonenumbers,checkdatatypesfornumericvalueslikeageorprice,setlengthlimitsandtrimwhitespacetopreventlayout

ThePhpfunctionSerialize () andunserialize () AreusedtoconvertcomplexdaTastructdestoresintostoraSandaBackagain.1.Serialize () c OnvertsdatalikecarraysorobjectsraystringcontainingTypeandstructureinformation.2.unserialize () Reconstruct theoriginalatataprom

You can embed PHP code into HTML files, but make sure that the file has an extension of .php so that the server can parse it correctly. Use standard tags to wrap PHP code, insert dynamic content anywhere in HTML. In addition, you can switch PHP and HTML multiple times in the same file to realize dynamic functions such as conditional rendering. Be sure to pay attention to the server configuration and syntax correctness to avoid problems caused by short labels, quotation mark errors or omitted end labels.

The key to writing clean and easy-to-maintain PHP code lies in clear naming, following standards, reasonable structure, making good use of comments and testability. 1. Use clear variables, functions and class names, such as $userData and calculateTotalPrice(); 2. Follow the PSR-12 standard unified code style; 3. Split the code structure according to responsibilities, and organize it using MVC or Laravel-style catalogs; 4. Avoid noodles-style code and split the logic into small functions with a single responsibility; 5. Add comments at key points and write interface documents to clarify parameters, return values ??and exceptions; 6. Improve testability, adopt dependency injection, reduce global state and static methods. These practices improve code quality, collaboration efficiency and post-maintenance ease.

Yes,youcanrunSQLqueriesusingPHP,andtheprocessinvolveschoosingadatabaseextension,connectingtothedatabase,executingqueriessafely,andclosingconnectionswhendone.Todothis,firstchoosebetweenMySQLiorPDO,withPDObeingmoreflexibleduetosupportingmultipledatabas
