php制作驗證碼
阿新 • • 發佈:2018-11-21
gef 文件 spa span 利用 mat mage content line
php制作驗證碼
利用畫布制作驗證碼
function Vchar(){ //創建畫布 $image=imagecreatetruecolor(100,30);<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <img src="驗證碼.php" onclick="this.src=‘validate.php?‘+Math.random()" >//之所以要加這個Math.random()這個隨機函數是為了每次點擊是更換驗證碼。 //最後的效果:
//為畫布填充顏色 $bg=imagecolorallocate($image,mt_rand(210,255),mt_rand(210,255),mt_rand(210,255)); imagefill($image,0,0,$bg); //將驗證碼繪制到畫布 $str=getcode();//獲得4個隨機的字母數字組合for($i=0;$i<4;$i++){//把這四個字母繪制到畫布上 $color=imagecolorallocate($image,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200)); imagettftext($image,24,mt_rand(-40,40),18*($i+1),24,$color,realpath(FRAME_ROOT."/Common/simhei.ttf"),$str[$i]); } //繪制線條遮擋文字 for($j=0;$j<4;$j++){ $color2=imagecolorallocate($image,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200)); imageline($image,mt_rand(0,100),mt_rand(0,30),mt_rand(0,100),mt_rand(0,30),$color2); } header("Content-type:image/jpeg"); imagejpeg($image); }//隨機獲取六位數字 function getcode(){ $code="abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVEXYZ23456789"; $str=""; for($i=0;$i<4;$i++){ $str.=$code[mt_rand(0,57)]; } return $str; }
Vchar();//調用繪制驗證碼函數 //具體的利用方案:上面那段代碼是php代碼名字叫做validate.php;暫且假設下面這個a.html文件和這個validate.php在同一目錄下
</body> </html>
php制作驗證碼