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

PHP 圖片驗證碼類庫封裝三選一:基礎版、MD5 Session 版、完整配置版

     編輯:PHP 綜合知識

版本一:基礎驗證碼類

class Image {
    private $img;
    public $width = 85, $height = 25, $codeLen = 4;
    public $codeStr = '329832983DSDSKDSLKQWEWQ2lkfDSFSDjfdsfdsjwlkfj93290KFDSKJFDSOIDSLK';
    public $bgColor = '#DCDCDC', $fontSize = 16, $font = 'font.ttf', $fontColor = '#000000';

    public function createCode() {
        $code = '';
        for ($i=0; $i<$this->codeLen; $i++) {
            $code .= $this->codeStr[mt_rand(0, strlen($this->codeStr)-1)];
        }
        return $this->code = $code;
    }

    public function getImage() {
        $img = imagecreatetruecolor($this->width, $this->height);
        $bg = imagecolorallocate($img, hexdec(substr($this->bgColor,1,2)), hexdec(substr($this->bgColor,3,2)), hexdec(substr($this->bgColor,5,2)));
        imagefill($img, 0, 0, $bg);
        $this->img = $img;
        $this->createFont();
        $this->createPix();
        $this->showCode();
    }

    private function createFont() {
        $this->createCode();
        $x = $this->width / $this->codeLen;
        for ($i=0; $i<$this->codeLen; $i++) {
            $c = imagecolorallocate($this->img, mt_rand(0,100), mt_rand(0,150), mt_rand(0,200));
            imagettftext($this->img, $this->fontSize, mt_rand(-30,30), $x*$i+mt_rand(3,6), mt_rand($this->height/1.2, $this->height), $c, $this->font, $this->code[$i]);
        }
    }

    private function createPix() {
        $c = imagecolorallocate($this->img, 0,0,0);
        for ($i=0; $i<100; $i++) imagesetpixel($this->img, mt_rand(0,$this->width), mt_rand(0,$this->height), $c);
        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), $c);
        }
    }

    public function getCode() { return strtoupper($this->code); }

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

版本二:MD5 加密存 Session 版

class Captcha {
    public function buildAndExportImage() {
        session_start();
        $code = $this->generateCode(4);
        $_SESSION['captcha'] = md5(strtoupper($code)); // 存 MD5
        $this->outputImage($code);
    }

    private function generateCode($len) {
        $chars = '23456789ABCDEFGHJKMNPQRSTUVWXYZ';
        $code = '';
        for ($i=0; $i<$len; $i++) $code .= $chars[mt_rand(0, strlen($chars)-1)];
        return $code;
    }

    private function outputImage($code) {
        $img = imagecreatetruecolor(100, 40);
        // ... 背景、干擾線、文字渲染 ...
        header("Content-Type: image/png");
        imagepng($img);
        imagedestroy($img);
    }
}

// 驗證時
if (md5(strtoupper($_POST['captcha'])) === $_SESSION['captcha']) { ... }

版本三:完整配置類(ValidationCode)

class ValidationCode {
    private $width, $height, $codeNum, $image, $disturbColorNum, $checkCode;

    public function __construct($width=80, $height=20, $codeNum=4) {
        $this->width=$width; $this->height=$height; $this->codeNum=$codeNum;
        $this->checkCode = $this->createCheckCode();
        $this->disturbColorNum = min(floor($width*$height/15), 240-$codeNum);
    }

    public function showImage($fontFace="") {
        $this->createImage();
        $this->setDisturbColor();
        $this->outputText($fontFace);
        $this->outputImage();
    }

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

    private function createImage() {
        $this->image = imagecreatetruecolor($this->width, $this->height);
        $bg = imagecolorallocate($this->image, rand(225,255), rand(225,255), rand(225,255));
        imagefill($this->image, 0, 0, $bg);
        $border = imagecolorallocate($this->image, 0,0,0);
        imagerectangle($this->image, 0,0, $this->width-1, $this->height-1, $border);
    }

    private function setDisturbColor() {
        for ($i=0; $i<$this->disturbColorNum; $i++) {
            $c = 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), $c);
        }
        for ($i=0; $i<10; $i++) {
            $c = imagecolorallocate($this->image, rand(200,255), rand(200,255), rand(200,255));
            imagearc($this->image, rand(-10,$this->width), rand(-10,$this->height), rand(30,300), rand(20,200), 55, 44, $c);
        }
    }

    private function createCheckCode() {
        $code = '23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ';
        $str = '';
        for ($i=0; $i<$this->codeNum; $i++) $str .= $code[rand(0, strlen($code)-1)];
        return $str;
    }

    private function outputText($fontFace) {
        for ($i=0; $i<$this->codeNum; $i++) {
            $c = imagecolorallocate($this->image, rand(0,128), rand(0,128), rand(0,128));
            if ($fontFace) {
                $size = rand(12,16);
                $x = floor(($this->width-8)/$this->codeNum)*$i+8;
                $y = rand($size+5, $this->height);
                imagettftext($this->image, $size, rand(-30,30), $x, $y, $c, $fontFace, $this->checkCode[$i]);
            } else {
                $size = rand(3,5);
                $x = floor($this->width/$this->codeNum)*$i+3;
                $y = rand(0, $this->height-15);
                imagechar($this->image, $size, $x, $y, $this->checkCode[$i], $c);
            }
        }
    }

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

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

使用範例

// code.php
session_start();
require 'ValidationCode.class.php';
$vc = new ValidationCode(80, 20, 4);
$vc->showImage();
$_SESSION['code'] = $vc->getCheckCode();

選型建議

  • 簡單專案、快速整合 → 版本一
  • 需安全性、防暴力破解 → 版本二(MD5 + Session)
  • 高度可配置、支援 TTF/內建字體、干擾豐富 → 版本三
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved