萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

PHP 縮圖產生器類別:支援等比縮放、裁切、多格式輸出

功能完整的 PHP 縮圖類別,支援 JPEG/PNG/GIF 讀取、等比縮放、裁切模式、輸出至瀏覽器或儲存檔案。

類別原始碼 (CreatMiniature.php)

<?php
/**
 * 縮圖產生器
 * @author phpox
 * @date 2007-05-17
 */
class CreatMiniature {
    // 公共屬性
    var $srcFile = "";      // 來源圖檔路徑
    var $echoType = "";     // 輸出模式:link=直接輸出, file=存檔
    var $im = "";           // GD 影像資源
    var $srcW = "";         // 原圖寬
    var $srcH = "";         // 原圖高

    // 初始化並載入圖片
    function SetVar($srcFile, $echoType) {
        if (!file_exists($srcFile)) {
            die('來源圖片檔案不存在!');
        }
        $this->srcFile = $srcFile;
        $this->echoType = $echoType;

        $info = [];
        $data = @getimagesize($this->srcFile, $info);
        if (!$data) die('無法讀取圖片資訊!');

        switch ($data[2]) {
            case IMAGETYPE_GIF:
                if (!function_exists('imagecreatefromgif')) die('GD 不支援 GIF!');
                $this->im = imagecreatefromgif($this->srcFile);
                break;
            case IMAGETYPE_JPEG:
                if (!function_exists('imagecreatefromjpeg')) die('GD 不支援 JPEG!');
                $this->im = imagecreatefromjpeg($this->srcFile);
                break;
            case IMAGETYPE_PNG:
                $this->im = imagecreatefrompng($this->srcFile);
                break;
            default:
                die('不支援的圖片格式!');
        }
        $this->srcW = imagesx($this->im);
        $this->srcH = imagesy($this->im);
    }

    // 扭曲縮放 (不保持比例,強制填滿目標尺寸)
    function Distortion($toFile, $toW, $toH) {
        $cImg = $this->CreatImage($this->im, $toW, $toH, 0, 0, 0, 0, $this->srcW, $this->srcH);
        return $this->EchoImage($cImg, $toFile);
    }

    // 等比縮放 (保持比例,縮放至目標框內)
    function Proportional($toFile, $toW, $toH) {
        $scale = min($toW / $this->srcW, $toH / $this->srcH);
        $newW = (int)($this->srcW * $scale);
        $newH = (int)($this->srcH * $scale);
        $cImg = $this->CreatImage($this->im, $newW, $newH, 0, 0, 0, 0, $this->srcW, $this->srcH);
        return $this->EchoImage($cImg, $toFile);
    }

    // 裁切縮放 (保持比例填滿目標框,超出部分裁切,置中)
    function Crop($toFile, $toW, $toH) {
        $scale = max($toW / $this->srcW, $toH / $this->srcH);
        $newW = (int)($this->srcW * $scale);
        $newH = (int)($this->srcH * $scale);
        $srcX = (int)(($newW - $toW) / 2);
        $srcY = (int)(($newH - $toH) / 2);
        $cImg = $this->CreatImage($this->im, $toW, $toH, 0, 0, $srcX, $srcY, $toW, $toH);
        return $this->EchoImage($cImg, $toFile);
    }

    // 核心繪圖邏輯
    function CreatImage($srcIm, $dstW, $dstH, $dstX, $dstY, $srcX, $srcY, $srcW, $srcH) {
        $dstIm = imagecreatetruecolor($dstW, $dstH);
        // PNG/GIF 透明度處理
        if (function_exists('imagealphablending') && function_exists('imagesavealpha')) {
            imagealphablending($dstIm, false);
            imagesavealpha($dstIm, true);
            $transparent = imagecolorallocatealpha($dstIm, 255, 255, 255, 127);
            imagefilledrectangle($dstIm, 0, 0, $dstW, $dstH, $transparent);
        }
        imagecopyresampled($dstIm, $srcIm, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH);
        return $dstIm;
    }

    // 輸出圖片
    function EchoImage($im, $toFile) {
        if ($this->echoType == 'link') {
            header('Content-Type: image/jpeg');
            imagejpeg($im, null, 90);
        } else {
            imagejpeg($im, $toFile, 90);
        }
        imagedestroy($im);
        return true;
    }
}

// 使用範例
// $thumb = new CreatMiniature();
// $thumb->SetVar('photo.jpg', 'link');  // 直接輸出瀏覽器
// $thumb->Proportional('', 200, 150);   // 等比縮放至 200x150 內
// 或
// $thumb->SetVar('photo.jpg', 'file');
// $thumb->Crop('thumb.jpg', 100, 100);  // 裁切成 100x100 正方形存檔
?>

三種縮放模式比較

模式方法特性適用場景
扭曲縮放Distortion()強制填滿目標尺寸,比例失真固定尺寸佔位圖
等比縮放Proportional()保持比例,完整顯示,可能留白相簿縮圖、商品列表
裁切縮放Crop()保持比例填滿,超出裁切 (置中)頭像、封面圖、固定比例區塊

注意事項

  • 需安裝 GD 擴展並啟用 JPEG/PNG/GIF 支援
  • 大圖處理需調整 memory_limit 避免記憶體不足
  • 輸出品質參數 (第 3 參數 90) 可依需求調整 (0-100)
  • 現代專案建議使用 intervention/image (Composer 套件) 獲得更優雅的 API
copyright © 萬盛學電腦網 all rights reserved