php生成驗證碼代碼

jopen 9年前發布 | 1K 次閱讀 PHP

<?php
session_start ();
header ( 'Content-type: image/png' );
//創建圖片
$im = imagecreate($x=130,$y=45 );
$bg = imagecolorallocate($im,rand(50,200),rand(0,155),rand(0,155)); //第一次對 imagecolorallocate() 的調用會給基于調色板的圖像填充背景色
$fontColor = imageColorAllocate ( $im, 255, 255, 255 );   //字體顏色
$fontstyle = 'rock.ttf';     //字體樣式,這個可以從c:\windows\Fonts\文件夾下找到,我把它放到和authcode.php文件同一個目錄,這里可以替換其他的字體樣式
//產生隨機字符
for($i = 0; $i < 4; $i ++) {
    $randAsciiNumArray = array (rand(48,57),rand(65,90));
    $randAsciiNum = $randAsciiNumArray [rand ( 0, 1 )];
    $randStr = chr ( $randAsciiNum );
    imagettftext($im,30,rand(0,20)-rand(0,25),5+$i*30,rand(30,35),$fontColor,$fontstyle,$randStr);
    $authcode .= $randStr;
}
$_SESSION['authcode'] = $randFourStr;//用戶和用戶輸入的驗證碼做比較

//干擾線 for ($i=0;$i<8;$i++){ $lineColor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)); imageline ($im,rand(0,$x),0,rand(0,$x),$y,$lineColor); }

//干擾點 for ($i=0;$i<250;$i++){ imagesetpixel($im,rand(0,$x),rand(0,$y),$fontColor); } imagepng($im); imagedestroy($im); ?></pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!