2012-08-31 24 views
0

我正在使用此代码来标记图像,然后将其保存为水标记。当我尝试为gif图像添加水印时,问题出在我得到的只是该gif的第一帧。php imagecopy不能使用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);} 

如何解决此问题?

+4

http://stackoverflow.com/search? q =%5Bphp%5D + watermark + gif –

+3

与PHP捆绑在一起的GD库不支持动画GIF。 – BenM

+2

ImageMagick确实如此:http://stackoverflow.com/questions/10531514/watermark-on-animated-gif-with-php –

回答

0

PHP中的image*函数使用GD库。尽管在PHP手册中没有说明,但GD库不支持GIF动画(尽管网站上的许多评论都提到了它)。目前,GD Project's网站也无法进行其他确认。

或者,如果你有ImageMagick可用,您可以使用exec()调用convert应用渲染你的水印+动画服务器端(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); 
+0

请注意,ImageMagick在处理动画gif时可能会消耗大量资源,因为它需要分割帧,然后处理每个帧。 –