日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

PHP使用GD库封装验证码类

發(fā)布時(shí)間:2025/3/14 php 62 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PHP使用GD库封装验证码类 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

調(diào)試小技巧:當(dāng)圖片無法顯示時(shí),將header函數(shù)注釋掉就可以看到報(bào)錯(cuò)信息了

字體文件放在當(dāng)前文件目錄的font文件夾中,windows的字體可以到C:\Windows\Fonts目錄下復(fù)制過來,處理好文件名即可

關(guān)于GD庫和面向?qū)ο蟮闹R(shí)點(diǎn)可以看我前面幾篇隨筆,直接上代碼了

?

<?php//驗(yàn)證碼類 namespace vendor;class CaptchaMaker{/*** @desc 制作驗(yàn)證碼* @param int $width = 450,驗(yàn)證碼圖片默認(rèn)寬度* @param int $height = 65,驗(yàn)證碼圖片默認(rèn)高度* @param int $length = 4,驗(yàn)證碼默認(rèn)字符數(shù)* @param string $fonts = '',驗(yàn)證碼字體,默認(rèn)為空(內(nèi)部使用默認(rèn)字體)* @param int $level = 2,干擾素級(jí)別(1-3),級(jí)別越高越難辨認(rèn)* @return null*/public static function getCaptcha($width = 450,$height = 65,$length = 4,$fonts = '',$level = 2){//判定字體資源if(empty($fonts)) $fonts = 'verdana.ttf';$fonts = __DIR__ . '/fonts/' . $fonts; //字體路徑//制作畫布$img = imagecreatetruecolor($width,$height);//分配背景色:隨機(jī)淺色系$bg_color = imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));imagefill($img,0,0,$bg_color);//獲取隨機(jī)字符$captcha = self::getString($length);//保存到session@session_start();$_SESSION['captcha'] = $captcha;//寫入圖片for($i = 0;$i < $length;$i++){//增加顏色$c_color = imagecolorallocate($img, mt_rand(0,60), mt_rand(0,60), mt_rand(0,60));imagettftext($img, mt_rand(15,25), mt_rand(-45,45), $width/($length+1)*($i+1), mt_rand(25,$height-25), $c_color, $fonts, $captcha[$i]);}$interferon_level = $level ? ($level >= 1 && $level <= 3 ? $level: 2) : 2;//增加干擾點(diǎn)$interferon_arr = array('*','(',')','~','^','=','-','+','#','&');for($i = 0;$i < $interferon_level * 100;$i++){$dots_color = imagecolorallocate($img,mt_rand(140,190),mt_rand(140,190),mt_rand(140,190));imagestring($img,mt_rand(1,5),mt_rand(0,$width),mt_rand(0,$height),$interferon_arr[mt_rand(0,9)],$dots_color);}//增加干擾線for($i = 0;$i < $interferon_level * 10;$i++){$line_color = imagecolorallocate($img, mt_rand(80,130), mt_rand(80,130), mt_rand(80,130));//制作線段imageline($img,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),$line_color);imagearc($img,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,360),mt_rand(0,360),$line_color);}//輸出資源header('Content-type:image/png');imagepng($img);imagedestroy($img);}//獲取隨機(jī)字符串private static function getString($length = 4){//定義變量保存數(shù)據(jù)$captcha = '';//循環(huán)隨機(jī)獲取數(shù)據(jù)for($i = 0;$i < $length;$i++){//隨機(jī)確定數(shù)字、大寫字母還是小寫字母switch(mt_rand(1,3)){case 1: //數(shù)字:49-57分別代表1-9$captcha .= chr(mt_rand(49,57)); break;case 2: //小寫字母$captcha .= chr(mt_rand(65,90));break;case 3: //大寫字母$captcha .= chr(mt_rand(97,122));break;}}//返回給調(diào)用處return $captcha;}//驗(yàn)證驗(yàn)證碼public static function checkCaptcha($captcha){//與session中存的進(jìn)行對(duì)比@session_start();return (strtolower($captcha) === strtolower($_SESSION['captcha']));} } //測(cè)試 CaptchaMaker::getCaptcha();

?

轉(zhuǎn)載于:https://www.cnblogs.com/chuanzi/p/10410708.html

與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的PHP使用GD库封装验证码类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。