問題描述
只是為了記錄 - 我在這里的第一個問題,但希望不是我在社區中的最后一個輸入.但這不是我在這里的原因.
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.
我目前正在開發一個簡單的系統,該系統必須生成帶有文本的圖像.一切都很順利,直到我意識到 GD 無法處理像
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
ā, ?, ?, ?, ?, é
ā, ?, ?, ?, ?, é
等等.
為了解決問題 - 我正在使用 imagettftext()一個>
為了解決我的問題,我深入谷歌搜索并返回了一些解決方案,但遺憾的是,沒有一個解決方案完全解決了我的問題.目前我正在使用我在這個線程中找到的這個腳本 - PHP 函數 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).
我使用 PHP 的 mb_detect_encoding() 并且我確保當前未正確顯示的所有字符都包含在我提供給 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!
編輯
澄清 - 字符顯示不正確,或者更準確地說,被格式錯誤的字符替換.這是屏幕截圖 -
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 提要檢索到的數據使用 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 字符的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!