相關(guān)關(guān)鍵詞
關(guān)于我們
最新文章
- PHP中opcode緩存簡(jiǎn)單用法分析
- thinkPHP控制器變量在模板中的顯示方法示例
- PHP move_uploaded_file() 函數(shù)(將上傳的文件移動(dòng)到新位置)
- dirname(__FILE__)的含義和應(yīng)用說(shuō)明
- thinkPHP5框架實(shí)現(xiàn)分頁(yè)查詢(xún)功能的方法示例
- PHP中單雙號(hào)與變量
- PHP獲得當(dāng)日零點(diǎn)時(shí)間戳的方法分析
- Laravel ORM對(duì)Model::find方法進(jìn)行緩存示例詳解
- PHP讀寫(xiě)文件高并發(fā)處理操作實(shí)例詳解
- 【CLI】利用Curl下載文件實(shí)時(shí)進(jìn)度條顯示的實(shí)現(xiàn)
php封裝的驗(yàn)證碼工具類(lèi)完整實(shí)例
本文實(shí)例講述了php封裝的驗(yàn)證碼工具類(lèi)。分享給大家供大家參考,具體如下:
<?php //驗(yàn)證碼工具類(lèi) class Captcha{ //屬性 private $width; private $height; private $fontsize; private $pixes; private $lines; private $str_len; /* * 構(gòu)造方法 * @param1 array $arr = array(),初始化屬性的關(guān)聯(lián)數(shù)組 */ public function __construct($arr = array()){ //初始化 $this->width = isset($arr['width']) ? $arr['width'] : $GLOBALS['config']['captcha']['width']; $this->height = isset($arr['height']) ? $arr['height'] : $GLOBALS['config']['captcha']['height']; $this->fontsize = isset($arr['fontsize']) ? $arr['fontsize'] : $GLOBALS['config']['captcha']['fontsize']; $this->pixes = isset($arr['pixes']) ? $arr['pixes'] : $GLOBALS['config']['captcha']['pixes']; $this->lines = isset($arr['lines']) ? $arr['lines'] : $GLOBALS['config']['captcha']['lines']; $this->str_len = isset($arr['str_len']) ? $arr['str_len'] : $GLOBALS['config']['captcha']['str_len']; } /* * 產(chǎn)生驗(yàn)證碼圖片 */ public function generate(){ //制作畫(huà)布 $img = imagecreatetruecolor($this->width,$this->height); //給定背景色 $bg_color = imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)); imagefill($img,0,0,$bg_color); //制作干擾線 $this->getLines($img); //增加干擾點(diǎn) $this->getPixels($img); //增加驗(yàn)證碼文字 $captcha = $this->getCaptcha(); //文字顏色 $str_color = imagecolorallocate($img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100)); //寫(xiě)入文字 //計(jì)算文字應(yīng)該出現(xiàn)的起始位置 $start_x = ceil($this->width/2) - 25; $start_y = ceil($this->height/2) - 8; if(imagestring($img,$this->fontsize,$start_x,$start_y,$captcha,$str_color)){ //成功:輸出驗(yàn)證碼 header('Content-type:image/png'); imagepng($img); }else{ //失敗 return false; } } /* * 獲取驗(yàn)證碼隨機(jī)字符串 * @return string $captcha,隨機(jī)驗(yàn)證碼文字 */ private function getCaptcha(){ //獲取隨機(jī)字符串 $str = implode('',array_merge(range('a','z'),range('A','Z'),range(1,9))); //隨機(jī)取 $captcha = ''; //保存隨機(jī)字符串 for($i = 0,$len = strlen($str);$i < $this->str_len;$i++){ //每次隨機(jī)取一個(gè)字符 $captcha .= $str[mt_rand(0,$len - 1)] . ' '; } //將數(shù)據(jù)保存到session $_SESSION['captcha'] = str_replace(' ','',$captcha); //返回值 return $captcha; } /* * 增加干擾點(diǎn) * @param1 resource $img */ private function getPixels($img){ //增加干擾點(diǎn) for($i = 0;$i < $this->pixes;$i++){ //分配顏色 $pixel_color = imagecolorallocate($img,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150)); //畫(huà)點(diǎn) imagesetpixel($img,mt_rand(0,$this->width),mt_rand(0,$this->height),$pixel_color); } } /* * 增加干擾線 * @param1 resource $img,要增加干擾線的圖片資源 */ private function getLines($img){ //增加干擾線 for($i = 0;$i < $this->lines;$i++){ //分配顏色 $line_color = imagecolorallocate($img,mt_rand(150,200),mt_rand(150,200),mt_rand(150,200)); //畫(huà)線 imageline($img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$line_color); } } /* * 驗(yàn)證驗(yàn)證碼 * @param1 string $captcha,用戶(hù)提交的驗(yàn)證碼 * @return bool,成功返回true,失敗返回false */ public static function checkCaptcha($captcha){ //驗(yàn)證碼不區(qū)分大小寫(xiě) return (strtolower($captcha) === strtolower($_SESSION['captcha'])); } }