水印类的基本结构
一个完整的PHP图片水印类应包含以下核心功能:支持文字水印和图片水印、支持多种水印位置(如左上、右上、居中、左下、右下等)、支持透明度调整、支持水印大小自适应等,以下是类的基本框架:
class ImageWatermark {private $sourceImage; // 源图片资源private $watermarkImage; // 水印图片资源(如果是图片水印)private $watermarkText; // 水印文字(如果是文字水印)private $fontSize; // 字体大小private $fontColor; // 字体颜色private $watermarkPosition; // 水印位置private $watermarkOpacity; // 水印透明度public function __construct($sourceImagePath) {// 初始化源图片$this->sourceImage = $this->createImageResource($sourceImagePath);}// 其他方法...}
核心方法实现
创建图片资源
private function createImageResource($imagePath) {$imageInfo = getimagesize($imagePath);Switch ($imageInfo[2]) {case IMAGETYPE_JPEG:return imagecreatefromjpeg($imagePath);case IMAGETYPE_PNG:return imagecreatefrompng($imagePath);case IMAGETYPE_GIF:return imagecreatefromgif($imagePath);default:throw new Exception("Unsupported image type");}}
添加文字水印
public function setTextWatermark($text, $fontSize = 16, $color = '#000000') {$this->watermarkText = $text;$this->fontSize = $fontSize;$this->fontColor = $this->hexToRgb($color);}private function hexToRgb($hexColor) {$hexColor = ltrim($hexColor, '#');return ['r' => hexdec(substr($hexColor, 0, 2)),'g' => hexdec(substr($hexColor, 1, 2)),'b' => hexdec(substr($hexColor, 2, 2))];}
添加图片水印
public function setImageWatermark($watermarkImagePath) {$this->watermarkImage = $this->createImageResource($watermarkImagePath);}
设置水印位置和透明度
public function setPosition($position = 'bottom-right') {$this->watermarkPosition = $position;}public function setopacity($opacity = 50) {$this->watermarkOpacity = min(max($opacity, 0), 100);}
应用水印并保存
public function applyWatermark($outputPath, $quality = 90) {$sourceWidth = imagesx($this->sourceImage);$sourceHeight = imagesy($this->sourceImage);if ($this->watermarkText) {// 文字水印逻辑$fontFile = 'arial.ttf'; // 确保字体文件存在$textWidth = imagettfbbox($this->fontSize, 0, $fontFile, $this->watermarkText)[2];$textHeight = imagettfbbox($this->fontSize, 0, $fontFile, $this->watermarkText)[5];$position = $this->calculatePosition($sourceWidth, $sourceHeight, $textWidth, $textHeight);$color = imagecolorallocatealpha($this->sourceImage,$this->fontColor['r'],$this->fontColor['g'],$this->fontColor['b'],127 ($this->watermarkOpacity * 127 / 100));imagettftext($this->sourceImage, $this->fontSize, 0, $position['x'], $position['y'], $color, $fontFile, $this->watermarkText);} elseif ($this->watermarkImage) {// 图片水印逻辑$watermarkWidth = imagesx($this->watermarkImage);$watermarkHeight = imagesy($this->watermarkImage);$position = $this->calculatePosition($sourceWidth, $sourceHeight, $watermarkWidth, $watermarkHeight);imagecopymerge($this->sourceImage, $this->watermarkImage,$position['x'], $position['y'], 0, 0,$watermarkWidth, $watermarkHeight,$this->watermarkOpacity);}// 保存图片switch (pathinfo($outputPath, PATHINFO_EXTENSION)) {case 'jpg':case 'jpeg':imagejpeg($this->sourceImage, $outputPath, $quality);break;case 'png':imagepng($this->sourceImage, $outputPath, round($quality / 11));break;case 'gif':imagegif($this->sourceImage, $outputPath);break;}imagedestroy($this->sourceImage);if ($this->watermarkImage) {imagedestroy($this->watermarkImage);}}private function calculatePosition($sourceWidth, $sourceHeight, $watermarkWidth, $watermarkHeight) {$margin = 10;switch ($this->watermarkPosition) {case 'top-left':return ['x' => $margin, 'y' => $margin];case 'top-right':return ['x' => $sourceWidth $watermarkWidth $margin, 'y' => $margin];case 'bottom-left':return ['x' => $margin, 'y' => $sourceHeight $watermarkHeight $margin];case 'bottom-right':return ['x' => $sourceWidth $watermarkWidth $margin, 'y' => $sourceHeight $watermarkHeight $margin];case 'center':return ['x' => ($sourceWidth $watermarkWidth) / 2, 'y' => ($sourceHeight $watermarkHeight) / 2];default:return ['x' => $sourceWidth $watermarkWidth $margin, 'y' => $sourceHeight $watermarkHeight $margin];}}
使用示例
try {$watermark = new ImageWatermark('source.jpg');$watermark->setTextWatermark('© 2025 My Company', 20, '#ffffff');$watermark->setPosition('bottom-right');$watermark->setOpacity(70);$watermark->applyWatermark('output.jpg');echo "Watermark applied successfully!";} catch (Exception $e) {echo "Error: " . $e->getMessage();}
注意事项
相关问答FAQs
Q1:如何为水印添加倾斜效果?
A:可以在
imagettftext()
函数中使用参数实现文字倾斜,例如
imagettftext($image, $fontSize, 45, $x, $y, $color, $fontFile, $text)
中的表示45度倾斜。
Q2:水印类如何支持批量处理多张图片?
A:可以创建一个循环遍历图片目录,对每张图片实例化
ImageWatermark
类并调用
applyWatermark
方法,
$images = glob('images/*.jpg');foreach ($images as $image) {$watermark = new ImageWatermark($image);$watermark->setTextWatermark('Batch Processed');$watermark->applyWatermark('output/' . basename($image));}














发表评论