Harshal
Harshal

Reputation: 3622

WaterMarking on gif image

So I am watermarking the png image on Gif image.here is my code :

<?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);
?>

My code is working fine and its watermarking the png image on gif image but The problem is : There is white background coming just behind of the watermark png image and not the transparent as comes for other images.

Can you guys tell me why only gif image have issues ?

See the generated image. enter image description here

I know Gif is the combinations of many images but is there any solution that we can put the image with normal behavior.? I saw some Online watermarking tools also but they have also same issue.

Upvotes: 2

Views: 921

Answers (1)

steveteuber
steveteuber

Reputation: 182

You can convert the gif to a true color image. Try this:

<?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);

Upvotes: 1

Related Questions