在 Web 開發中,圖片縮略圖生成是高頻需求。本文提供一個基於 PHP GD2 擴展的完整縮略圖類,支持等比縮放、裁剪模式、多格式(JPG/GIF/PNG)處理,開箱即用。
gd 擴展(GD2 版本)<?php
require_once 'ResizeImage.php';
// 參數:源圖路徑、目標寬、目標高、是否裁剪(1/0)、保存路徑
$resizeImage = new ResizeImage(
'uploads/photo.jpg', // 源圖
200, // 目標寬度
100, // 目標高度
1, // 1=裁剪填滿,0=等比縮放
'thumbs/photo_thumb.jpg' // 縮略圖保存路徑(可與源圖相同)
);
// 實例化即自動完成生成
?>
<?php
/**
* GD2 圖片縮略圖生成類
* 支持 JPG/GIF/PNG,提供裁剪/等比兩種模式
*/
class ResizeImage {
// 圖片屬性
private $type; // 圖片類型
private $width; // 原圖寬度
private $height; // 原圖高度
private $resizeWidth; // 目標寬度
private $resizeHeight; // 目標高度
private $cut; // 是否裁剪
private $srcImg; // 源圖路徑
private $dstImg; // 目標圖路徑
private $im; // GD 圖片資源
/**
* 構造函數:初始化並執行生成流程
* @param string $img 源圖路徑
* @param int $wid 目標寬
* @param int $hei 目標高
* @param int $c 裁剪模式 1/0
* @param string $dst 保存路徑
*/
public function __construct($img, $wid, $hei, $c, $dst) {
$this->srcImg = $img;
$this->resizeWidth = $wid;
$this->resizeHeight = $hei;
$this->cut = $c;
// 解析圖片類型(副檔名小寫)
$this->type = strtolower(substr(strrchr($this->srcImg, '.'), 1));
// 初始化 GD 資源
$this->initImage();
// 設置目標路徑
$this->setDstPath($dst);
// 獲取原圖尺寸
$this->width = imagesx($this->im);
$this->height = imagesy($this->im);
// 執行縮略圖生成
$this->generate();
// 釋放資源
imagedestroy($this->im);
}
/** 初始化 GD 圖片資源 */
private function initImage() {
switch ($this->type) {
case 'jpg':
case 'jpeg':
$this->im = imagecreatefromjpeg($this->srcImg);
break;
case 'gif':
$this->im = imagecreatefromgif($this->srcImg);
break;
case 'png':
$this->im = imagecreatefrompng($this->srcImg);
break;
default:
throw new Exception("不支持的圖片格式: {$this->type}");
}
}
/** 設置目標保存路徑 */
private function setDstPath($dstPath) {
$this->dstImg = $dstPath;
}
/** 核心生成邏輯 */
private function generate() {
$resizeRatio = $this->resizeWidth / $this->resizeHeight; // 目標比例
$srcRatio = $this->width / $this->height; // 原圖比例
if ($this->cut) {
// ===== 裁剪模式:填滿目標尺寸,超出部分裁除 =====
$newImg = imagecreatetruecolor($this->resizeWidth, $this->resizeHeight);
if ($srcRatio >= $resizeRatio) {
// 原圖較寬 → 以高度為基準縮放,寬度居中裁剪
$srcW = $this->height * $resizeRatio;
$srcX = ($this->width - $srcW) / 2;
imagecopyresampled($newImg, $this->im, 0, 0, $srcX, 0,
$this->resizeWidth, $this->resizeHeight, $srcW, $this->height);
} else {
// 原圖較高 → 以寬度為基準縮放,高度居中裁剪
$srcH = $this->width / $resizeRatio;
$srcY = ($this->height - $srcH) / 2;
imagecopyresampled($newImg, $this->im, 0, 0, 0, $srcY,
$this->resizeWidth, $this->resizeHeight, $this->width, $srcH);
}
imagejpeg($newImg, $this->dstImg);
} else {
// ===== 等比縮放模式:完整保留圖像,留白不裁剪 =====
if ($srcRatio >= $resizeRatio) {
// 以寬度為基準
$newW = $this->resizeWidth;
$newH = $this->resizeWidth / $srcRatio;
} else {
// 以高度為基準
$newW = $this->resizeHeight * $srcRatio;
$newH = $this->resizeHeight;
}
$newImg = imagecreatetruecolor($newW, $newH);
imagecopyresampled($newImg, $this->im, 0, 0, 0, 0,
$newW, $newH, $this->width, $this->height);
imagejpeg($newImg, $this->dstImg);
}
}
}
?>
imagecopyresized()。srcX/srcY)實現居中裁剪。imagedestroy() 釋放 GD 資源,防止內存洩漏。imagesavealpha + imagealphablending)這個類封裝了 GD2 縮略圖生成的核心邏輯,代碼清晰、易於維護擴展。實際項目中可根據需求增加緩存機制、水印功能等,構建完整的圖片處理流水線。