Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

encoding error displaying fontawesome with imagemagick


i'm trying to use fontawesome with imagemagick to display it's character as png ,
here is my code:

$size = '50';
$text = '';
$imgW = 200;
$imgH = 200;
$font = 'fontawesome-webfont.ttf';
$image = new Imagick();
$image->setResolution(144,144);
$draw = new ImagickDraw();
$draw->setFont($font);
$draw->setFontSize($size);
//$draw->setTextEncoding('UTF-8');
//$draw->setStrokeAntialias(true);
//$draw->setTextAntialias(true);
$image->newImage($imgW, $imgH, new ImagickPixel('none'));
$image->annotateImage($draw, 10, 45, 0, $text);
$image->setImageFormat('png');
header('Content-type: image/png');
echo $image;


but the image output looks like this coded image


any idea how to fix such a encoding problem?

like image 383
Mostafa Darwish Avatar asked Mar 25 '26 00:03

Mostafa Darwish


1 Answers

Using the example from this answer to ensure I had a UTF-8 string being passed into ImageMagick works for me. The only change from your code is the second line:

$size = '50';
$text = html_entity_decode("", ENT_COMPAT, 'UTF-8');
$imgW = 200;
$imgH = 200;
$font = 'fontawesome-webfont.ttf';
$image = new Imagick();
$image->setResolution(144,144);
$draw = new ImagickDraw();
$draw->setFont($font);
$draw->setFontSize($size);
//$draw->setTextEncoding('UTF-8');
//$draw->setStrokeAntialias(true);
//$draw->setTextAntialias(true);
$image->newImage($imgW, $imgH, new ImagickPixel('none'));
$image->annotateImage($draw, 10, 45, 0, $text);
$image->setImageFormat('png');
header('Content-type: image/png');
echo $image;
like image 191
Paul Ferrett Avatar answered Mar 26 '26 12:03

Paul Ferrett



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!