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

PHP GD 縮放 PNG 保留透明通道完整代碼

     編輯:PHP 綜合知識

問題背景

JPEG 縮放直觀,但 PNG 含透明通道 (Alpha) 時,若直接用 imagecopyresampled 會導致透明區域變黑或變白,必須顯式處理 Alpha 通道。

核心代碼

<?php
// 1. 載入源圖
$srcImg = imagecreatefrompng('./source.png');
$srcWidth = imagesx($srcImg);
$srcHeight = imagesy($srcImg);

// 2. 計算目標尺寸 (例:縮小一半)
$newWidth = round($srcWidth / 2);
$newHeight = round($srcHeight / 2);

// 3. 建立真彩色目標畫布
$newImg = imagecreatetruecolor($newWidth, $newHeight);

// 4. 關鍵:分配全透明色並填充畫布
$transparent = imagecolorallocatealpha($newImg, 0, 0, 0, 127); // 127 = 完全透明
imagefill($newImg, 0, 0, $transparent);

// 5. 關鍵:啟用 Alpha 混合模式與保存完整 Alpha 通道
imagealphablending($newImg, true);
imagesavealpha($newImg, true);

// 6. 執行重採樣縮放
imagecopyresampled($newImg, $srcImg, 0, 0, 0, 0, $newWidth, $newHeight, $srcWidth, $srcHeight);

// 7. 輸出 PNG
imagepng($newImg, './thumb.png');

// 8. 釋放資源
imagedestroy($srcImg);
imagedestroy($newImg);

關鍵函數對照表

函數作用
imagecolorallocatealpha分配含 Alpha 的顏色 (0-127,127 完全透明)
imagefill以透明色填充整個畫布
imagealphablending開啟混合模式,讓來源像素 Alpha 與目標融合
imagesavealpha輸出 PNG 時保存完整 Alpha 通道資訊 (關鍵!)

常見錯誤與修正

  • 漏掉 imagesavealpha(true):輸出 PNG 會丟失 Alpha,透明變黑。
  • 未填充透明底:預設畫布為黑色,縮放後透明區變黑。
  • 使用 imagecopyresized 而非 resampled:品質較差,建議一律用 resampled。
  • 源圖非 PNG:imagecreatefrompng 會失敗,需根據副檔名/MIME 動態選擇 createfromjpeg 等。

通用封裝函數

function resizePngKeepTransparency($srcPath, $dstPath, $maxWidth, $maxHeight) {
    $src = imagecreatefrompng($srcPath);
    $sw = imagesx($src); $sh = imagesy($src);
    $ratio = min($maxWidth/$sw, $maxHeight/$sh);
    $nw = (int)($sw * $ratio); $nh = (int)($sh * $ratio);

    $dst = imagecreatetruecolor($nw, $nh);
    $tp = imagecolorallocatealpha($dst, 0,0,0,127);
    imagefill($dst,0,0,$tp);
    imagealphablending($dst,true);
    imagesavealpha($dst,true);
    imagecopyresampled($dst,$src,0,0,0,0,$nw,$nh,$sw,$sh);
    imagepng($dst,$dstPath);
    imagedestroy($src); imagedestroy($dst);
}

結語

四行關鍵代碼 (allocatealpha、fill、alphablending、savealpha) 即可完美解決 PNG 縮放透明度丟失問題。封裝為通用函數後,專案中所有縮略圖生成均可復用,確保視覺一致性。

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