萬盛學電腦網

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

PHP GD 庫圖像處理完整指南:從基礎操作到實戰技巧

核心函數分類速查

1. 創建畫布與載入圖片

  • `imagecreatetruecolor($w, $h)`:建立真彩色畫布(推薦)
  • `imagecreatefromjpeg/png/gif/webp($file)`:從文件/URL 載入

2. 獲取屬性

  • `imagesx($im)`、`imagesy($im)`:寬/高
  • `getimagesize($file)`:返回 `[寬, 高, 類型常量, 屬性字串]`

3. 輸出與銷毀

  • `imagejpeg/png/gif/webp($im, $file?, $quality?)`
  • `imagedestroy($im)`:釋放記憶體,必須調用

進階技巧實戰

透明度處理(GIF/PNG)

<?php
// 保留 PNG 透明通道
$src = imagecreatefrompng('logo.png');
$dst = imagecreatetruecolor(200, 200);
imagealphablending($dst, false);
imagesavealpha($dst, true);
imagecopyresampled($dst, $src, 0,0,0,0, 200,200, imagesx($src), imagesy($src));
imagepng($dst, 'logo_resized.png');
imagedestroy($src); imagedestroy($dst);
?>

等比例縮放函數(支援透明)

<?php
function resizeProportional(string $srcPath, string $dstPath, int $maxW, int $maxH): bool
{
    $info = getimagesize($srcPath);
    if (!$info) return false;
    [$srcW, $srcH, $type] = $info;
    $scale = min($maxW/$srcW, $maxH/$srcH);
    $dstW = (int)($srcW * $scale);
    $dstH = (int)($srcH * $scale);

    $create = ['imagecreatefromjpeg','imagecreatefrompng','imagecreatefromgif','imagecreatefromwebp'][$type-1] ?? null;
    $output = ['imagejpeg','imagepng','imagegif','imagewebp'][$type-1] ?? null;
    if (!$create || !$output) return false;

    $src = $create($srcPath);
    $dst = imagecreatetruecolor($dstW, $dstH);
    if ($type == IMAGETYPE_PNG || $type == IMAGETYPE_GIF) {
        imagealphablending($dst, false);
        imagesavealpha($dst, true);
        $transparent = imagecolortransparent($src);
        if ($transparent >= 0) {
            $rgb = imagecolorsforindex($src, $transparent);
            $newTransparent = imagecolorallocatealpha($dst, $rgb['red'], $rgb['green'], $rgb['blue'], 127);
            imagefill($dst, 0, 0, $newTransparent);
            imagecolortransparent($dst, $newTransparent);
        }
    }
    imagecopyresampled($dst, $src, 0,0,0,0, $dstW,$dstH, $srcW,$srcH);
    $result = $output($dst, $dstPath, $type == IMAGETYPE_JPEG ? 90 : null);
    imagedestroy($src); imagedestroy($dst);
    return $result;
}
?>

水印(文字/圖片)

<?php
// 文字水印
function addTextWatermark($src, $text, $font, $size, $color, $x, $y, $dst) {
    $im = imagecreatefromjpeg($src);
    $c = imagecolorallocate($im, ...$color);
    imagettftext($im, $size, 0, $x, $y, $c, $font, $text);
    imagejpeg($im, $dst);
    imagedestroy($im);
}

// 圖片水印
function addImageWatermark($src, $watermark, $x, $y, $dst) {
    $im = imagecreatefromjpeg($src);
    $wm = imagecreatefrompng($watermark);
    imagecopy($im, $wm, $x, $y, 0, 0, imagesx($wm), imagesy($wm));
    imagejpeg($im, $dst);
    imagedestroy($im); imagedestroy($wm);
}
?>

旋轉與翻轉

  • `imagerotate($im, $angle, $bgColor)`:逆時針旋轉
  • 翻轉可用 `imagecopy` 逐行/列複製實現,或 PHP 8.0+ `imageflip($im, IMG_FLIP_HORIZONTAL|VERTICAL)`

記憶體管理關鍵

處理大圖或批量處理時,務必及時 `imagedestroy()`;若遇到「Allowed memory size exhausted」,可臨時調大 `memory_limit` 或分塊處理。

copyright © 萬盛學電腦網 all rights reserved