Warm tip: This article is reproduced from serverfault.com, please click

php-ImageMagic注释字符代码,而不是注释图像中的字符

(php - ImageMagic is annotating the character codes rather than the characters in annotateimage)

发布于 2020-12-02 16:48:14

当我向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;
Questioner
Trevor Newhook
Viewed
11
O. Jones 2020-12-03 02:16:31

似乎$textString传递给你的函数值是HTML编码的。你的我字符串是HTML实体它们在浏览器中可以很好地呈现,但是你需要先将其解码为utf-8,然后再将其转换为big-5。

尝试使用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);