PHP 图片加中文水印是指在图片上添加中文文字作为水印的过程。这个过程通常涉及到图像处理库,如 GD 库或 Imagick 扩展,用于在图片上绘制文字。
以下是一个使用 GD 库在 PHP 中添加中文水印的示例代码:
<?php
// 加载图片
$image = imagecreatefromjpeg('path/to/image.jpg');
// 设置字体和颜色
$font = 'path/to/font.ttf'; // 确保字体支持中文
$color = imagecolorallocate($image, 255, 255, 255); // 白色
// 设置水印文字
$text = '中文水印';
$textWidth = imagettfbbox(30, 0, $font, $text);
$textHeight = $textWidth[1] - $textWidth[7];
// 计算水印位置
$x = (imagesx($image) - $textWidth[2]) / 2;
$y = (imagesy($image) - $textHeight) / 2;
// 添加水印
imagettftext($image, 30, 0, $x, $y, $color, $font, $text);
// 输出图片
header('Content-Type: image/jpeg');
imagejpeg($image);
// 释放内存
imagedestroy($image);
?>
x
和 y
的计算方式,确保水印位置符合预期。x
和 y
的计算公式,确保它们正确反映了水印的位置。通过以上方法,可以有效地在 PHP 中实现图片加中文水印的功能,并解决常见的问题。
领取专属 10元无门槛券
手把手带您无忧上云