[英]Adding text on animated gif with ImageMagick and php
我嘗試使用ImageMagick在動畫gif上添加文本:
$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);
但是使用此代碼后,圖像不再具有動畫效果,並且質量很差,您知道嗎?
您可能想使用-coalesce
方法從GIF框架中刪除所有優化,然后對其進行標記,然后使用-layers Optimize
重新設置其動畫-layers Optimize
,因為否則,各種優化(針對調色板和幀間差異)都會干擾,並降低標簽的質量。
因此,在命令行中,過程如下:
convert animation.gif -coalesce \
-gravity SouthEast -background white ... <do label here>
-layers Optimize output.gif
參考 : 請參見ImageMagick文檔
我不傾向於使用PHP,但是似乎有一些方法可以匹配上述功能:
Imagick Imagick::coalesceImages ( void )
bool Imagick::optimizeImageLayers ( void )
終於找到了 :
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.