不透明的水印图像

发布于 2024-12-28 18:05:30 字数 1417 浏览 3 评论 0原文

我正在尝试使用 PHP 和 GD 图像库向图像添加水印。我可以在指定的位置应用正确的不透明度设置的水印。

问题是我的水印本身有透明背景。当我尝试将此水印应用到图像时,我得到黑色背景。

应用水印的图像是 jpeg。这可能是问题所在吗?如果是这样,我如何将 jpeg 转换为支持透明度的格式,应用水印,然后将其转换回来?

这是我目前拥有的关键代码。

// Determine image size and type
$size = getimagesize($this->image_path);
$size_x = $size[0];
$size_y = $size[1];
$image_type = $size[2]; // This is always a JPEG

// load source image
$image = $this->ImageCreateFromType($image_type, $this->image_path);

// Determine watermark size and type
$wsize = getimagesize($watermark_path);
$watermark_x = $wsize[0];
$watermark_y = $wsize[1];
$watermark_type = $wsize[2]; // This is typically a PNG

// load watermark
$watermark = $this->ImageCreateFromType($watermark_type, $watermark_path);

$dest_x = $this->setX($size_x, $watermark_x);
$dest_y = $this->setY($size_y, $watermark_y);

imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_x, $watermark_y, $this->opacity);

虽然不太相关,但这里是 ImageCreateFromType 函数的代码

function ImageCreateFromType($type,$filename) {
 $im = null;
 switch ($type) {
   case 1:
     $im = ImageCreateFromGif($filename);
     break;
   case 2:
     $im = ImageCreateFromJpeg($filename);
     break;
   case 3:
     $im = ImageCreateFromPNG($filename);
     imagealphablending($im, true);
     imagesavealpha($im, true);
     break;
  }
  return $im;
}

I am trying to add a watermark to an image using PHP and the GD image library. I can apply the watermark where I specify with the correct opacity setting.

The problem is that my watermark itself has a transparent background. When I try to apply this watermark to the image I get a black background.

The image which the watermark is being applied to is a jpeg. Could this be the problem? If so how would I convert the jpeg into a format which supports transparency, apply watermark, then convert it back?

This is the key bit of code I have at the moment.

// Determine image size and type
$size = getimagesize($this->image_path);
$size_x = $size[0];
$size_y = $size[1];
$image_type = $size[2]; // This is always a JPEG

// load source image
$image = $this->ImageCreateFromType($image_type, $this->image_path);

// Determine watermark size and type
$wsize = getimagesize($watermark_path);
$watermark_x = $wsize[0];
$watermark_y = $wsize[1];
$watermark_type = $wsize[2]; // This is typically a PNG

// load watermark
$watermark = $this->ImageCreateFromType($watermark_type, $watermark_path);

$dest_x = $this->setX($size_x, $watermark_x);
$dest_y = $this->setY($size_y, $watermark_y);

imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_x, $watermark_y, $this->opacity);

While not really relevant, here is the code for the ImageCreateFromType function

function ImageCreateFromType($type,$filename) {
 $im = null;
 switch ($type) {
   case 1:
     $im = ImageCreateFromGif($filename);
     break;
   case 2:
     $im = ImageCreateFromJpeg($filename);
     break;
   case 3:
     $im = ImageCreateFromPNG($filename);
     imagealphablending($im, true);
     imagesavealpha($im, true);
     break;
  }
  return $im;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清醇 2025-01-04 18:05:30

我发现最有效的是以下解决方案:

$image_url = "The url of my image";
$watermark_image_url= "The url of my watermark image";
$watermark_image_size = 40; // percent of size relatively to the target image
$watermark_opacity = 50; // opacity 0-100

$im = imagecreatefromstring(file_get_contents($image_url));
$stamp = imagecreatefromstring(file_get_contents($watermark_image_url));

$margin_right = 10;
$margin_bottom = 10;

$stamp_w = imagesx($stamp);
$stamp_h = imagesy($stamp);

// Change the size of the watermark image
$percent_size = $watermark_image_size;
$percent_stamp_w = (int)($stamp_w * $percent_size / 100);
$percent_stamp_h = (int)($stamp_h * $percent_size / 100);

$stamp_imgResized = imagescale($stamp , $percent_stamp_w, $percent_stamp_h);

$opacity = (float)(watermark_opacity / 100);
$final_opacity = 127 - (int)(127*$opacity);

imagealphablending($stamp_imgResized, false);
imagesavealpha($stamp_imgResized, true);

imagefilter($stamp_imgResized, IMG_FILTER_COLORIZE, 0,0,0,$final_opacity); // the fourth parameter is alpha       

imagecopy($im, $stamp_imgResized, $margin_right, $margin_bottom, 0, 0, $percent_stamp_w, $percent_stamp_h);

// output to file
if (str_contains($thumbnail_image, '.png'))
{
    imagepng($im, $output);
} else if (str_contains($thumbnail_image, '.jpg'))
{
    imagejpeg($im, $output);
}

它也适用于已经具有透明背景的图像,例如徽标图像。

What I found that works the most is the following solution:

$image_url = "The url of my image";
$watermark_image_url= "The url of my watermark image";
$watermark_image_size = 40; // percent of size relatively to the target image
$watermark_opacity = 50; // opacity 0-100

$im = imagecreatefromstring(file_get_contents($image_url));
$stamp = imagecreatefromstring(file_get_contents($watermark_image_url));

$margin_right = 10;
$margin_bottom = 10;

$stamp_w = imagesx($stamp);
$stamp_h = imagesy($stamp);

// Change the size of the watermark image
$percent_size = $watermark_image_size;
$percent_stamp_w = (int)($stamp_w * $percent_size / 100);
$percent_stamp_h = (int)($stamp_h * $percent_size / 100);

$stamp_imgResized = imagescale($stamp , $percent_stamp_w, $percent_stamp_h);

$opacity = (float)(watermark_opacity / 100);
$final_opacity = 127 - (int)(127*$opacity);

imagealphablending($stamp_imgResized, false);
imagesavealpha($stamp_imgResized, true);

imagefilter($stamp_imgResized, IMG_FILTER_COLORIZE, 0,0,0,$final_opacity); // the fourth parameter is alpha       

imagecopy($im, $stamp_imgResized, $margin_right, $margin_bottom, 0, 0, $percent_stamp_w, $percent_stamp_h);

// output to file
if (str_contains($thumbnail_image, '.png'))
{
    imagepng($im, $output);
} else if (str_contains($thumbnail_image, '.jpg'))
{
    imagejpeg($im, $output);
}

It also works with images that already have a transparent background, such as a logo image for example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文