1. 程式人生 > >【網站開發】驗證碼製作--製作篇

【網站開發】驗證碼製作--製作篇

工程

index.html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<body>
		<input type="text" name="vcode" id="yzm"><a href="javascript:">
<span style="white-space:pre">		</span><img class="vcode" id="yzmimg" src="code.php" width="85" height="35" title="看不清?點選切換">
	</body>
</html>

WampServer  裡的php_gd2開啟


下面是製作驗證碼

第一步:隨機產生4位數

$num = "";
for($i=0;$i<4;$i++){
<span style="white-space:pre">	</span>$num = $num.rand(0,9);
}
$_SESSION["code"]=$num;<span style="white-space:pre">		</span>//儲存到session

檢測  echo $num;


第二步:繪製圖片

$img = imagecreate(85,35);

第三步:填充背景

$backg = imagecolorallocate($img,rand(1, 255),rand(1, 255),rand(1, 255));<span style="white-space:pre">	</span>//rand(1,255) 在1到255之間數字隨機
imagefill($img, 0, 0, $backg);


第四步:邊框

$border = imagecolorallocate($img,165,42,42);
imagerectangle($img, 0,0,84,34, $border);

第五步:畫字元到圖片

$font = "t1.ttf";
$fontc = imagecolorallocate($img,0,0,0);
imagettftext($img, 20, 10, rand(4, 16),rand(30, 33), $fontc, $font, $num);

第六步:畫點

$dian = imagecolorallocate($img,0,100,0);
for($i=0;$i<400;$i++){
<span style="white-space:pre">	</span>imagesetpixel($img, rand(1, 84), rand(1, 34), $dian);
}

第七步:畫線

$xian = imagecolorallocate($img,124,252,0);
for($i=0;$i<10;$i++){
	imageline($img,rand(1, 84), rand(1, 34),rand(1, 84), rand(1, 34), $xian);
}

第八步:輸送到頁面

header("Content-type: image/png");			//輸出型別
imagejpeg($img);			//輸出

第九步:銷燬圖片

imagedestroy($img);

完整程式碼:

code.php


<?php
//驗證碼
	session_start();//會話	
	
	//隨機產生4位數
	$num = "";
	for($i=0;$i<4;$i++){
		$num = $num.rand(0,9);
	}
	$_SESSION["code"]=$num;
	
	
	//建立圖片
	$img = imagecreate(85,35);
	
	//填充背景
	$backg = imagecolorallocate($img,rand(1, 255),rand(1, 255),rand(1, 255));
	imagefill($img, 0, 0, $backg);
	
	//邊框
	$border = imagecolorallocate($img,165,42,42);
	imagerectangle($img, 0,0,84,34, $border);
	
	
	//畫字元到圖片
	$font = "t1.ttf";
	$fontc = imagecolorallocate($img,0,0,0);
	imagettftext($img, 20, 10, rand(4, 16),rand(30, 33), $fontc, $font, $num);
	
	//畫點
	$dian = imagecolorallocate($img,0,100,0);
	for($i=0;$i<400;$i++){
	imagesetpixel($img, rand(1, 84), rand(1, 34), $dian);
	}
	
	//畫線
	$xian = imagecolorallocate($img,124,252,0);
	for($i=0;$i<10;$i++){
		imageline($img,rand(1, 84), rand(1, 34),rand(1, 84), rand(1, 34), $xian);
	}
	
	//輸送到客戶端
	header("Content-type: image/png");			//輸出型別
	imagejpeg($img);			//輸出
	
	//銷燬圖片
	imagedestroy($img);

方法二

<pre name="code" class="php"><?php
//驗證碼
<span style="white-space: pre;">		</span>Header("Content-type: image/PNG");
 
 
		//建立圖片
		$img = imagecreate(85,35);
		//顏色
		$b = imagecolorallocate($img, 0, 0, 0);
		$w = imagecolorallocate($img, 255, 255, 255);
<span style="white-space:pre">		</span>$s = imagecolorallocate($img, rand(1, 255),rand(1, 255),rand(1, 255));
		//填充
		imagefill($img, 0, 0, $w);
		//矩形輪廓
		imagerectangle($img, 0, 0, 84, 34, $b);
		
		//隨機數字
		$num = rand(1000, 9999);
		//放到session中
		$_SESSION["num"] = $num;
		//輸送到圖片上
		$font = $_SERVER["DOCUMENT_ROOT"]."20151228/common/AngelicWar.ttf";
		imagettftext($img, 20, 0, rand(0, 50),  rand(0,20), $<span style="font-family: Arial, Helvetica, sans-serif;">s</span><span style="font-family: Arial, Helvetica, sans-serif;">, $font, $num);</span>
	<span style="white-space:pre">	</span>//imagestring($img,4,rand(0, 50),rand(0, 20),$num,$b);
		//畫點
		for($i=0;$i<100;$i++){
			imagesetpixel($img, rand(0, 84), rand(0, 34), $b);
		}
		//畫線
		for($i=0;$i<10;$i++){
			imageline($img, rand(0, 84), rand(0, 34),rand(0, 84), rand(0, 34), $b);
		}

		//以jpeg格式繪製出來
		imagejpeg($img);
		//銷燬圖片
		imagedestroy($img);


最後效果: