首頁  >  文章  >  後端開發  >  PHP影像驗證碼產生函數的實作方法

PHP影像驗證碼產生函數的實作方法

WBOY
WBOY原創
2023-06-15 20:58:071269瀏覽

PHP影像驗證碼產生函數的實作方法

影像驗證碼是一種透過影像形式展示出來的安全驗證碼,能夠有效地防止各種網路攻擊,如暴力破解、自動化腳本攻擊等。 PHP作為一種流行的網頁程式語言,其內建的影像處理函數和函式庫可以方便的實作影像驗證碼功能。本文將介紹一種基於PHP的影像驗證碼產生函數實作方法。

首先,我們需要明確圖片驗證碼的要素:畫布、驗證碼字元、幹擾線、幹擾點等。接下來,我們就一一實作這些要素,以實作一個完整的PHP影像驗證碼產生函數。

  1. 建立畫布

要建立畫布,我們可以使用PHP的imagecreatetruecolor()函數。此函數會建立一個指定大小的真彩色影像。以下是該函數的呼叫格式:

resource imagecreatetruecolor ( int $width , int $height )

其中,width和height參數分別為圖片寬度和高度。以下是建立畫布的程式碼範例:

// 创建画布
$image_width = 140; // 画布宽度
$image_height = 50; // 画布高度
$image = imagecreatetruecolor($image_width, $image_height);
  1. 設定背景顏色

#要設定背景顏色,我們可以使用PHP的imagefill()函數。此函數為影像染色,使用指定顏色填充整個影像。以下是函數的呼叫格式:

bool imagefill ( resource $image , int $x , int $y , int $color )

其中,image參數為圖片資源,x和y參數為染色起始點座標,color參數為顏色,使用imagecolorallocate()函數建立。

下面是設定背景顏色的程式碼範例:

// 设置背景颜色
$bg_color = imagecolorallocate($image, 255, 255, 255); // 白色 
imagefill($image, 0, 0, $bg_color); // 填充背景为白色
  1. 新增幹擾線和乾擾點

為了提高驗證碼的安全性,我們需要加入乾擾線和乾擾點。 PHP的基本影像處理函數可以實現這些功能。

加入乾擾線,可以使用PHP的imageline()函數。此函數繪製一條直線。以下是函數的呼叫格式:

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

#其中,image參數為影像資源,(x1,y1)和(x2,y2)為線段的起點和終點座標,color參數為顏色。

加入乾擾點,可以使用PHP的imagesetpixel()函數。此函數在圖像中描繪一個像素點。以下是函數的呼叫格式:

bool imagesetpixel ( resource $image , int $x , int $y , int $color )

其中,image參數為映像資源,(x, y)為像素點的座標,color參數為顏色。

下面是新增幹擾線和乾擾點的程式碼範例:

// 添加干扰线
for ($i = 0; $i < 5; $i++) {
    $line_color = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255)); // 随机颜色
    imageline($image, rand(0, $image_width), rand(0, $image_height),
        rand(0, $image_width), rand(0, $image_height), $line_color);
}
// 添加干扰点
for ($i = 0; $i < 50; $i++) {
    $pixel_color = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255)); // 随机颜色
    imagesetpixel($image, rand(0, $image_width), rand(0, $image_height), $pixel_color);
}
  1. 新增驗證碼字元

最後一步,我們需要新增驗證碼字符。驗證碼字元可以是數字、字母或符號等。為了方便起見,我們在這裡只考慮數位驗證碼。

新增驗證碼字符,可以使用PHP的imagestring()函數或imagettftext()函數。

使用imagestring()函數時,函數直接在映像上輸出字串。以下是函數的呼叫格式:

bool imagestring ( resource $image , int $font , int $x , int $y , string $string , int $color )

#其中,image參數為圖像資源,font為字體大小,x和y為字串起始點座標,string為輸出的字串,color為顏色。

使用imagettftext()函數時,函數將指定的字串依指定字體、大小和隨意旋轉角度輸出到指定畫布上。以下是函數的呼叫格式:

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

其中,image參數為圖像資源,size為字體大小,angle為旋轉角度,(x,y)為字串起始點座標,color為顏色,fontfile為字體文件,text為輸出的字串。

下面是產生驗證碼字元的程式碼範例:

// 添加验证码字符
$code = '';
for ($i = 0; $i < 4; $i++) {
    $digital_color = imagecolorallocate($image, rand(0, 200), rand(0, 200), rand(0, 200)); // 随机颜色
    $digital = rand(0, 9); // 随机数字
    $code .= $digital;
    // 按一定的规则将字符画布输出
    imagettftext($image, 20, rand(-30, 30), $i * 30 + 10, rand($image_height/2, $image_height-20),
        $digital_color, './arial.ttf', $digital);
}

最後,將驗證碼字元儲存到session中,供後續比較驗證使用。

$_SESSION['code'] = $code;

完整程式碼範例:

session_start();

header("Content-type: image/png");

$image_width = 140; // 画布宽度
$image_height = 50; // 画布高度

$image = imagecreatetruecolor($image_width, $image_height); // 创建画布

$bg_color = imagecolorallocate($image, 255, 255, 255); // 白色 
imagefill($image, 0, 0, $bg_color); // 填充背景为白色

// 添加干扰线
for ($i = 0; $i < 5; $i++) {
    $line_color = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255)); // 随机颜色
    imageline($image, rand(0, $image_width), rand(0, $image_height),
        rand(0, $image_width), rand(0, $image_height), $line_color);
}

// 添加干扰点
for ($i = 0; $i < 50; $i++) {
    $pixel_color = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255)); // 随机颜色
    imagesetpixel($image, rand(0, $image_width), rand(0, $image_height), $pixel_color);
}

// 添加验证码字符
$code = '';
for ($i = 0; $i < 4; $i++) {
    $digital_color = imagecolorallocate($image, rand(0, 200), rand(0, 200), rand(0, 200)); // 随机颜色
    $digital = rand(0, 9); // 随机数字
    $code .= $digital;
    // 按一定的规则将字符画布输出
    imagettftext($image, 20, rand(-30, 30), $i * 30 + 10, rand($image_height/2, $image_height-20),
        $digital_color, './arial.ttf', $digital);
}

$_SESSION['code'] = $code; // 将验证码存入session

imagepng($image); // 输出验证码图片

imagedestroy($image); // 销毁图像资源

總結

PHP映像驗證碼產生函數是一種非常實用的防止網路攻擊的技術。本文介紹了PHP影像驗證碼產生函數的實作方法,具體包括建立畫布、設定背景顏色、新增幹擾線和乾擾點、新增驗證碼字元等。透過這些要素的綜合使用,可以產生高品質的、安全的影像驗證碼。

以上是PHP影像驗證碼產生函數的實作方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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