1

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

中国人

我明白了:

字符代码

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

    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;
4

1 回答 1

0

看起来$textString传递给您的函数的值是 HTML 编码的。您的我字符串是HTML 实体。它们在浏览器中呈现良好,但您需要在将它们转换为 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); 
于 2020-12-02T16:59:49.737 回答