【问题标题】:WaterMarking on gif imagegif图像上的水印
【发布时间】:2014-04-14 14:55:47
【问题描述】:

所以我在 Gif 图像上为 png 图像添加水印。这是我的代码:

<?php
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('add_item.png');// watermark image
$im = imagecreatefromjpeg('gif_image.gif');// source image
$image_path = "/opt/lampp/htdocs/my/Harshal/watermarking/".time().".png";

$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory
//header('Content-type: image/png');
imagepng($im,$image_path);
imagedestroy($im);
?>

我的代码工作正常,它在 gif 图像上为 png 图像加水印,但问题是: 水印png图像的后面是白色背景,而不是其他图像的透明背景。

你们能告诉我为什么只有 gif 图像有问题吗?

查看生成的图像。

我知道 Gif 是许多图像的组合,但有什么解决方案可以让图像具有正常行为。? 我也看到了一些在线水印工具,但它们也有同样的问题。

【问题讨论】:

  • 如果您的PNG中设置为“透明”的颜色与您的GIF不同,它将显示为不透明。
  • 这里也是一个很好的例子:Link

标签: php gif watermark


【解决方案1】:

您可以将 gif 转换为真彩色图像。试试这个:

<?php
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('add_item.png');// watermark image
$im = imagecreatefromgif('gif_image.gif');// source image
$image_path = "/opt/lampp/htdocs/my/Harshal/watermarking/".time().".png";

$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Convert gif to a true color image
$tmp = imagecreatetruecolor(imagesx($im), imagesy($im));
$bg = imagecolorallocate($tmp, 255, 255, 255);
imagefill($tmp, 0, 0, $bg);
imagecopy($tmp, $im, 0, 0, 0, 0, imagesx($im), imagesy($im));
$im = $tmp;

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory
//header('Content-type: image/png');
imagepng($im,$image_path);
imagedestroy($im);

【讨论】:

  • 对我来说工作得很好:) 谢谢..!!
猜你喜欢
  • 1970-01-01
  • 2013-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多