| 類型 | 核心函式 | 適用場景 |
|---|---|---|
| 文字水印 | `imagettftext()` / `imagestring()` | 版權聲明、用戶 ID、動態文字、批次編號 |
| 圖片水印 | `imagecopy()` / `imagecopymerge()` | Logo、簽名圖、QR Code、固定圖樣 |
<?php
/**
* 給圖片加文字水印
*
* @param string $srcPath 來源圖路徑
* @param string $text 水印文字
* @param string $fontPath TTF 字型檔絕對路徑
* @param int $fontSize 字號 (pt)
* @param array $color RGB 陣列 [R,G,B]
* @param int $x 左上角 X
* @param int $y 基線 Y (非左上角)
* @param int $angle 旋轉角度 (度)
* @param string $outputPath 輸出路徑 (null 直接輸出)
*/
function addTextWatermark(
string $srcPath,
string $text,
string $fontPath,
int $fontSize = 20,
array $color = [0,0,0],
int $x = 20,
int $y = 20,
int $angle = 0,
?string $outputPath = null
): bool {
$img = imagecreatefromstring(file_get_contents($srcPath));
if (!$img) return false;
$textColor = imagecolorallocate($img, $color[0], $color[1], $color[2]);
// imagettftext 需 FreeType 支援,字型檔必須存在
$bbox = imagettftext($img, $fontSize, $angle, $x, $y, $textColor, $fontPath, $text);
// $bbox 回傳 8 元素陣列:四個角落座標,可用於計算實際佔用區塊
return outputImage($img, $outputPath ?? $srcPath);
}
function outputImage(GdImage $img, string $path): bool
{
// 依副檔名決定輸出格式
$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
switch ($ext) {
case 'jpg': case 'jpeg': return imagejpeg($img, $path, 90);
case 'png': return imagepng($img, $path, 9);
case 'webp': return imagewebp($img, $path, 90);
case 'gif': return imagegif($img, $path);
default: return imagejpeg($img, $path, 90);
}
}
// 使用
addTextWatermark('photo.jpg', '© MySite 2024', __DIR__.'/fonts/NotoSansTC-Regular.ttf', 24, [255,255,255], 30, 50, -15, 'photo_wm.jpg');
<?php
/**
* 給圖片加圖片水印 (支援半透明 PNG)
*
* @param string $srcPath 目標圖
* @param string $wmPath 水印圖 (建議 PNG 含 Alpha)
* @param int $x 目標圖左上角 X
* @param int $y 目標圖左上角 Y
* @param int $opacity 透明度 0-100 (100=不透明),需水印圖無 Alpha 通道時才生效
* @param string $outputPath
*/
function addImageWatermark(
string $srcPath,
string $wmPath,
int $x = 10,
int $y = 10,
int $opacity = 50,
?string $outputPath = null
): bool {
$dst = imagecreatefromstring(file_get_contents($srcPath));
$src = imagecreatefromstring(file_get_contents($wmPath));
if (!$dst || !$src) return false;
// 開啟 Alpha 混合 (關鍵)
imagealphablending($dst, true);
imagesavealpha($dst, true);
$srcW = imagesx($src);
$srcH = imagesy($src);
// 兩種模式:
if ($opacity < 100 && !imageistruecolor($src)) {
// 水印圖本身無 Alpha,用 imagecopymerge 設定透明度
imagecopymerge($dst, $src, $x, $y, 0, 0, $srcW, $srcH, $opacity);
} else {
// 水印圖已含 Alpha 通道,直接 imagecopy (保留原透明度)
imagecopy($dst, $src, $x, $y, 0, 0, $srcW, $srcH);
}
return outputImage($dst, $outputPath ?? $srcPath);
}
// 使用:右下角置中、50% 透明度
$wmW = 100; $wmH = 50; // 水印尺寸
$dstW = 800; $dstH = 600; // 目標尺寸 (實際應用需動態取得)
addImageWatermark('photo.jpg', 'logo.png', $dstW - $wmW - 20, $dstH - $wmH - 20, 50, 'photo_wm.jpg');
// 目錄掃描 + 隊列
$files = glob('uploads/*.{jpg,jpeg,png}', GLOB_BRACE);
foreach ($files as $file) {
// 推送到 Redis Queue,Worker 異步處理
Redis::lpush('watermark_queue', json_encode(['file' => $file]));
}
// Worker (CLI 長期運行)
while (true) {
$job = Redis::rpop('watermark_queue');
if ($job) {
$data = json_decode($job, true);
addTextWatermark($data['file'], 'Copyright', $font, 20, [255,255,255]);
addImageWatermark($data['file'], 'logo.png');
} else {
sleep(1);
}
}
use Intervention\Image\ImageManagerStatic as Image;
Image::make('photo.jpg')
->text('© MySite', 100, 100, function($font) {
$font->file(__DIR__.'/fonts/NotoSans.ttf');
$font->size(24);
$font->color('#ffffff');
$font->align('center');
$font->valign('top');
$font->angle(-15);
})
->insert('logo.png', 'bottom-right', 20, 20, 50) // 50% 透明度
->save('photo_wm.jpg', 90);
文字水印靠 `imagettftext` + TTF 字型,圖片水印靠 `imagecopy` (含 Alpha) / `imagecopymerge` (無 Alpha 可調透明度)。掌握 **Alpha 混合開啟順序、座標系、字型路徑** 三要素,即可穩定產出專業水印圖片。批次處理請務必異步佇列,避免阻塞 Web 請求。