简体   繁体   中英

PHP-gd text display in Japanese is weird

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.

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.

我必须运行它才能使其正常工作

$text = mb_convert_encoding('夏の天気', "SJIS", 'UTF-8');
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM