当我向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;发布于 2020-12-03 00:59:50
看起来传递给函数的HTML值是$textString编码的。您的我字符串是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); https://stackoverflow.com/questions/65112475
复制相似问题