2012-08-31 22 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時可能會消耗大量資源,因爲它需要分割幀,然後處理每個幀。 –