在 PHP 中实现图片平铺和倾斜水印效果,可以借助 GD 库来操作图片。下面是一个简单的示例代码,演示如何实现这两种效果:
// 设置图片路径
$sourceImage = 'source.jpg';
// 创建GD图片对象
$source = imagecreatefromjpeg($sourceImage);
// 获取图片尺寸
list($width, $height) = getimagesize($sourceImage);
// 创建一个新的空白图片对象,尺寸为原始图片的两倍
$destination = imagecreatetruecolor($width * 2, $height * 2);
// 在新图片对象上平铺原始图片
imagecopyresampled($destination, $source, 0, 0, 0, 0, $width, $height, $width, $height);
imagecopyresampled($destination, $source, $width, 0, 0, 0, $width, $height, $width, $height);
imagecopyresampled($destination, $source, 0, $height, 0, 0, $width, $height, $width, $height);
imagecopyresampled($destination, $source, $width, $height, 0, 0, $width, $height, $width, $height);
// 添加倾斜水印
$watermark = imagecreatefrompng('watermark.png');
$watermarkWidth = imagesx($watermark);
$watermarkHeight = imagesy($watermark);
imagecopyresampled($destination, $watermark, $width/2, $height/2, 0, 0, $watermarkWidth, $watermarkHeight, $watermarkWidth, $watermarkHeight);
// 输出图片
header('Content-Type: image/jpeg');
imagejpeg($destination);
// 释放内存
imagedestroy($source);
imagedestroy($destination);
在这个示例中,首先加载原始图片,然后创建一个两倍尺寸的空白图片对象,并在其中平铺原始图片。接着加载一个带有水印的 PNG 图片,将水印添加到空白图片的中心位置。
最后,输出合成后的图片并释放内存。请确保将示例中的source.jpg
和watermark.png
替换为实际图片的路径,以及适当调整水印的位置和尺寸。