ImageMagic 注释的是字符代码,而不是 annotateimage 中的字符

ImageMagic is annotating the character codes rather than the characters in annotateimage

当我将一串中文文本发送到 ImageMagick 以注释图像时,字符代码被打印出来。例如,而不是这个:

我明白了:

下面是我的代码。显然,我的字体设置正确。当我在第 3 行 echo $textString; 时,它会正确地打印到浏览器。

    function drawText($textString,$height,$width){
  $textString = mb_convert_encoding($textString, 'UTF-8', 'BIG-5');
  echo $textString;
  $permitted_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  $filepath =  ABSPATH . "\wp-content\uploads\h5p\content\words\".substr(str_shuffle($permitted_chars), 0, 16).".PNG";
  $image = new \Imagick();
  $draw = new \ImagickDraw();
  $pixel = new ImagickPixel('white');

  /* New image */
  $image->newImage($width, $height, $pixel);

  /* Black text */
  $draw->setFillColor('black');

  /* Font properties */
  $draw->setFont(plugin_dir_path( __FILE__ ) .'wt034.ttf');
  $draw->setFontSize( 30 );

  /* Create text */
  $image->annotateImage($draw, 10, 45, 0, $textString);

  /* Give image a format */
  $image->setImageFormat('png');
  file_put_contents($filepath,$image);
return $filepath;

看起来传递给函数的 $textString 值是 HTML 编码的。您的 我 字符串是 HTML entities。它们在浏览器中呈现良好,但您需要在将它们转换为 big-5 之前解码为 utf-8。

尝试使用 html_entity_decode() 进行转化。

$textString = html_entity_decode($textString, ENT_COMPAT, 'UTF-8');
$textString = mb_convert_encoding($textString, 'UTF-8', 'BIG-5');

或者,您可以尝试一步完成。

$textString = html_entity_decode($textString, ENT_COMPAT, 'BIG5');

或者,当你最终这样做时,像这样:

$textString = html_entity_decode($textString);