PHP-gd text display in Japanese is weird

流过昼夜 提交于 2019-12-25 09:36:16

问题


Finally I had to recompile PHP with --enable-gd-jis-conv. However the text display is wrong, in Japanese.

$text = '夏の天気';
$fontfile = '/usr/share/fonts/japanese/TrueType/sazanami-mincho.ttf';

return imagettftext ($image, $size,  $angle,  $x,  $y,  $color, $fontfile, $text);

But different kanji (Japanese characters) are displayed instead (very different, looks like Chinese).

Could it be an encoding issue?

Using PHP 5.3.3 on RHEL 5.4.


回答1:


Well, japanese as a multibyte encoded language has quite a few quirks. First of all, be sure that your server has the mbstring module installed.

Second, to reduce the chances of possible breakage midway, try to keep all encodings in your site/project consistent: site views and source files should ideally be written with the same encoding.

Specifically for your problem, you might want to try using the following functions:

mb_http_input http://www.php.net/manual/en/function.mb-http-input.php

This one will make sure your HTTP input is correctly encoded (ie. form data).

mb_ internal_ encoding http://www.php.net/manual/en/function.mb-internal-encoding.php

Sets the internal encoding used by PHP.

mb_regex_encoding http://www.php.net/manual/en/function.mb-regex-encoding.php

Sets the encoding used by PHP for regexes.

mb_convert_encoding http://www.php.net/manual/en/function.mb-convert-encoding.php

For String conversion.

mb_convert_variables http://www.php.net/manual/en/function.mb-convert-variables.php

Converts encodings of a whole batch of strings/arrays.

Edit: besides, from the name of the module, you might want to try feeding JIS encoded data to the function.




回答2:


I had to run this to get it to work

$text = mb_convert_encoding('夏の天気', "SJIS", 'UTF-8');



回答3:


imagettftext($this->im, 58, 0, 50, 100, $text_color, $font, mb_convert_encoding('佳人', 'UTF8', 'UTF-8'));

This worked for me. Seems to work accross a few different Japanese fonts.



来源:https://stackoverflow.com/questions/3697626/php-gd-text-display-in-japanese-is-weird

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!