PHP生成製作驗證碼的簡單例項

2022-10-03 18:24:08 字數 4273 閱讀 3195

看完就會,不會你打我,話不多說、開搞(人狠話不多)

1.0 首先先看**

phpheader("content-type:text/html;charset=utf-8");// 設定頁面的www.cppcns.com編碼風格

header("content-type:image/jpeg");// 通知瀏覽器輸出的是jpeg格式的影象

$img = imagecreatetruecolor(150,50);//建立畫布並設定大小 x軸150 y軸50

$bgcolor = imagecolorallocate($img, mt mt_rand(0,255), mt_rand(0,255));//分配背景顏色

imagefill($img, 0, 0, $bgcolor); ////把背景填充到影象

imagejpeg($img); // 輸出影象

imagedestroy($img); // 銷毀影象

?>

好,現在結合以上**,來分析分析以上用到的幾個函式:

① imagecreatetruecolor();

imagecreatetruecolor — 新建乙個真彩色影象(感覺哇,那麼長,其實仔細一看挺好記的 image/create/true/color,什麼是真彩色影象?往下看)

resource imagecreatetruecolor ( int $width , int $height )

imagecreatetruecolor() 和 imagecreate()兩個函式都能建立畫布

resource imagecreate ( int $x_size , int $y_size )

imagecreatetruecolor()建立的是一幅大小為 x和 y的黑色影象(預設為黑色[即便叫法就是真彩色影象]),如想改變背景顏色則需要用填充顏色函式 imagefill($img,0,0,$color);

imagecreate 新建乙個空白影象資源,用imagecolorallocate()新增背景色

上面兩個函式只不過是乙個功能的兩種方法

② imagecolorallocate();

imagecolorallocate — 為一幅影象分配顏色

int imagecolorallocate ( resource $image , int $red , int $green , 程式設計客棧int $blue )

顏色分別用 紅 綠 藍三色組合,這些引數是 0 到 255 的整數或者十六進製制的 0x00 到 0xff。

③ mt_rand();

mt_rand — 生成更好的隨機數

int mt_rand ( int $min , int $max )

$min 可選的、返回的最小值(預設:0)  $max 可選的、返回的最大值(預設:mt_getrandmax())

這裡就是用來讓他隨機生成背景顏色,0-255隨便取值。所以頁面沒重新整理一次畫布背景顏色就不一樣。

效果圖:

2.0 開始往裡面做干擾線,干擾點。防止驗證影象被秒識別

<?php header("content-type:text/html;charset=utf-8");// 設定頁面的編碼風格

header("content-type:image/jpeg");// 通知瀏覽器輸出的是jpeg格式的影象

$img = imagecreatetruecolor(150,50);//建立畫布並設定大小 x軸150 y軸50

$bgcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));//分配背景顏色

//新增干擾線,並迴圈3次,背景顏色隨機

for($i=0;$i<3;$i++)

//新增干擾點,並迴圈25次,背景顏色隨機

for($i=0;$i<25;$i++)

imagefill($img, 0, 0, $bgcolor); ////把背景填充到影象

imagejpeg($img); // 輸出影象

imagedestroy($img); // 銷毀影象

?>

函式分析:

① imageline();

imageline — 畫一條線段

bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )

imageline() 用 color 顏色在影象 image 中從座標 x1,y1 到 x2,y2(影象左上角為 0, 0)畫一條線段。

imageline($img, mt_rand(0,150), mt_rand(0,50), mt_rand(0,150), mt_rand(0,50), $linecolor);這裡意思就是 畫布$img 中從座標 x1,y1 到 x2,y2隨機

② imagesetpixel();

imagesetpixel— 畫乙個單一畫素

bool imagesetpixel ( resource $image , int $x , int $y , int $color )imagesetpixel() 在 image 影象中用 color 顏色在 x,y 座標(影象左上角為 0,0)上畫乙個點。

imagesetpixel($img, mt_rand(0,150), mt_rand(0,60), $dotcolor);具體含義同上 效果圖:

3.0 新增驗證字母數字

<?php header("content-type:text/html;charset=utf-8");// 設定頁面的編碼風格

header("content-type:image/jpeg");// 通知瀏覽器輸出的是jpeg格式的影象

$img = imagecreatetruecolor(150,50);//建立畫布並設定大小 x軸150 y軸50

$bgcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));//分配背景顏色

//新增干擾線,並迴圈3次,背景顏色隨機

for($i=0;$i<3;$i++)

//新增干擾點,並迴圈25次,背景顏色隨機

fbrqqeror($i=0;$i<25;$i++)

//新增需要驗證的字母或者數字

$rand_str = "qwertyuiopasdfghjklzxcvbnm1234567890";//需要使用到驗證的一些字母和數字

$str_arr = array(); //命名乙個陣列

for($i = 0;$i<4;$i++)

$x_start=150/4;//單個字元x軸位置

foreach ($str_arr as $key)

imagefill($img, 0, 0, $bgcolor); ////把背景填充到影象

imagejpeg($img); /www.cppcns.com/ 輸出影象

imagedestroy($img); // 銷毀影象

?>

函式:imagettftext();

imagettftext — 用 truetype 字型向影象寫入文字

array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )

分析下面的**:

imagettftext($img, 25, mt_rand(-15,15), $x_start, 50/2, $fontcolor, "c:/windows/fonts/verdana.ttf", $key);

$img-----------畫布

25-----------字型的尺寸。

mt_rand(-15,15)----------角度制表示的角度,0 度為從左向右讀的文字。更高數值表示逆時針旋轉。例如 90 度表示從下向上讀的文字。(就是字型角度的問題,)

$x_start----------通俗易懂的講就是字元的x軸位置

50/2----------字元的高度

$fontcolor----------字元顏色

"c:/windows/fonts/verdana.ttf"----------字元的字型樣式路徑

$key-----------遍歷出後的字元

效果:看起來還是挺可愛的。

本文標題: php生成製作驗證碼的簡單例項

本文位址:

php 驗證碼製作

直接上 function buildrandomstring type 1,length 4 else if type 2 else if type 3 if length strlen chars 隨意打亂字串 chars str shuffle chars return substr chars...

php製作驗證碼

session start 型別 type gif 的尺寸 width 40 height 16 header content type image type srand double microtime 1000000 生成字元的個數 randval randstr 4,if type gif f...

php製作驗證碼

用php製作乙個一串驗證碼很簡單,主要需要用到一下函式,到手冊上查詢下就知道了,需要注意imagettftext,需要乙個字型檔案,我在win7控制面板字型裡拷貝出來的.這個函式比imagestring 好一點是因為,可以改變字型,和字型大小,imagestring只提供內建的字型大小1,2,3,4...