今天寫一個生成驗證碼的程序,就是用了剛學的GD2圖形庫,下面我就爲大家分享我的步驟:
首先我們要有一個明確的計劃:
1.GD庫的知識
創建一個基於真彩的畫布
imagecreatetruecolor(int x_size,int y_size);
分配一個顏色
imagecolorallocate(resource image,int red,int green,int blue);//紅綠藍
區域填充
imagefill(resource image,int x,int y,int color);
矩形框
imagerectangle(resource image,int x1,int y1,int x2,int y2,int color);
畫一個像素點
imagesetpixel(resource image,int x,int y,int color);
畫一條線段
imageline(resource image,int x1,int y1,int x2,int y2,int color);
繪製文本內容
imagettftext(resource image,float size,float angle,int x,int y,int color,string content);
以png格式將圖像輸出到瀏覽器或文件
imagepng(resource image[,string filename]);
銷燬一個圖像
imagedestroy(resource image);
int rand([int min,int max]);//產生一個隨機數
strlen();//獲取字符串的長度
header();//設置相應頭信息
2.實現步驟
1.創建一個畫布
2.開始繪畫
3.輸出圖像
4.銷燬圖片(釋放內存)
3.具體實現步驟
整個驗證碼的實現步驟
1.先複製一個字體文件
2.定義一個函數(用於隨機獲取驗證碼內容)
3.開始繪畫,繪畫驗證碼
我們的字體:C:/window/fonts,然後選擇你喜歡的字體
直接上乾貨,代碼註釋的比較詳細:php
<?php
//header("content-type:text/html;charset=utf-8");
header("content-type:image/png");
//繪製驗證碼
$number=4;//驗證碼的長度
$str=getCode($number,0);//獲取驗證碼
$height=30;//驗證碼的高度
$width=$number*20;
//1創建畫布
$im=imagecreate($width,$height);//圖像元
//定義幾個顏色,輸出不同顏色的驗證碼
$color[]=imagecolorallocate($im,111,0,55);
$color[]=imagecolorallocate($im,0,77,0);
$color[]=imagecolorallocate($im,0,0,160);
$color[]=imagecolorallocate($im,221,111,0);
$color[]=imagecolorallocate($im,220,0,0);
//背景顏色(這就是顏色)
$bg=imagecolorallocate($im,200,200,200);
//2開始繪畫
imagefill($im,0,0,$bg);
//添加一個邊框
imagerectangle($im,0,0,$width-1,$height-1,$color[rand(0,4)]);
//隨機添加一些干擾點
for($j=0;$j<200;$j++){
$c=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im,rand(0,$width),rand(0,$height),$c);
}
//隨機添加干擾線
for($i=0;$i<5;$i++){
$c=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imageline($im,rand(0,$width),rand(0,$height),rand(0,$width),rand(0,$height),$c);
}
//繪製驗證碼
for($i=0;$i<$number;$i++){
imagettftext($im,10,20,8+(18*$i),24,$color[rand(0,4)],"msyh.ttf",$str[$i]);
}
//3輸出圖像
//設置響應頭信息,圖像相應
imagepng($im);
//4銷燬圖像資源
imagedestroy($im);
/**
定義一個隨機生成驗證碼內容的函數
@ param $m=4 驗證碼的個數
@ param $type=0 驗證碼的類型(純數字) 1(數字加小寫字母) 2(數字加大小寫字母)
**/
function getCode($m=4,$type=0){
//數字+小寫字母+大寫字母
$str="0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$t=array(9,35,strlen($str)-1);
//隨機生成驗證碼內容
$c="";
for($i=0;$i<$m;$i++){
$c.=$str[rand(0,$t[$type])];
}
return $c;
//echo strlen($str);
}
//echo getCode(6,1);
?>
HTML:
<?php
//驗證碼的使用
header("content-type:text/html;charset=utf-8");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>歡迎來到登錄界面</title>
</head>
<body>
<h2 align="center">驗證碼的使用</h2><br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<form action="" method="post">
<table align="center" >
<caption>用戶登錄</caption>
<tr>
<td><em>用戶名:</em></td>
<td><input type="text" name="username"></td>
<td></td>
</tr>
<tr>
<td><em>密碼:</em></td>
<td><input type="password" name="password"></td>
<td></td>
</tr>
<tr>
<td><em>驗證碼:</em></td>
<td><input type="text" name="yzm"></td>
<td><img src="demo.php" οnclick="this.src='demo.php?id='+Math.random()"/></td>
</tr>
<tr>
<td colspan="3"><input type="submit" name="sub" value="登錄"></td>
</tr>
</table>
</form>
</body>
</html>
結果如下:在這裏,不同的瀏覽器可能會得到不同的效果,注意觀察