2014-11-23 89 views
1

我嘗試在gif動畫添加文字與ImageMagick的:添加對gif動畫文字與ImageMagick的和PHP

$source = public_path().'/test.gif'; 
$output = public_path().'/test2.gif'; 
$text = 'the cake is a lie'; 

// Create objects 
$image = new Imagick($source); 

// Create a new drawing palette 
$draw = new ImagickDraw(); 

// Set font properties 
$draw->setFont(public_path().'/fonts/Impact.ttf'); 
$draw->setFontSize(20); 
$draw->setFillColor('black'); 

// Position text at the bottom-right of the image 
$draw->setGravity(Imagick::GRAVITY_SOUTHEAST); 

// Draw text on the image 
$image->annotateImage($draw, 10, 12, 0, $text); 

// Draw text again slightly offset with a different color 
$draw->setFillColor('white'); 
$image->annotateImage($draw, 11, 11, 0, $text); 

// Set output image format 
$image->setImageFormat('gif'); 

$image->writeImage($output); 

但有了這個代碼的圖像不再動畫和質量是非常糟糕的,任何想法?

回答

1

最後發現:

exec('/usr/local/bin/convert '.$source_img.' -font '.$font_location.' -pointsize 14 -draw "gravity south fill black text 0,12 \'some text\' fill white text 1,11 \'some text\' " '.$output_img); 
1

你可能想使用-coalesce方法從您的GIF幀刪除所有的優化,然後貼上標籤,然後用-layers Optimize重新制作動畫,因爲否則的各種優化技術(在調色板和幀間差異)干擾,並降低您的標籤的質量。

所以,在命令行中,過程是這樣的:

convert animation.gif -coalesce \ 
     -gravity SouthEast -background white ... <do label here> 
     -layers Optimize output.gif 

參考See ImageMagick documentation here

我不傾向於使用PHP,但似乎方法匹配上述功能:

​​

documentation

bool Imagick::optimizeImageLayers (void) 

documentation

+0

謝謝,但我怎麼能這樣執行命令?只有exec()?這個命令中的文本部分在哪裏?再次感謝 – Tahola 2014-11-23 14:33:20

+0

的文字註釋會做,我把'<做標記的位置>'。您可以使用自己的代碼進行標記,也可以使用我添加到我的答案中的兩個PHP函數。 – 2014-11-23 15:00:08