標籤:des style blog io color ar os sp for
code.php:
1 <?php 2 //驗證碼製作 3 //檔案頭... 4 header("Content-type: image/png"); 5 //建立真彩色白紙 6 $im = @imagecreatetruecolor(50, 20) or die("建立映像失敗"); 7 //擷取背景顏色 8 $background_color = imagecolorallocate($im, 255, 255, 255); 9 //填充背景顏色(這個東西類似油桶) 10 imagefill($im,0,0,$background_color); 11 //擷取邊框顏色 12 $border_color = imagecolorallocate($im,200,200,200); 13 //畫矩形,邊框顏色200,200,200 14 imagerectangle($im,0,0,49,19,$border_color); 15 16 //逐行炫耀背景,全屏用1或0 17 for($i=2;$i<18;$i++){ 18 //擷取隨機淡色 19 $line_color = imagecolorallocate($im,rand(200,255),rand(200,255),rand(200,255)); 20 //畫線 21 imageline($im,2,$i,47,$i,$line_color); 22 } 23 24 //設定字型大小 25 $font_size=12; 26 27 //設定印上去的文字 28 $Str[0] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 29 $Str[1] = "abcdefghijklmnopqrstuvwxyz"; 30 $Str[2] = "01234567891234567890123456"; 31 32 //擷取第1個隨機文字 33 $imstr[0]["s"] = $Str[rand(0,2)][rand(0,25)]; 34 $imstr[0]["x"] = rand(2,5); 35 $imstr[0]["y"] = rand(1,4); 36 37 //擷取第2個隨機文字 38 $imstr[1]["s"] = $Str[rand(0,2)][rand(0,25)]; 39 $imstr[1]["x"] = $imstr[0]["x"]+$font_size-1+rand(0,1); 40 $imstr[1]["y"] = rand(1,3); 41 42 //擷取第3個隨機文字 43 $imstr[2]["s"] = $Str[rand(0,2)][rand(0,25)]; 44 $imstr[2]["x"] = $imstr[1]["x"]+$font_size-1+rand(0,1); 45 $imstr[2]["y"] = rand(1,4); 46 47 //擷取第4個隨機文字 48 $imstr[3]["s"] = $Str[rand(0,2)][rand(0,25)]; 49 $imstr[3]["x"] = $imstr[2]["x"]+$font_size-1+rand(0,1); 50 $imstr[3]["y"] = rand(1,3); 51 52 //寫入隨機字串 53 for($i=0;$i<4;$i++){ 54 //擷取隨機較深顏色 55 $text_color = imagecolorallocate($im,rand(50,180),rand(50,180),rand(50,180)); 56 //畫文字 57 imagechar($im,$font_size,$imstr[$i]["x"],$imstr[$i]["y"],$imstr[$i]["s"],$text_color); 58 } 59 60 //顯示圖片 61 imagepng($im); 62 //銷毀圖片 63 imagedestroy($im); 64 ?>
調用的html:
1 <html> 2 <head> 3 <title>圖片驗證執行個體</title> 4 <style type="text/css"> 5 </style> 6 </head> 7 <body> 8 <form id="login" action="" method="post"> 9 <p>此例為驗證執行個體</p>10 <p>11 <span>驗證碼:</span>12 <input type="text" name="validate" value="" size=10> 13 <img title="點擊重新整理" src="code.php"></img>14 </p>15 <p>16 <input type="submit">17 </p>18 </form>
PHP 產生驗證碼