PHP 函数 imagettftext() 和 unicode

PHP function imagettftext() and unicode(PHP 函数 imagettftext() 和 unicode)
本文介绍了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,

&#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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)