PHP ImagickDraw with outlined text issues

夙愿已清 提交于 2019-12-05 02:48:29

问题


I'm learning and practicing my Imagick skills.

I have issues with outlined text using Imagick stroke. I would like to achieve an effect visible on this image: a popular Internet meme:

Here's the code I have so far:

$draw = new \ImagickDraw();
$outputImage = new \Imagick('meme.jpg');

$draw->setFillColor('#fff');
$draw->setFont('impact.ttf');
$draw->setFontSize(40);
$draw->setGravity(\Imagick::GRAVITY_NORTH);
$draw->setStrokeColor('#000');
$draw->setStrokeWidth(1);
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);

$outputImage->annotateImage($draw, 0, 5, 0, 'Sample text');

$outputImage->setFormat('png');
$outputImage->writeimage('tmp/meme.png');

The issue: text stroke does not look nice. I've found a tip on Imagick discussion board about annotating image second time, but without stroke. Does not work.

Before writing image:

   $draw->setStrokeColor('transparent');
   $outputImage->annotateImage($draw, 0, 5, 0, 'Sample text');

Can anybody give me a clue?

Update
To conclude, my generated image looks as following:

As you can see, I have some issues with 2px stroke while using different font size. On big fonts, it looks nice but with smaller font there are some issues with the stroke and font.


回答1:


Version1: resizing

Version 2: composite over and resizing

Version 2 gives a much better result. See code below. Depending on the final size, you need to play around with font and stroke size, as the resizing may give unwanted effects. You may also try version 2 without resizing.

Version 1: resizing

$draw = new ImagickDraw();
$draw->setFillColor('#fff');
$draw->setFont('impact.ttf');
$draw->setFontSize(100); //use a large font-size
$draw->setStrokeColor('#000');
$draw->setStrokeWidth(4);
$draw->setStrokeAntialias(true);  //try with and without
$draw->setTextAntialias(true);  //try with and without
$outputImage = new Imagick();
$outputImage->newImage(1400,400, "transparent");  //transparent canvas
$outputImage->annotateImage($draw, 20, 100, 0, 'STOP ME FROM MEMEING');
$outputImage->trimImage(0); //Cut off transparent border
$outputImage->resizeImage(300,0, imagick::FILTER_CATROM, 0.9, false); //resize to final size
/*
Now you can compositve over the image
*/
//Clean up
$draw->clear();
$draw->destroy();
$outputImage->clear();
$outputImage->destroy();

Version 2: composite over and resizing

$draw = new ImagickDraw();
$draw->setFont('impact.ttf');
$draw->setFontSize(100); //use a large font-size
$draw->setStrokeAntialias(true);  //try with and without
$draw->setTextAntialias(true);  //try with and without

//Create text  
$draw->setFillColor('#fff');
$textOnly = new Imagick();
$textOnly->newImage(1400,400, "transparent");  /transparent canvas
$textOnly->annotateImage($draw, 21, 101, 0, 'STOP ME FROM MEMEING');  //parameters depend of stroke and text size
//Create stroke
$draw->setFillColor('#000'); //same as stroke color
$draw->setStrokeColor('#000');
$draw->setStrokeWidth(8);
$strokeImage = new Imagick();
$strokeImage->newImage(1400,400, "transparent");
$strokeImage->annotateImage($draw, 20, 100, 0, 'STOP ME FROM MEMEING');

//Composite text over stroke
$strokeImage->compositeImage($textOnly, imagick::COMPOSITE_OVER, 0, 0, Imagick::CHANNEL_ALPHA );
$strokeImage->trimImage(0);  //cut transparent border
$strokeImage->resizeImage(300,0, imagick::FILTER_CATROM, 0.9, false); //resize to final size
/*
Now you can compositve over the image
*/
//Clean up
$draw->clear();
$draw->destroy();
$strokeImage->clear();
$strokeImage->destroy();
$textOnly->clear();
$textOnly->destroy();



回答2:


Can you post your result or be more specific what doesn't look fine to you?

A solution could be to create the text (with stroke) first on a transaprent background and then composite it over the image. You could create the text in bigger font size and resize to make it look smoother on the final image.



来源:https://stackoverflow.com/questions/13840544/php-imagickdraw-with-outlined-text-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!