問題描述
我正在使用 PHP 函數 imagettftext() 將文本轉換為 GIF 圖像.我正在轉換的文本包含 Unicode 字符,包括日語.在我的本地機器(Ubuntu 7.10)上一切正常,但在我的網絡主機服務器上,日語字符被破壞了.什么可能導致差異?一切都應編碼為 UTF-8.
I'm using the PHP function imagettftext() to convert text into a GIF image. The text I am converting has Unicode characters including Japanese. Everything works fine on my local machine (Ubuntu 7.10), but on my webhost server, the Japanese characters are mangled. What could be causing the difference? Everything should be encoded as UTF-8.
虛擬主機服務器上的損壞圖像:http://www.ibeni.net/flashcards/imagetest.php
Broken Image on webhost server: http://www.ibeni.net/flashcards/imagetest.php
從我的本地機器復制正確的圖像:http://www.ibeni.net/flashcards/imagetest.php.gif
Copy of correct image from my local machine: http://www.ibeni.net/flashcards/imagetest.php.gif
從我的本地機器復制 phpinfo():http://www.ibeni.net/flashcards/phpinfo.php.html
Copy of phpinfo() from my local machine: http://www.ibeni.net/flashcards/phpinfo.php.html
從我的虛擬主機服務器復制 phpinfo():http://example5.nfshost.com/phpinfo
Copy of phpinfo() from my webhost server: http://example5.nfshost.com/phpinfo
代碼:
mb_language('uni');
mb_internal_encoding('UTF-8');
header('Content-type: image/gif');
$text = '日本語';
$font = './Cyberbit.ttf';
// Create the image
$im = imagecreatetruecolor(160, 160);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
// Create some colors
imagefilledrectangle($im, 0, 0, 159, 159, $white);
// Add the text
imagettftext($im, 12, 0, 20, 20, $black, $font, $text);
imagegif($im);
imagedestroy($im);
推薦答案
以下是最終對我有用的解決方案:
Here's the solution that finally worked for me:
$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;
}
}
將字符串轉換為 HTML 實體是可行的,只是函數 imagettftext() 不接受命名實體.例如,
Converting the string to HTML entities works except that the function imagettftext() doesn't accept named entities. For example,
日本語
沒問題,但是
ç
不是.轉換回 ISO-8859-1,將命名實體轉換回字符,但還有第二個問題.imagettftext() 不支持值大于 >127 的字符.最后的 for 循環以十六進制對這些字符進行編碼.此解決方案適用于我正在使用的文本(包括日語、中文和葡萄牙語的重音拉丁字符),但我不能 100% 確定它適用于所有情況.
is not. Converting back to ISO-8859-1, converts the named entities back to characters, but there is a second problem. imagettftext() doesn't support characters with a value greater than >127. The final for-loop encodes these characters in hexadecimal. This solution is working for me with the text that I am using (includes Japanese, Chinese and accented latin characters for Portuguese), but I'm not 100% sure it will work in all cases.
需要所有這些體操,因為 imagettftext() 在我的服務器上并不真正接受 UTF-8 字符串.
All of these gymnastics are needed because imagettftext() doesn't really accept UTF-8 strings on my server.
這篇關于PHP 函數 imagettftext() 和 unicode的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!