首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 物件導向驗證碼類別完整實作(可自訂字型、顏色、干擾)

     編輯:PHP 相關知識

PHP 物件導向驗證碼類別完整實作

將驗證碼生成邏輯封裝為可高度配置的類別,支援自訂字元集、尺寸、字型、顏色、干擾線/點、字元旋轉角度等。

類別程式碼

class Image {
    // 畫布設定
    public $width = 85;
    public $height = 25;
    public $code_len = 4;
    public $code_str = "329832983DSDSKDSLKQWEWQ2lkfDSFSDjfdsfdsjwlkfj93290KFDSKJFDSOIDSLK";
    public $bg_color = '#DCDCDC';
    public $font_size = 16;
    public $font = 'font.ttf';
    public $font_color = '#000000';

    private $img;
    public $code;

    // 產生隨機驗證碼
    public function create_code() {
        $code = '';
        for ($i = 0; $i < $this->code_len; $i++) {
            $code .= $this->code_str[mt_rand(0, strlen($this->code_str)-1)];
        }
        return $this->code = $code;
    }

    // 主流程:建立畫布 → 寫字元 → 干擾 → 輸出
    public function getImage() {
        $img = imagecreatetruecolor($this->width, $this->height);
        $bg = $this->hex2rgb($this->bg_color);
        $bg_color = imagecolorallocate($img, $bg[0], $bg[1], $bg[2]);
        imagefill($img, 0, 0, $bg_color);
        $this->img = $img;

        $this->create_font();  // 寫入驗證碼字元
        $this->create_pix();   // 干擾線/點
        $this->show_code();    // 輸出圖片
    }

    // 寫入字元(使用 TTF 字型支援旋轉)
    public function create_font() {
        $this->create_code();
        $x = $this->width / $this->code_len;
        for ($i = 0; $i < $this->code_len; $i++) {
            $color = imagecolorallocate($this->img, mt_rand(0,100), mt_rand(0,150), mt_rand(0,200));
            imagettftext($this->img, $this->font_size, mt_rand(-30, 30),
                $x*$i + mt_rand(3,6), mt_rand($this->height/1.2, $this->height),
                $color, $this->font, $this->code[$i]);
        }
    }

    // 干擾點與線
    public function create_pix() {
        $color = $this->hex2rgb($this->font_color);
        $pix_color = imagecolorallocate($this->img, $color[0], $color[1], $color[2]);
        for ($i=0; $i<100; $i++) {
            imagesetpixel($this->img, mt_rand(0,$this->width), mt_rand(0,$this->height), $pix_color);
        }
        for ($j=0; $j<4; $j++) {
            imagesetthickness($this->img, mt_rand(1,2));
            imageline($this->img, mt_rand(0,$this->width), mt_rand(0,$this->height),
                      mt_rand(0,$this->width), mt_rand(0,$this->height), $pix_color);
        }
    }

    // 取得驗證碼(大寫)供驗證用
    public function getCode() {
        return strtoupper($this->code);
    }

    // 輸出 PNG
    private function show_code() {
        header("Content-Type: image/png");
        imagepng($this->img);
        imagedestroy($this->img);
    }

    // 輔助:16進位顏色轉 RGB 陣列
    private function hex2rgb($hex) {
        return [
            hexdec(substr($hex, 1, 2)),
            hexdec(substr($hex, 3, 2)),
            hexdec(substr($hex, 5, 2))
        ];
    }
}

使用方式

// captcha.php
require 'Image.class.php';
session_start();

$captcha = new Image();
$captcha->width = 120;
$captcha->height = 40;
$captcha->code_len = 4;
$captcha->font = __DIR__ . '/fonts/Verdana.ttf';
$captcha->getImage();

$_SESSION['captcha_code'] = $captcha->getCode();
// 驗證邏輯
if (strtoupper($_POST['captcha']) !== $_SESSION['captcha_code']) {
    die('驗證碼錯誤');
}

可自訂屬性總覽

屬性預設值說明
width85畫布寬度
height25畫布高度
code_len4驗證碼字元數
code_str長字串字元池(可移除易混淆字元)
bg_color#DCDCDC背景色
font_size16字體大小
fontfont.ttfTTF 字型路徑(必須存在)
font_color#000000干擾線/點顏色基準

效果圖特徵

  • 字元隨機顏色、隨機旋轉角度(-30°~30°)、隨機 X/Y 位移
  • 100 個干擾點 + 4 條隨機粗細干擾線
  • 輸出 PNG 格式,品質佳且支援透明背景

物件導向封裝讓驗證碼生成邏輯可複用、可配置、易維護,專案中只需實例化並設定屬性即可獲得專業級驗證碼圖片。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved