当我向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;
似乎$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);
感谢您为我设定正确的方向。我的最终代码是
$textString = html_entity_decode($textString);