12

我正在使用 PHP 函数 imagettftext() 将文本转换为 GIF 图像。我正在转换的文本包含 Unicode 字符,包括日语。在我的本地机器(Ubuntu 7.10)上一切正常,但在我的虚拟主机服务器上,日语字符被破坏了。什么可能导致差异?一切都应该编码为 UTF-8。

虚拟主机服务器上的损坏图像:http: //www.ibeni.net/flashcards/imagetest.php

从我的本地机器复制正确的图像:http: //www.ibeni.net/flashcards/imagetest.php.gif

从我的本地机器复制 phpinfo():http: //www.ibeni.net/flashcards/phpinfo.php.html

从我的虚拟主机服务器复制 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); 
4

5 回答 5

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;
    }
}

除了函数 imagettftext() 不接受命名实体之外,将字符串转换为 HTML 实体是可行的。例如,

&#26085;&#26412;&#35486;

可以,但是

&ccedil;

不是。转换回 ISO-8859-1,将命名实体转换回字符,但还有第二个问题。imagettftext() 不支持值大于 >127 的字符。最后的 for 循环将这些字符编码为十六进制。这个解决方案适用于我正在使用的文本(包括日语、中文和葡萄牙语的重音拉丁字符),但我不能 100% 确定它是否适用于所有情况。

所有这些体操都是必需的,因为 imagettftext() 在我的服务器上并不真正接受 UTF-8 字符串。

于 2008-10-14T16:02:47.710 回答
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');

这解决了imagettftext无法处理 #127 以上字符的任何问题,只需将所有字符(包括多字节 Unicode 字符)更改为它们的 HTML 数字字符实体——“A” 对于“A”、“B” 对于“B”等——手册页声称支持。

于 2009-12-24T02:23:40.370 回答
4

我有同样的问题。将字体从 otf 转换为 ttf 有帮助。您可以使用 FontForge(标准存储库中提供)进行转换。

于 2010-10-13T08:54:56.627 回答
0

我的主要嫌疑人是您用于渲染的字体。

根据http://fr3.php.net/imagettftext,php 使用的不同版本的 GD 库可以显示不同的行为。

  • 本地机器上的 GD 版本:2.0 或更高版本
  • 您的虚拟主机服务器上的 GD 版本:捆绑(与 2.0.34 兼容)

编辑: 另一个想法:你能验证它$text = '日本語';真的像这样保存在你的生产服务器上吗?您的脚本可能存在编码问题。

下一次编辑: BKB 已经提出了这一点。因此,如果这是原因:他首先得到了答案;-)

于 2008-10-13T15:48:50.953 回答
-1

您的生产机器上是否存在该特定字体文件?如果使用 FTP 上传文件,您是否使用二进制编码?

于 2008-10-13T17:10:59.710 回答