How can I overlay a watermark on an already resampled image in PHP (using GD)?

﹥>﹥吖頭↗ 提交于 2020-01-06 08:27:12

问题


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...


回答1:


You can use $croppedThumb as the first argument to imagecopymerge. You don't have to save the image first.



来源:https://stackoverflow.com/questions/2902000/how-can-i-overlay-a-watermark-on-an-already-resampled-image-in-php-using-gd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!