Home > Article > Backend Development > What is the reason why gd2 Chinese garbled characters appear in php?
The reasons why gd2 Chinese garbled characters appear in php: 1. The encoding of the server is different from the default encoding of the "imagettftext()" function in gd2. The default encoding of the function is UTF8; 2. The gd library does not select the correct TTF font. file, when gd2 outputs Chinese characters, the correct font file must be selected.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer.
The reason why php gd2 generates Chinese garbled characters
1. The reason for server encoding. Since the default encoding of the string parameter of the imagettftext function is UTF8, if the server encoding is inconsistent and the corresponding character encoding format conversion is not performed, it is easy for Chinese characters to become garbled when using gd2 to output images.
2. The correct TTF font file is not selected. If you want to output Chinese characters when using gd2, you need to select the correct TTF font file.
php gd2 Chinese garbled code solution
According to the principle of php gd2 generating Chinese garbled code introduced before, the method to solve php gd2 Chinese garbled code is actually very simple.
1. It is recommended that the entire site uses UTF8 encoding. If you are already using GB2312 or GBK encoding, please use iconv or a custom gb2312 and utf8 conversion function to convert character encodings. For gb2312 and utf8 conversion functions, please refer to How to solve the problem of garbled Chinese characters in PHP Ajax value transfer.
2. If you have just set up a php environment, it is recommended to change the default character set in the Apache configuration file to UTF8, that is, AddDefaultCharset UTF8.
3. If the above method does not work, please check whether you added the –enable-gd-jis-conv option when compiling gd2. This option is to allow gd2 to support Japanese-encoded fonts. Please cancel this option. and recompile. I have not verified this method. It is estimated that it is mainly used to install and configure the PHP environment under Unix. This situation generally does not occur in the Windows environment. It seems that the default PHP configuration file is commented out.
4. Another reason why Chinese garbled characters are generated when using the php gd library is that the correct TTF font is not selected. You need to choose a font that supports Chinese. Commonly used Chinese font files are simsun.ttc and simhei.ttf.
OK, as long as you follow the above method, basically using php gd2 to generate Chinese garbled characters can be solved. As long as you check carefully, it is actually very convenient to solve the Chinese garbled characters of gd2.
Recommended learning: php video tutorial
The above is the detailed content of What is the reason why gd2 Chinese garbled characters appear in php?. For more information, please follow other related articles on the PHP Chinese website!