使用GD(imagettftext())和UTF-8字符 [英] Working with GD ( imagettftext() ) and UTF-8 characters

查看:243
本文介绍了使用GD(imagettftext())和UTF-8字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅供记录 - 这是我的第一个问题,但希望不是我在社区的最后一个输入。
但这不是我在这里的原因。

Just for the record - my first question here but hopefully not my last input in the community. But that's not why I'm here.

我目前正在开发一个简单的系统,必须生成一个带有文本的图像。 Everthing进展顺利,直到我意识到GD无法处理UTF-8字符,如

I'm currently developing a simple system that has to generate an image with a text on it. Everthing went well until I realised that GD cannot handle UTF-8 characters like


ā,č,ž,ä,ø,é

ā, č, ž, ä, ø, é

等等。

要清理 - 我m使用 imagettftext()

试图解决我的问题我深入研究谷歌并返回了一些解决方案,遗憾的是,他们都没有完全解决我的问题。
目前我正在使用我在此主题中找到的脚本 - PHP function imagettftext()和unicode

Trying to solve my problem I dug into depths of google and some solutions were returned, none of them, sadly, solved my problem completely. Currently I'm using this script I found in this thread - PHP function imagettftext() and unicode

private function properText($text){

    // Convert UTF-8 string to HTML entities
    $text = mb_convert_encoding($text, 'HTML-ENTITIES',"UTF-8");
    // Convert HTML entities into ISO-8859-1
    $text = html_entity_decode($text,ENT_NOQUOTES, "ISO-8859-1");
    // Convert characters > 127 into their hexidecimal equivalents
    $out = "";
    for($i = 0; $i < strlen($text); $i++) {
        $letter = $text[$i];
        $num = ord($letter);
        if($num>127) {
          $out .= "&#$num;";
        } else {
          $out .=  $letter;
        }
    }

    return $out;

}

它适用于某些字符,但不适用于所有字符例如,一个变音符号没有正确转换。

and it works fine for some characters but not all of them, for example, a with umlaut isn't converted correctly.

所以此时我不知道在哪里以及该找什么因为我无法预测用户输入。更确切地说,系统从xml提要中提取艺术家名称并使用数据生成图像(我不打算支持象形文字)。

So at this point I'm not sure where and what to look for anymore as I cannot predict the user input. To be more precise, the system is pulling artist names from an xml feed and using the data for the image generation (I'm not planning to support hieroglyphs).

I通过使用PHP的 mb_detect_encoding()我确保当前未正确显示的所有字符都放在我正在输入 imagettftext() $的字体文件中b $ b通过使用windows charmap 工具检查它。

I've made sure that the data gathered from the feed is indeed UTF-8 by using PHP's mb_detect_encoding() and I've made sure that all the characters that currently aren't displayed correctly are indded in the font file I'm feeding to the imagettftext() function by checking it with windows charmap tool.

希望我能在这里找到答案,并提前感谢您的帮助!

Hopefully I can find my answer here and thank you for your help in advance!

编辑

澄清 - 字符显示不正确,或者,更准确地说,被畸形字符取代。这是截图 -

To clarify - the characters are not displayed correctly, or, to be more precise, are replaced by malformed characters. Here is a screenshot -

它应该是JoséGonzález

it should read "José González"

编辑No2

对从xml Feed中检索到的数据使用 bin2hex()函数会返回此信息。

Using bin2hex() function on data retrieved from the xml feed returns this.

José González -> 4a6f73c3a920476f6e7ac3a16c657a
// input -> bin2hex(input)

编辑 - 已修复

当我继续我的研究时,我想出了一个问题的答案,这段代码做到了!

As I continued my research I came up with an answer for my problem, this piece of code did it!

$text = mb_convert_encoding($text, "HTML-ENTITIES", "UTF-8");
$text = preg_replace('~^(&([a-zA-Z0-9]);)~',htmlentities('${1}'),$text);
return($text); 

现在所有困扰我的人物都能正确显示!

Now all the characters that troubled me are displayed correctly!

推荐答案

当我继续我的研究时,我想出了一个问题的答案,这段代码做到了!

As I continued my research I came up with an answer for my problem, this piece of code did it!

private function properText($text){
    $text = mb_convert_encoding($text, "HTML-ENTITIES", "UTF-8");
    $text = preg_replace('~^(&([a-zA-Z0-9]);)~',htmlentities('${1}'),$text);
    return($text); 
}

现在所有的角色(以及我见过的所有角色)都是麻烦我正确显示!

Now all the characters (and all the new ones I've seen) that troubled me are displayed correctly!

这篇关于使用GD(imagettftext())和UTF-8字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆