許多 PHP 縮略圖類在處理 PNG 透明圖片時存在兩大缺陷:
本文提供的類完美解決上述問題,保持原圖格式與透明通道。
imagealphablending + imagesavealpha 組合<?php
/**
* PNG 透明度友好的縮略圖生成類
* Author: 十年後的盧哥哥
* Date: 2014-11-13
*/
class ResizeImage {
// 私有屬性
private $type;
private $width;
private $height;
private $resizeWidth;
private $resizeHeight;
private $cut;
private $srcImg;
private $dstImg;
private $im;
/**
* 構造函數
* @param string $imgPath 源圖路徑
* @param int $width 目標寬
* @param int $height 目標高
* @param bool $isCut 是否裁剪
* @param string $savePath 保存路徑
*/
public function __construct($imgPath, $width, $height, $isCut, $savePath) {
$this->srcImg = $imgPath;
$this->resizeWidth = $width;
$this->resizeHeight = $height;
$this->cut = $isCut;
// 解析副檔名
$this->type = strtolower(substr(strrchr($this->srcImg, '.'), 1));
// 初始化 GD 資源
$this->initImage();
// 設置保存路徑
$this->setDstPath($savePath);
// 獲取原圖尺寸
$this->width = imagesx($this->im);
$this->height = imagesy($this->im);
// 生成縮略圖
$this->generate();
// 清理資源
imagedestroy($this->im);
}
/** 生成核心邏輯 */
private function generate() {
$resizeRatio = $this->resizeWidth / $this->resizeHeight;
$srcRatio = $this->width / $this->height;
if ($this->cut) {
// ---- 裁剪模式 ----
$newImg = imagecreatetruecolor($this->resizeWidth, $this->resizeHeight);
// 【關鍵】PNG 透明度處理:創建全透明畫布
if ($this->type === 'png') {
imagealphablending($newImg, false);
$transparent = imagecolorallocatealpha($newImg, 0, 0, 0, 127);
imagefill($newImg, 0, 0, $transparent);
imagesavealpha($newImg, true);
}
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);
}
} else {
// ---- 等比縮放模式 ----
if ($srcRatio >= $resizeRatio) {
$newW = $this->resizeWidth;
$newH = $this->resizeWidth / $srcRatio;
} else {
$newW = $this->resizeHeight * $srcRatio;
$newH = $this->resizeHeight;
}
$newImg = imagecreatetruecolor($newW, $newH);
// PNG 透明度處理
if ($this->type === 'png') {
imagealphablending($newImg, false);
$transparent = imagecolorallocatealpha($newImg, 0, 0, 0, 127);
imagefill($newImg, 0, 0, $transparent);
imagesavealpha($newImg, true);
}
imagecopyresampled($newImg, $this->im, 0, 0, 0, 0,
$newW, $newH, $this->width, $this->height);
}
// ---- 按原格式輸出 ----
if ($this->type === 'png') {
imagepng($newImg, $this->dstImg);
} else {
imagejpeg($newImg, $this->dstImg);
}
}
/** 初始化 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("Unsupported format: {$this->type}");
}
}
/** 設置目標路徑 */
private function setDstPath($dstPath) {
$this->dstImg = $dstPath;
}
}
?>
<?php
require_once 'ResizeImage.php';
// 參數:源圖、目標寬、目標高、是否裁剪(true/false)、保存路徑
$resize = new ResizeImage(
'images/logo.png', // 源圖(PNG 透明)
300, // 寬
200, // 高
true, // 裁剪模式
'thumbs/logo_thumb.png' // 保持 PNG 格式
);
// 實例化即完成生成
?>
// 1. 關閉混合模式,避免與背景色混合
imagealphablending($newImg, false);
// 2. 分配全透明顏色(Alpha=127 為完全透明)
$transparent = imagecolorallocatealpha($newImg, 0, 0, 0, 127);
// 3. 填充整個畫布為透明
imagefill($newImg, 0, 0, $transparent);
// 4. 關鍵:保存完整 Alpha 通道信息
imagesavealpha($newImg, true);
這四步緩一不可少,缺一會導致透明區域變黑或出現鋸齒邊緣。
| 現象 | 原因 | 解決 |
|---|---|---|
| PNG 縮略圖背景變黑 | 未設置 imagesavealpha(true) | 添加該調用 |
| 半透明邊緣出現鋸齒 | 未關閉 imagealphablending | 設置為 false |
| 輸出變成 JPG | 統一用 imagejpeg | 按 $this->type 分發輸出函數 |
本類通過精細的 GD 透明度控制,實現了 PNG 縮略圖的無損生成。代碼結構清晰,易於集成到 Laravel、ThinkPHP、CodeIgniter 等框架,或作為獨立工具類使用。建議在生產環境中加入圖片緩存機制,避免重複生成消耗 CPU。