Imagick annotateImage 没有正确显示日语文本

Imagick annotateImage not showing correctly japanese text

我正在尝试将日语文本打印到图像中。
我的代码:

$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

当我检查生成的图像时,我看到的不是普通的日文文本,而是“??”。有解决这个问题的想法吗?

很确定这是一个与字体相关的问题。确保您有能够显示日语字符的字体,将该字体复制到您的脚本目录,并添加以下内容:

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

其中 fonts-japanese-gothic.ttf 是您的字体名称。我在我的本地机器上测试了它,成功了。