PHP 函数 imagettftext() 和 unicode
我正在使用 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.
相关文章