本文介绍了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.

Web主机服务器上的图像损坏: 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

从我的Web主机服务器复制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,

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

可以,但是

&ccedil;

不是.转换回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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-31 17:09