問(wèn)題描述
只是為了記錄 - 我在這里的第一個(gè)問(wèn)題,但希望不是我在社區(qū)中的最后一個(gè)輸入.但這不是我在這里的原因.
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.
我目前正在開(kāi)發(fā)一個(gè)簡(jiǎn)單的系統(tǒng),該系統(tǒng)必須生成帶有文本的圖像.一切都很順利,直到我意識(shí)到 GD 無(wú)法處理像
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
ā, ?, ?, ?, ?, é
ā, ?, ?, ?, ?, é
等等.
為了解決問(wèn)題 - 我正在使用 imagettftext()一個(gè)>
為了解決我的問(wèn)題,我深入谷歌搜索并返回了一些解決方案,但遺憾的是,沒(méi)有一個(gè)解決方案完全解決了我的問(wèn)題.目前我正在使用我在這個(gè)線程中找到的這個(gè)腳本 - PHP 函數(shù) imagettftext() 和 unicode一個(gè)>
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;
}
它適用于某些字符,但不是所有字符,例如,帶有變音符號(hào)的轉(zhuǎn)換不正確.
and it works fine for some characters but not all of them, for example, a with umlaut isn't converted correctly.
所以在這一點(diǎn)上,我不知道去哪里尋找什么,因?yàn)槲覠o(wú)法預(yù)測(cè)用戶(hù)輸入.更準(zhǔn)確地說(shuō),系統(tǒng)從 xml 提要中提取藝術(shù)家姓名并使用數(shù)據(jù)生成圖像(我不打算支持象形文字).
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).
我使用 PHP 的 mb_detect_encoding() 并且我確保當(dāng)前未正確顯示的所有字符都包含在我提供給 imagettftext()<的字體文件中/em>使用 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!
編輯
澄清 - 字符顯示不正確,或者更準(zhǔn)確地說(shuō),被格式錯(cuò)誤的字符替換.這是屏幕截圖 -
To clarify - the characters are not displayed correctly, or, to be more precise, are replaced by malformed characters. Here is a screenshot -
應(yīng)該是José González"
it should read "José González"
編輯 No2
對(duì)從 xml 提要檢索到的數(shù)據(jù)使用 bin2hex() 函數(shù)返回這個(gè).
Using bin2hex() function on data retrieved from the xml feed returns this.
José González -> 4a6f73c3a920476f6e7ac3a16c657a
// input -> bin2hex(input)
編輯 - 已修復(fù)
在我繼續(xù)研究的過(guò)程中,我為我的問(wèn)題找到了答案,這段代碼做到了!
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);
現(xiàn)在所有困擾我的字符都正確顯示了!
Now all the characters that troubled me are displayed correctly!
推薦答案
在我繼續(xù)研究的過(guò)程中,我找到了解決問(wèn)題的答案,這段代碼做到了!
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);
}
現(xiàn)在所有困擾我的角色(以及我見(jiàn)過(guò)的所有新角色)都正確顯示了!
Now all the characters (and all the new ones I've seen) that troubled me are displayed correctly!
這篇關(guān)于使用 GD ( imagettftext() ) 和 UTF-8 字符的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!