1. 程式人生 > 其它 >PHP做一個驗證碼效果

PHP做一個驗證碼效果

技術標籤:PHP

<?php
header('Content-type:image/jpeg');	//輸出格式
// header('Content-type:text/html');
$width=120;	//驗證碼圖形長
$height=40;	//驗證碼圖形高
$element=array('a','b','c','d','e','f','g','h','i','j','k','m','n','o','p','q','r','s','t','u','v','w','x','y','z');		//字串
$string='';		//定義變數
for ($i=0;$i<5;$i++){	//for迴圈輸出幾個字母
$string.=$element[rand(0,count($element)-1)]; } $img=imagecreatetruecolor($width, $height); //新建一個真彩色影象 $colorBg=imagecolorallocate($img, rand(200, 255), rand(200, 255), rand(200, 255)); //RGB,三原色,數值越大,顏色越淺 $colorBoder=imagecolorallocate($img, rand(200, 255), rand(200, 255), rand(200, 255)); //邊框顏色 $colorString=imagecolorallocate
($img, rand(10, 100), rand(10, 100), rand(10, 100)); imagefill($img, 0, 0, $colorBg); //區域填充顏色 imagerectangle($img, 0, 0, $width-1, $height-1, $colorBoder); //矩形邊框 for ($i=0;$i<=100;$i++){ //for迴圈輸出幾個畫素點 imagesetpixel($img, rand(0, $width-1), rand(0, $height-1), imagecolorallocate($img, rand(100, 200), rand(100, 200), rand(100, 200
))); } for ($i=0;$i<=3;$i++){ //for迴圈輸出幾條線條 imageline($img,rand(0,$width/2),rand(0, $height), rand($width/2, $width), rand(0, $height), imagecolorallocate($img, rand(100, 200), rand(100, 200), rand(100, 200))); } //imagestring($img, 5, 0, 0, 'abcd', $colorString); imagettftext($img, 14, 5, rand(30, 15), rand(20, 40), $colorString, 'font/BERNIER-REGULAR.OTF', $string); // imagettftext(影象, 字型大小, 傾斜角度, x座標, y座標, 顏色, 相對路徑/絕對路徑, 文字字串); imagejpeg($img); //輸出影象 imagedestroy($img); //釋放資源 ?>

相對路徑/絕對路徑中的字型檔案可到網上下載,或者在自己電腦C:\Windows\Fonts目錄中也有。
在這裡插入圖片描述