本文實例講述了PHP生成圖片驗證碼功能。分享給大家供大家參考,具體如下:
只是簡單的用隨機函數實現了圖片的生成,沒有對驗證的整個流程做介紹。
代碼如下:
<?php /** * Created by JetBrains PhpStorm. * User: lee * To change this template use File | Settings | File Templates. */ header("content-type:image/png"); $validateLength=4; $strToDraw=""; $chars=[ "0","1","2","3","4", "5","6","7","8","9", "a","b","c","d","e","f","g", "h","i","j","k","l","m","n", "o","p","q","r","s","t", "u","v","w","x","y","z", "A","B","C","D","E","F","G", "H","I","J","K","L","M","N", "O","P","Q","R","S","T", "U","V","W","X","Y","Z" ]; $imgW=80; $imgH=25; $imgRes=imagecreate($imgW,$imgH); $imgColor=imagecolorallocate($imgRes,255,255,100); $color=imagecolorallocate($imgRes,0,0,0); for($i=0;$i<$validateLength;$i++){ $rand=rand(1,58); $strToDraw=$strToDraw." ".$chars[$rand]; } imagestring($imgRes,5,0,5,$strToDraw,$color); for($i=0;$i<100;$i++){ imagesetpixel($imgRes,rand(0,$imgW),rand(0,$imgH),$color); } imagepng($imgRes); imagedestroy($imgRes);
運行效果如下:
PS:這裡再為大家推薦幾款比較實用的圖片處理工具供大家參考使用:
在線圖片轉換BASE64工具:
http://tools.jb51.net/transcoding/img2base64
ICO圖標在線生成工具:
http://tools.jb51.net/aideddesign/ico_img
在線Email郵箱圖標制作工具:
http://tools.jb51.net/email/emaillogo
在線圖片格式轉換(jpg/bmp/gif/png)工具:
http://tools.jb51.net/aideddesign/picext
更多關於PHP相關內容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結》、《php面向對象程序設計入門教程》、《PHP網絡編程技巧總結》、《PHP數組(Array)操作技巧大全》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。