拓展
我們需要開啟gd拓展,可以使用下面的代碼來查看是否開啟gd拓展。
<?phpecho "Hello World!!!!";echo phpinfo();?>
然後在瀏覽器上Ctrl+F尋找gd選項即可驗證自己有沒有裝這個拓展,如果沒有的話,還需要自己全裝一下這個拓展。
背景圖
imagecreatetruecolor
預設產生黑色背景
<?php// 使用gd的imagecreatetruecolor();建立一張背景圖$image = imagecreatetruecolor(100,30);// 在顯示這張圖片的時候一定要先聲明頭資訊header('content-type:image/png');imagepng($image);// 釋放資源,銷毀執行對象imagedestroy($image);
imagecolorallocate
建立一個填充色,並用imagefill(image,x,y,color)方法來附著。
<?php// 使用gd的imagecreatetruecolor();建立一張背景圖$image = imagecreatetruecolor(100,30);// 產生填充色$bgcolor = imagecolorallocate($image,255,255,255);// 將填充色填充到背景圖上imagefill($image,0,0,$bgcolor);// 在顯示這張圖片的時候一定要先聲明頭資訊header('content-type:image/png');imagepng($image);// 釋放資源,銷毀執行對象imagedestroy($image);
imagepng
在使用這個方法之前,一定要先設定頭資訊,否則不會正常的顯示圖片
imagedestory(image)
適時的釋放資源會減輕對伺服器請求的壓力。
簡易數字驗證碼
imagecolorallocate
產生顏色資訊,方便待會的賦予處理。
$fontcolor=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));
imagestring
把內容資訊寫到圖片的相應位置上。
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
增加識別幹擾
//增加點// 產生一些幹擾的點,這裡是200個for($i=0;$i<200;$i++){ $pointcolor = imagecolorallocate($image,rand(50,255),rand(50,255),rand(50,255)); imagesetpixel($image,rand(0,100),rand(0,30),$pointcolor);}// 增加線// 產生一些幹擾線 這裡是5個for($i=0;$i<5;$i++){ // 設定為淺色的線,防止喧賓奪主 $linecolor = imagecolorallocate($image,rand(50,255),rand(50,255),rand(50,255)); imageline($image,rand(0,99),rand(0,29),rand(0,99),rand(0,29),$linecolor);}
數字字母混合驗證碼
<?php// 使用gd的imagecreatetruecolor();建立一張背景圖$image = imagecreatetruecolor(100,40);// 產生填充色$bgcolor = imagecolorallocate($image,255,255,255);// 將填充色填充到背景圖上imagefill($image,0,0,$bgcolor);//////// 產生隨機4位字母以及數字混合的驗證碼for($i=0;$i<4;$i++){ $fontsize = rand(6,8); $fontcolor = imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255)); // 為了避免使用者難於辨認,去掉了某些有歧義的字母和數字 $rawstr = 'abcdefghjkmnopqrstuvwxyz23456789ABCDEFGHJKLMNOPQRSTUVWXYZ'; $fontcontent = substr($rawstr,rand(0,strlen($rawstr)),1); // 避免產生的圖片重疊 $x += 20; $y = rand(10,20); imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor); }// 產生一些幹擾的點,這裡是200個for($i=0;$i<200;$i++){ $pointcolor = imagecolorallocate($image,rand(50,255),rand(50,255),rand(50,255)); imagesetpixel($image,rand(0,100),rand(0,30),$pointcolor);}// 產生一些幹擾線 這裡是4個for($i=0;$i<4;$i++){ // 設定為淺色的線,防止喧賓奪主 $linecolor = imagecolorallocate($image,rand(50,255),rand(50,255),rand(50,255)); imageline($image,rand(0,99),rand(0,29),rand(0,99),rand(0,29),$linecolor);}header('content-type:image/png');imagepng($image);// 釋放資源,銷毀執行對象imagedestroy($image);
使用驗證碼
開啟session的時機
注意: 開啟session一定要在開始的地方
驗證的原理
驗證的過程就是用戶端輸入的驗證碼和存在於session域中的驗證碼進行對比。即:
if(isset($_REQUEST['checkcode'])){ session_start(); if($_REQUEST['checkcode']==$_SESSION['checkcode']){ echo "<font color='green'>Success!</font>"; }else{ echo "<font color='red'>Failed!</font>"; } exit(); }
最佳化驗證
但是簡單的這樣驗證有一點不好的地方,那就是字母的大小寫容易出錯。所以我們要做一下轉換,將使用者輸入的數值全部變成小寫。
if(strtolower($_REQUEST['checkcode'])==$_SESSION['checkcode']){···}
小案例
產生驗證碼
<?phpsession_start();// 必須在php的最開始部分聲明,來開啟session// 使用gd的imagecreatetruecolor();建立一張背景圖$image = imagecreatetruecolor(100,40);// 產生填充色$bgcolor = imagecolorallocate($image,255,255,255);// 將填充色填充到背景圖上imagefill($image,0,0,$bgcolor);//////// 產生隨機4位字母以及數字混合的驗證碼$checkcode='';for($i=0;$i<4;$i++){ $fontsize = rand(6,8); $fontcolor = imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255)); // 為了避免使用者難於辨認,去掉了某些有歧義的字母和數字 $rawstr = 'abcdefghjkmnopqrstuvwxyz23456789'; $fontcontent = substr($rawstr,rand(0,strlen($rawstr)),1); // 拼接即將誕生的驗證碼 $checkcode.=$fontcontent; // 避免產生的圖片重疊 $x += 20; $y = rand(10,20); imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor); }// 儲存到session變數中$_SESSION['checkcode']=$checkcode;// 產生一些幹擾的點,這裡是200個for($i=0;$i<200;$i++){ $pointcolor = imagecolorallocate($image,rand(50,255),rand(50,255),rand(50,255)); imagesetpixel($image,rand(0,100),rand(0,30),$pointcolor);}// 產生一些幹擾線 這裡是4個for($i=0;$i<4;$i++){ // 設定為淺色的線,防止喧賓奪主 $linecolor = imagecolorallocate($image,rand(50,255),rand(50,255),rand(50,255)); imageline($image,rand(0,99),rand(0,29),rand(0,99),rand(0,29),$linecolor);}header('content-type:image/png');imagepng($image);// 釋放資源,銷毀執行對象imagedestroy($image);
表單驗證
<?phpheader("Content-Type:text/html;charset=utf8"); if(isset($_REQUEST['checkcode'])){ session_start(); if(strtolower($_REQUEST['checkcode'])==$_SESSION['checkcode']){ echo "<font color='green'>Success!</font>"; }else{ echo "<font color='red'>Failed!</font>"; } exit(); }?><!DOCTYPE html><html><head> <meta charset="utf-8" /> <title>驗證驗證碼資訊</title> <script> function change(){ document.getElementById("image_checkcode").src='./store.php?r='+Math.random(); } </script></head><body><form action="./form.php" method="post"><p>驗證碼圖片:</p><img id="image_checkcode" src="./store.php?r=<?php echo rand();?>" /><a href="javascript:void(0)" onclick="change()">看不清楚</a><br/>請輸入驗證碼<input type="text" name="checkcode" /><br /><p><input type="submit" value="提交" /></p></form></body></html>
總結
最後,來個總結吧。
•使用php製作驗證碼需要gd拓展的支援。
•使用imagecreatetruecolor方法產生背景色,並用imagefill填充一個由imagecolorallocate產生的顏色。
•使用imagestring來實現驗證碼和背景圖的結合
•使用imagesetpixel來添加幹擾點
•使用imageline來添加幹擾線
•使用session之前要在開頭開啟session_start()方法
•使用JavaScript來動態修改驗證碼的src,來滿足使用者“換一張”的需求。
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。