我正在使用PHP函数imagettftext()将文本转换为GIF图像.我转换的文本包含Unicode字符,包括日语.在我的本地机器(Ubuntu 7.10)上一切正常,但在我的webhost服务器上,日文字符被破坏了.可能导致差异的原因是什么?一切都应编码为UTF-8.
webhost服务器上的破碎图像:http: //www.ibeni.net/flashcards/imagetest.php
从我的本地机器复制正确的图像:http: //www.ibeni.net/flashcards/imagetest.php.gif
我本地机器上的phpinfo()副本:http: //www.ibeni.net/flashcards/phpinfo.php.html
我的webhost服务器上的phpinfo()副本: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);
Run Code Online (Sandbox Code Playgroud)
use*_*478 13
这是最终为我工作的解决方案:
$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;
}
}
Run Code Online (Sandbox Code Playgroud)
将字符串转换为HTML实体的工作原理除外,函数imagettftext()不接受命名实体.例如,
日本語
Run Code Online (Sandbox Code Playgroud)
没关系,但是
ç
Run Code Online (Sandbox Code Playgroud)
不是.转换回ISO-8859-1,将命名实体转换回字符,但还有第二个问题.imagettftext()不支持值大于> 127的字符.最终的for循环以十六进制编码这些字符.这个解决方案对我来说正在使用我正在使用的文本(包括日语,中文和葡萄牙语的重音拉丁字符),但我不是100%肯定它会在所有情况下都有效.
所有这些体操都是必需的,因为imagettftext()并不真正接受我服务器上的UTF-8字符串.
amp*_*ine 12
我一直有一个脚本的问题,它将在图像中呈现文本并输出它.问题是,由于不同的浏览器(或代码的耐寒性/偏执狂,无论你想怎么想),我都无法知道在$_GET
数组中放入了什么编码.
这是我解决问题的方法.
$item_text = $_GET['text'];
# detect if the string was passed in as unicode
$text_encoding = mb_detect_encoding($item_text, 'UTF-8, ISO-8859-1');
# make sure it's in unicode
if ($text_encoding != 'UTF-8') {
$item_text = mb_convert_encoding($item_text, 'UTF-8', $text_encoding);
}
# html numerically-escape everything (&#[dec];)
$item_text = mb_encode_numericentity($item_text,
array (0x0, 0xffff, 0, 0xffff), 'UTF-8');
Run Code Online (Sandbox Code Playgroud)
imagettftext
通过简单地将所有字符(包括多字节Unicode字符)更改为其HTML数字字符实体 - "A",这解决了无法处理#127以上字符的任何问题.为"A","B" 对于"B"等,手册页声称支持.