0

I am using this code to water mark an image and then save it water marked. The problem when I try to watermark a gif image the I am getting only the first frame of that gif.

imagecopy(
   // source
   $main_image,
   // destination
   $logoImage,
   // destination x and y
   $imageWidth-$logoWidth, $imageHeight-$logoHeight,
   // source x and y
   0, 0,
   // width and height of the area of the source to copy
   $logoWidth, $logoHeight);
    if($type == "png"){imagepng($main_image, $new_path);}
    if($type == "jpeg"){imagejpeg($main_image, $new_path);}
    if($type == "gif"){imagegif($main_image, $new_path);}

How do I fix this problem ?

ernie
  • 6,356
  • 23
  • 28
user1481850
  • 238
  • 3
  • 13
  • 24

1 Answers1

0

The image* functions in PHP use the GD library. Though not stated in the PHP manual, the GD library does not support GIF animations (though, numerous comments on the site mention it). At this time, the GD Project's website is also unavailable for additional confirmation.

Alternatively, if you have ImageMagick available, you can use exec() to call the convert application to render your watermark+animation server-side (reference):

$animated_gif = "source.gif";
$gif_with_watermark = "destination.gif";
$watermark = "watermark.png";

$cmd = 'convert '
       . escapeshellarg($animated_gif)
       . ' -coalesce -gravity South -geometry +0+0 null: '
       . escapeshellarg($watermark)
       . ' -layers composite -layers optimize '
       . escapeshellarg($gif_with_watermark);
exec($cmd);
Community
  • 1
  • 1
newfurniturey
  • 37,556
  • 9
  • 94
  • 102
  • Beware, ImageMagick can consume a *lot* of resources when handling animated gifs, as it needs to split apart the frames, and then process each. – Chris Henry Aug 31 '12 at 21:36