Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP ImagickDraw with outlined text issues

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:

enter image description here

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:

enter image description here

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.

like image 769
user1854344 Avatar asked Apr 14 '26 19:04

user1854344


1 Answers

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.

like image 91
Lukas Nagel Avatar answered Apr 16 '26 08:04

Lukas Nagel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!