encoding error displaying fontawesome with imagemagick

早过忘川 提交于 2019-12-22 10:39:23

问题



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


any idea how to fix such a encoding problem?


回答1:


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;



回答2:


Looks like the Unicode sequence isn't being recognised properly as you are getting multiple characters rather than just one. If you get one missing block displaying - it's a font issue!

I have a unicode escape working in Ruby RMagic so maybe the same sequence will work for you in PHP?

On http://fortawesome.github.io/Font-Awesome/cheatsheet/ the "fa-heart" is listed as "#xf004;" This appears to be an HTML escape, so the one you need should be:

$text = "\uf004";


来源:https://stackoverflow.com/questions/19728744/encoding-error-displaying-fontawesome-with-imagemagick

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