自寫 WaterMask 類,同時支援文字與圖片浮水印,可設定位置(9 宮格+隨機)、透明度、字體、顏色、縮圖輸出格式,針對 PNG 透明背景優化處理。
<?php
class WaterMask {
public $waterType = 1; // 0=文字, 1=圖片
public $pos = 9; // 1-9 九宮格, 0=隨機
public $transparent = 45; // 透明度 0-100
public $waterStr = 'www.example.com';
public $fontSize = 16;
public $fontColor = [255,0,255]; // RGB
public $fontFile = 'AHGBold.ttf';
public $waterImg = 'logo.png';
private $srcImg, $im, $water_im;
private $srcInfo, $waterInfo;
private $x, $y;
public function __construct($img) {
$this->srcImg = file_exists($img) ? $img : die('檔案不存在: '.$img);
}
private function loadSrc() {
$this->srcInfo = getimagesize($this->srcImg);
switch ($this->srcInfo[2]) {
case IMAGETYPE_PNG: $this->im = imagecreatefrompng($this->srcImg); break;
case IMAGETYPE_JPEG: $this->im = imagecreatefromjpeg($this->srcImg); break;
case IMAGETYPE_GIF: $this->im = imagecreatefromgif($this->srcImg); break;
default: die('不支援格式');
}
}
private function loadWaterImg() {
$this->waterInfo = getimagesize($this->waterImg);
switch ($this->waterInfo[2]) {
case IMAGETYPE_PNG: $this->water_im = imagecreatefrompng($this->waterImg); break;
case IMAGETYPE_JPEG: $this->water_im = imagecreatefromjpeg($this->waterImg); break;
case IMAGETYPE_GIF: $this->water_im = imagecreatefromgif($this->waterImg); break;
default: die('浮水印圖片格式錯誤');
}
}
private function calcPos() {
$sw = $this->srcInfo[0]; $sh = $this->srcInfo[1];
$ww = $this->waterInfo[0]; $wh = $this->waterInfo[1];
switch ($this->pos) {
case 1: $this->x=0; $this->y=0; break; // 左上
case 2: $this->x=($sw-$ww)/2; $this->y=0; break; // 上中
case 3: $this->x=$sw-$ww; $this->y=0; break; // 右上
case 4: $this->x=0; $this->y=($sh-$wh)/2; break; // 左中
case 5: $this->x=($sw-$ww)/2; $this->y=($sh-$wh)/2; break; // 正中
case 6: $this->x=$sw-$ww; $this->y=($sh-$wh)/2; break; // 右中
case 7: $this->x=0; $this->y=$sh-$wh; break; // 左下
case 8: $this->x=($sw-$ww)/2; $this->y=$sh-$wh; break; // 下中
case 9: default: $this->x=$sw-$ww; $this->y=$sh-$wh; break; // 右下
case 0: $this->x=rand(0,$sw-$ww); $this->y=rand(0,$sh-$wh); break; // 隨機
}
}
private function applyWaterImg() {
if ($sw <= $ww || $sh <= $wh) die('浮水印大於原圖');
$this->calcPos();
$cut = imagecreatetruecolor($ww, $wh);
imagecopy($cut, $this->im, 0,0, $this->x,$this->y, $ww,$wh);
imagecopy($cut, $this->water_im, 0,0, 0,0, $ww,$wh);
imagecopymerge($this->im, $cut, $this->x,$this->y, 0,0, $ww,$wh, $this->transparent);
}
private function applyWaterText() {
$box = imagettfbbox($this->fontSize, 0, $this->fontFile, $this->waterStr);
$w = abs($box[2]-$box[6]); $h = abs($box[3]-$box[7]);
$this->water_im = imagecreatetruecolor($w, $h);
imagealphablending($this->water_im, false);
imagesavealpha($this->water_im, true);
$trans = imagecolorallocatealpha($this->water_im, 255,255,255, 127);
imagefill($this->water_im, 0,0, $trans);
$color = imagecolorallocate($this->water_im, ...$this->fontColor);
imagettftext($this->water_im, $this->fontSize, 0, 0, $this->fontSize, $color, $this->fontFile, $this->waterStr);
$this->waterInfo = [0=>$w, 1=>$h];
$this->applyWaterImg(); // 復用圖片合成邏輯
}
public function output() {
$this->loadSrc();
if ($this->waterType === 0) {
$this->applyWaterText();
} else {
$this->loadWaterImg();
$this->applyWaterImg();
}
// 輸出同格式
switch ($this->srcInfo[2]) {
case IMAGETYPE_PNG: imagepng($this->im, $this->srcImg); break;
case IMAGETYPE_JPEG: imagejpeg($this->im, $this->srcImg, 100); break;
case IMAGETYPE_GIF: imagegif($this->im, $this->srcImg); break;
}
imagedestroy($this->im);
imagedestroy($this->water_im);
}
}
?>
<?php
$wm = new WaterMask('photo.jpg');
$wm->waterType = 1; // 圖片浮水印
$wm->transparent = 50; // 50% 透明
$wm->pos = 9; // 右下角
$wm->waterImg = 'logo.png';
$wm->output(); // 直接覆蓋原檔,或修改輸出路徑
?>