php實(shí)現(xiàn)自動(dòng)生成驗(yàn)證碼的實(shí)例講解
現(xiàn)在驗(yàn)證碼在表單中的應(yīng)用越來越多了,但是如果用js來實(shí)現(xiàn)總覺得不太方便,因此使用php來實(shí)現(xiàn)下,在此記錄下。
當(dāng)然,我們也可以封裝成一個(gè)函數(shù),以后使用的時(shí)候也是很方便的,這里并未封裝,感興趣的小伙伴可以自己封裝下。
具體實(shí)現(xiàn)代碼:
新建一個(gè)cap_sz.php文件:
<?php session_start(); //設(shè)置session,一定要在頂部 $width = 150; //設(shè)置圖片寬為300像素 $height = 40; //設(shè)置圖片高為40像素 $image = imagecreatetruecolor($width, $height); //設(shè)置驗(yàn)證碼大小的函數(shù) $bgcolor = imagecolorallocate($image, 255, 255, 255); //驗(yàn)證碼顏色RGB為(255,255,255)#ffffff imagefill($image, 0, 0, $bgcolor); //區(qū)域填充 $cap_code = ""; for($i=0;$i<4;$i++){ $fontsize = 7; //設(shè)置字體大小 $fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120)); //數(shù)字越大,顏色越淺,這里是深顏色0-120 $fontcontent = rand(0,9); $cap_code.=$fontcontent; //.=連續(xù)定義變量 $x = ($i*150/4)+rand(5,10); $y = rand(5,10); //設(shè)置坐標(biāo) imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor); } $_SESSION['code'] = $cap_code; //存到session //設(shè)置干擾元素,設(shè)置雪花點(diǎn) for($i=0;$i<300;$i++){ $inputcolor = imagecolorallocate($image, rand(50,200), rand(20,200), rand(50,200)); //設(shè)置顏色,20-200顏色比數(shù)字淺,不干擾閱讀 imagesetpixel($image, rand(1,149), rand(1,39), $inputcolor); //畫一個(gè)單一像素的元素 } //增加干擾元素,設(shè)置橫線(先設(shè)置線的顏色,在設(shè)置橫線) for ($i=0;$i<4;$i++) { $linecolor = imagecolorallocate($image, rand(20,220), rand(20,220),rand(20,220)); //設(shè)置線的顏色 imageline($image, rand(1,149), rand(1,39), rand(1,299), rand(1,149), $linecolor); } //因?yàn)橛行g覽器,訪問的content-type會(huì)是文本型(亂碼),所以我們需要設(shè)置成圖片的格式類型 header('Content-Type:image/png'); imagepng($image); //建立png函數(shù) imagedestroy($image); //結(jié)束圖形函數(shù),消除$image
實(shí)例擴(kuò)展:
<?php $iC = new idCode(5,60,30); $iC->createPNG(); class idCode{ private $words = array('a','b', 'c','d','e','f','g','h','i','j','k','l', 'm','n','o','p','q','r','s','t','u','v', 'w','x','y','z','A','B','C','D','E','F', 'G','H','I','J','K','L','M','N','O','P', 'Q','R','S','T','U','V','W','X','Y','Z', '0','1','2','3','4','5','6','7','8','9'); private $fonts; private $count;//驗(yàn)證碼字符數(shù) private $height; private $width; private $path = '..myfolderfonts'; private $keys; //構(gòu)造函數(shù) public function __construct($count,$width,$height){ $this->count = $count; $this->getFonts(); $this->height = $height; $this->width = $width; } private function getFonts(){ $dir = dir($this->path); while(false !== ($file = $dir->read())){ if($file != '.' && $file != '..'){ $this->fonts[count($this->fonts)] = basename($file); } } $dir->close(); } private function createKeys(){ for($i = 0;$i < $this->count;$i++){ $this->keys[$i]['char'] = $this->words[rand(0,count($this->words)-1)]; //使用字體路徑標(biāo)識(shí) $this->keys[$i]['filename'] = $this->path.''.$this->fonts[rand(0,count($this->fonts)-1)]; } } public function createPNG(){ $this->createKeys(); //創(chuàng)建畫布以及顏色塊兒 $bg = imagecreatetruecolor($this->width + 10*2,$this->height + 3*2);//兩邊留10px空白,上下3px $grey = imagecolorallocate($bg,155,155,155); $blue = imagecolorallocate($bg,0x00,0x00,0xff); //填充背景 imagefill($bg,0,0,$grey); //添加字符 $pwidth = $this->width/$this->count; $x;$y; for($i = 0;$i < $this->count;$i++){ $rotation = rand(-40,40);//偏轉(zhuǎn)角度±40° $fontsize = 33; $width_txt; $height_txt; do{ $fontsize--; $bbox = imagettfbbox($fontsize,$rotation,$this->keys[$i]['filename'],$this->keys[$i]['char']); $width_txt = $bbox[2] - $bbox[0];//x 0 2 4 6,y1 3 5 7;左下,右下,右上,左上 $height_txt = $bbox[7] - $bbox[1]; }while($fontsize > 8 && ($height_txt > $this->height || $width_txt > $pwidth)); $fontcolor = imagecolorallocate($bg,rand(0,255),rand(0,255),rand(0,255)); $x = 8 + $pwidth*$i + $pwidth/2 - $width_txt/2;//x坐標(biāo)基本位置 $y = $this->height/2 - $height_txt/2; imagettftext($bg,$fontsize,$rotation,$x,$y,$fontcolor,$this->keys[$i]['filename'],$this->keys[$i]['char']); } //繪制干擾線 //根據(jù)字體酌情增加干擾線 imageline($bg,0,15,40,10,$blue); //圖像輸出頭文件 header('Content-type:image/png'); //輸出png圖像 imagepng($bg); //清除緩存資源 imagedestroy($bg); } public function checkKeys($input){ if(count($input)!=$this->count){ return 'ERROR:長(zhǎng)度不正確.'; }else{ for($i=0;$i < $this->count;$i++){ //0 o O I l 1 校準(zhǔn),根據(jù)所選擇的字體確定是否需要手動(dòng)校準(zhǔn) if($input[$i] != $this->keys[$i]['char']){ return 'SUCCESS.'; }else{ return 'ERROR:請(qǐng)輸入正確驗(yàn)證碼.'; } } } } } ?>
到此這篇關(guān)于php實(shí)現(xiàn)自動(dòng)生成驗(yàn)證碼的實(shí)例講解的文章就介紹到這了,更多相關(guān)php實(shí)現(xiàn)自動(dòng)生成驗(yàn)證碼的方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
版權(quán)聲明:
本站所有文章和圖片均來自用戶分享和網(wǎng)絡(luò)收集,文章和圖片版權(quán)歸原作者及原出處所有,僅供學(xué)習(xí)與參考,請(qǐng)勿用于商業(yè)用途,如果損害了您的權(quán)利,請(qǐng)聯(lián)系網(wǎng)站客服處理。