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

實用 PHP 圖形驗證碼類別:ImageCode 完整實作

     編輯:PHP 相關知識

類別特色

  • 可自訂圖片寬高、驗證碼字元數
  • 支援數字、大小寫英文混合
  • 內建干擾像素與邊框
  • 自動輸出 GIF/JPEG 格式
  • 解構子自動釋放圖片資源

完整類別代碼

<?php
class ImageCode {
    private $width;      // 驗證碼圖片寬度
    private $height;     // 驗證碼圖片高度
    private $codeNum;    // 驗證碼字元個數
    private $checkCode;  // 驗證碼字串
    private $image;      // 圖片資源

    public function __construct($width=60, $height=20, $codeNum=4) {
        $this->width = $width;
        $this->height = $height;
        $this->codeNum = $codeNum;
        $this->checkCode = $this->createCheckCode();
    }

    public function getCreateImage() {
        $this->createImage();
        $this->outputText();
        $this->setDisturbColor();
        $this->outputImage();
    }

    public function getCheckCode() {
        return $this->checkCode;
    }

    private function createImage() {
        $this->image = imagecreatetruecolor($this->width, $this->height);
        $bg = imagecolorallocate($this->image, 255, 255, 255); // 白底
        $border = imagecolorallocate($this->image, 0, 0, 0);   // 黑框
        imagefilledrectangle($this->image, 0, 0, $this->width-1, $this->height-1, $bg);
        imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $border);
    }

    private function createCheckCode() {
        $code = '';
        for ($i=0; $i < $this->codeNum; $i++) {
            $type = rand(0, 2);
            switch ($type) {
                case 0: $char = chr(rand(48, 57)); break;      // 0-9
                case 1: $char = chr(rand(65, 90)); break;      // A-Z
                case 2: $char = chr(rand(97, 122)); break;     // a-z
            }
            $code .= $char;
        }
        return $code;
    }

    private function outputText() {
        for ($i=0; $i < $this->codeNum; $i++) {
            $x = ($i+1) * (($this->width-10) / $this->codeNum);
            $y = rand(2, $this->height-4);
            $color = imagecolorallocate($this->image, rand(0,150), rand(0,150), rand(0,150));
            imagechar($this->image, 5, $x, $y, $this->checkCode[$i], $color);
        }
    }

    private function setDisturbColor() {
        for ($i=0; $i <= 100; $i++) {
            $color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));
            imagesetpixel($this->image, rand(1,$this->width-2), rand(1,$this->height-2), $color);
        }
    }

    private function outputImage() {
        header('Content-Type: image/png');
        imagepng($this->image);
    }

    public function __destruct() {
        imagedestroy($this->image);
    }
}
?>

使用範例

<?php
session_start();
$captcha = new ImageCode(80, 30, 5);
$_SESSION['captcha_code'] = $captcha->getCheckCode();
$captcha->getCreateImage();
?>

驗證時比對 $_SESSION['captcha_code'] 與用戶輸入(建議轉大小寫不敏感比對)。

擴充建議

  • 加入背景圖片或漸層增加難度
  • 使用 TTF 字體(imagettftext)提升視覺效果
  • 加入扭曲、旋轉等干擾效果
  • 儲存驗證碼於 Session 時加入時間戳,限制有效期
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved