Search code examples
phpimagick

Imagick annotateImage not showing correctly japanese text


I'm trying to print japanese text to the image.
My code:

$text = // some japanese text

$imagick = new IMagick();
// $imagick implementation

$imagickDraw = new ImagickDraw();
$imagickDraw->setFontSize(12);

$textFontMetrics = $imagick->queryFontMetrics($imagickDraw, $text);
$imagick->annotateImage($imagickDraw, ($imageWidth - $textFontMetrics['textWidth']) / 2, $imageHeight * 0.5, 0, $text);

// save imageBlob

When i check my generated image, instead of normal japanese text i just see '??'. Any ideas how to solve this problem?


Solution

  • Pretty sure this is a font related issue. Make sure you have a font capable of displaying Japanese characters, copy that font to your script's directory, and add the following:

    $draw->setFont('fonts-japanese-gothic.ttf');
    

    Where fonts-japanese-gothic.ttf is the name of your font. I tested it out on my local machine and that did the trick.