<bdo id='YnxDc'></bdo><ul id='YnxDc'></ul>

    1. <tfoot id='YnxDc'></tfoot>

    2. <i id='YnxDc'><tr id='YnxDc'><dt id='YnxDc'><q id='YnxDc'><span id='YnxDc'><b id='YnxDc'><form id='YnxDc'><ins id='YnxDc'></ins><ul id='YnxDc'></ul><sub id='YnxDc'></sub></form><legend id='YnxDc'></legend><bdo id='YnxDc'><pre id='YnxDc'><center id='YnxDc'></center></pre></bdo></b><th id='YnxDc'></th></span></q></dt></tr></i><div id='YnxDc'><tfoot id='YnxDc'></tfoot><dl id='YnxDc'><fieldset id='YnxDc'></fieldset></dl></div>

      <small id='YnxDc'></small><noframes id='YnxDc'>

      <legend id='YnxDc'><style id='YnxDc'><dir id='YnxDc'><q id='YnxDc'></q></dir></style></legend>

      PHP 函数 imagettftext() 和 unicode

      时间:2023-09-22

        • <small id='lJqeq'></small><noframes id='lJqeq'>

        • <tfoot id='lJqeq'></tfoot>

          <legend id='lJqeq'><style id='lJqeq'><dir id='lJqeq'><q id='lJqeq'></q></dir></style></legend>

          • <bdo id='lJqeq'></bdo><ul id='lJqeq'></ul>
              <tbody id='lJqeq'></tbody>
            <i id='lJqeq'><tr id='lJqeq'><dt id='lJqeq'><q id='lJqeq'><span id='lJqeq'><b id='lJqeq'><form id='lJqeq'><ins id='lJqeq'></ins><ul id='lJqeq'></ul><sub id='lJqeq'></sub></form><legend id='lJqeq'></legend><bdo id='lJqeq'><pre id='lJqeq'><center id='lJqeq'></center></pre></bdo></b><th id='lJqeq'></th></span></q></dt></tr></i><div id='lJqeq'><tfoot id='lJqeq'></tfoot><dl id='lJqeq'><fieldset id='lJqeq'></fieldset></dl></div>
              1. 本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:什么是标准化的 UTF-8? 下一篇:如何在 PHP 中打开名称中包含 unicode 字符的文件

                相关文章

                最新文章

              2. <small id='N55UL'></small><noframes id='N55UL'>

                <i id='N55UL'><tr id='N55UL'><dt id='N55UL'><q id='N55UL'><span id='N55UL'><b id='N55UL'><form id='N55UL'><ins id='N55UL'></ins><ul id='N55UL'></ul><sub id='N55UL'></sub></form><legend id='N55UL'></legend><bdo id='N55UL'><pre id='N55UL'><center id='N55UL'></center></pre></bdo></b><th id='N55UL'></th></span></q></dt></tr></i><div id='N55UL'><tfoot id='N55UL'></tfoot><dl id='N55UL'><fieldset id='N55UL'></fieldset></dl></div>
                  <bdo id='N55UL'></bdo><ul id='N55UL'></ul>
                <legend id='N55UL'><style id='N55UL'><dir id='N55UL'><q id='N55UL'></q></dir></style></legend>

                  <tfoot id='N55UL'></tfoot>