php驗證碼製作

來源:互聯網
上載者:User

標籤:sub   最好   ora   word   random   tracking   需要   shuf   知識庫   

有一些點需要注意:

1,建立畫布,imagecreatetruecolor()
   配置顏色,imagecolorallocate()
2,用矩形填充畫布,imagefilledrectangle()
3,把TrueType文本寫入映像,imagettftext()
4,添加幹擾點,imagesetpixel()
5, 添加幹擾線,imageline()
6, header("Content-type:image/gif");
   imagegif($image);
   imagedestroy($image);//銷毀資源
7,join()把數組轉化為字串,range()是範圍數組
8,array_merge()把很多數組合并為一個數組
9,str_shuffle()打亂字串
10,substr()截取字串
驗證碼應該分成兩部分去想,一部分是映像,一部分是文本,映像是根據gd庫中的函數來完成,畫布,顏色,
填充,幹擾線,幹擾點等,比較重要的一點是用imagettftext()把文本寫入映像,文本是通過自訂隨機
字串函數獲得,隨機函數能夠支援純數字、大小寫混合隨機產生。把驗證碼操作封裝成函數,調用即可,
調用時最好新開一個PHP檔案。

代碼如下:

image.php頁面(需要調用string.php中產生隨機字串的函數)

?php
//通過GD庫做驗證碼
function verifyImage($type=1,$length=4,$pixel=0,$line=0,$sess_name="verify"){
//建立畫布
require_once‘string.func.php‘;
$width = 80;
$height = 28;
$image = imagecreatetruecolor($width,$height);//畫布
$white = imagecolorallocate($image,255,255,255);//白色
//用矩形填充畫布
imagefilledrectangle($image,1,1,$width-2,$height-2,$white);
$chars = buildRandomString($type,$length);//調用函數產生隨機字串
$_SESSION[$sess_name] = $chars;//把隨機字串寫入session,後面驗證使用者填入的是否正確時會用到
for($i=0;$i<$length;$i++){
$size = mt_rand(14,18);//mt_rand()產生隨機數
$angle = mt_rand(-15,15);
$x = 5+$i*$size;
$y = mt_rand(20,26);
$fontfile = "../fonts/SIMYOU.TTF";
$color = imagecolorallocate($image,mt_rand(50,90),mt_rand(80,200),mt_rand(90,180));
$text = substr($chars,$i,1);
imagettftext($image,$size,$angle,$x,$y,$color,$fontfile,$text);//imagettftext()把TrueType文本寫入映像
}
for($i=0;$i<$pixel;$i++){
  $color = imagecolorallocate($image,mt_rand(50,90),mt_rand(80,200),mt_rand(90,180));
  imagesetpixel($image,mt_rand(0,$width-1),mt_rand(0,$height-1),$color);//添加點
}
for($i=0;$i<$line;$i++){
  $color = imagecolorallocate($image,mt_rand(50,90),mt_rand(80,200),mt_rand(90,180));
  imageline($image,mt_rand(0,$width-1),mt_rand(0,$height-1),mt_rand(0,$width-1),mt_rand(0,$height-1),$color);//添加直線
}
ob_clean(); //清除之前的緩衝
header("Content-type:image/gif");
imagegif($image);
imagedestroy($image);//銷毀資源
}

string.php頁面(產生隨機字串)

  

php驗證碼製作

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.