首頁  >  文章  >  後端開發  >  PHP產生圖片驗證碼功能詳解

PHP產生圖片驗證碼功能詳解

墨辰丷
墨辰丷原創
2018-05-26 15:43:291787瀏覽

這篇文章主要介紹了PHP生成圖片驗證碼功能,結合實例形式簡單介紹了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);

運行效果如下:

以上就是本文的全部內容,希望對大家的學習有所幫助。


相關推薦:

php實作網站驗證碼功能

php封裝的驗證碼類別詳解

#php實作封裝的驗證碼類別

以上是PHP產生圖片驗證碼功能詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn