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

基於 GD2 的 PHP 圖片縮圖類:支援等比縮放與裁切

     編輯:PHP 相關知識

前置需求

伺服器需安裝並啟用 PHP GD2 擴展(php.ini 中 extension=gd2)。

使用方式

$resizeimage = new resizeimage(
    "原圖路徑",      // 來源檔案
    200,             // 目標寬度
    100,             // 目標高度
    0,               // 是否裁切:1=裁切、0=等比縮放
    "縮圖儲存路徑"   // 目標檔案(可與來源相同)
);

完整類別程式碼

<?php
class resizeimage {
    var $type;               // 圖片類型
    var $width;              // 實際寬度
    var $height;             // 實際高度
    var $resize_width;       // 目標寬度
    var $resize_height;      // 目標高度
    var $cut;                // 是否裁切
    var $srcimg;             // 來源圖片路徑
    var $dstimg;             // 目標圖片路徑
    var $im;                 // GD 圖片資源

    function resizeimage($img, $wid, $hei, $c, $dstpath) {
        $this->srcimg = $img;
        $this->resize_width = $wid;
        $this->resize_height = $hei;
        $this->cut = $c;

        // 解析副檔名判斷類型
        $this->type = strtolower(substr(strrchr($this->srcimg, "."), 1));

        $this->initi_img();      // 初始化 GD 資源
        $this->dst_img($dstpath); // 設定目標路徑

        $this->width = imagesx($this->im);
        $this->height = imagesy($this->im);

        $this->newimg();         // 產生縮圖
        ImageDestroy($this->im); // 釋放資源
    }

    function newimg() {
        $resize_ratio = $this->resize_width / $this->resize_height;
        $ratio = $this->width / $this->height;

        if ($this->cut == "1") { // 裁切模式
            if ($ratio >= $resize_ratio) { // 高度優先
                $newimg = imagecreatetruecolor($this->resize_width, $this->resize_height);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0,
                    $this->resize_width, $this->resize_height,
                    ($this->height) * $resize_ratio, $this->height);
                ImageJpeg($newimg, $this->dstimg);
            } else { // 寬度優先
                $newimg = imagecreatetruecolor($this->resize_width, $this->resize_height);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0,
                    $this->resize_width, $this->resize_height,
                    $this->width, ($this->width) / $resize_ratio);
                ImageJpeg($newimg, $this->dstimg);
            }
        } else { // 等比縮放模式
            if ($ratio >= $resize_ratio) {
                $newimg = imagecreatetruecolor($this->resize_width, ($this->resize_width) / $ratio);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0,
                    $this->resize_width, ($this->resize_width) / $ratio,
                    $this->width, $this->height);
                ImageJpeg($newimg, $this->dstimg);
            } else {
                $newimg = imagecreatetruecolor(($this->resize_height) * $ratio, $this->resize_height);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0,
                    ($this->resize_height) * $ratio, $this->resize_height,
                    $this->width, $this->height);
                ImageJpeg($newimg, $this->dstimg);
            }
        }
    }

    function initi_img() {
        if ($this->type == "jpg") {
            $this->im = imagecreatefromjpeg($this->srcimg);
        }
        if ($this->type == "gif") {
            $this->im = imagecreatefromgif($this->srcimg);
        }
        if ($this->type == "png") {
            $this->im = imagecreatefrompng($this->srcimg);
        }
    }

    function dst_img($dstpath) {
        $full_length = strlen($this->srcimg);
        $type_length = strlen($this->type);
        $name_length = $full_length - $type_length;
        $name = substr($this->srcimg, 0, $name_length - 1);
        $this->dstimg = $dstpath;
    }
}
?>

已知限制

  • 輸出固定為 JPEG 格式,PNG 透明度會遺失(轉黑底)。
  • 程式碼風格較舊(PHP 4 風格 var 宣告),建議重構為 PHP 5+ 類別語法。

下一篇文章提供支援 PNG 透明度的改良版本。

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