如何在 PHP 中已重新采样的图像上覆盖水印(使用 GD)?
这是我当前的代码:
define('IMG_WIDTH', (isset ($_GET['width'])) ? (int) $_GET['width'] : 99);
define('IMG_HEIGHT', (isset ($_GET['height'])) ? (int) $_GET['height'] : 75);
$image = imagecreatefromjpeg($_GET['image']);
$origWidth = imagesx($image);
$origHeight = imagesy($image);
$croppedThumb = imagecreatetruecolor(IMG_WIDTH, IMG_HEIGHT);
if ($origWidth > $origHeight)
{
$leftOffset = ($origWidth - $origHeight) / 2;
imagecopyresampled($croppedThumb, $image, 0, 0, $leftOffset, 0, IMG_WIDTH, IMG_HEIGHT, $origHeight, $origHeight);
}
else
{
$topOffset = ($origHeight - $origWidth) / 2;
imagecopyresampled($croppedThumb, $image, 0, 0, 0, $topOffset, IMG_WIDTH, IMG_HEIGHT, $origWidth, $origWidth);
}
它基本上获取图像并重新调整其大小以创建缩略图。它工作得很好。我现在想做的是在右下角添加水印。我已经看到用于此目的的 imagecopymerge
函数...但是,这似乎不允许我提供重新采样的图像作为源。
如何获取已修改的图像并添加水印? :/
我想过将图像保存到/tmp,然后在添加水印后取消链接(),但这看起来有点混乱......
Here's my current code:
define('IMG_WIDTH', (isset ($_GET['width'])) ? (int) $_GET['width'] : 99);
define('IMG_HEIGHT', (isset ($_GET['height'])) ? (int) $_GET['height'] : 75);
$image = imagecreatefromjpeg($_GET['image']);
$origWidth = imagesx($image);
$origHeight = imagesy($image);
$croppedThumb = imagecreatetruecolor(IMG_WIDTH, IMG_HEIGHT);
if ($origWidth > $origHeight)
{
$leftOffset = ($origWidth - $origHeight) / 2;
imagecopyresampled($croppedThumb, $image, 0, 0, $leftOffset, 0, IMG_WIDTH, IMG_HEIGHT, $origHeight, $origHeight);
}
else
{
$topOffset = ($origHeight - $origWidth) / 2;
imagecopyresampled($croppedThumb, $image, 0, 0, 0, $topOffset, IMG_WIDTH, IMG_HEIGHT, $origWidth, $origWidth);
}
It basically takes an image and re-sizes it to create a thumbnail. It works quite nicely. What I would like to do now is add a watermark to the bottom right corner. I've seen the imagecopymerge
function used for this... However, that doesn't seem to allow me to supply a resampled image as the source.
How can I take my already modified image and add a watermark? :/
I've thought of saving the image to /tmp and then unlink()'ing it once I've added the watermark but that seems like a bit of a mess...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
$croppedThumb
作为imagecopymerge
的第一个参数。您不必先保存图像。You can use
$croppedThumb
as the first argument toimagecopymerge
. You don't have to save the image first.