Home > Backend Development > PHP Problem > How to solve the problem of php outputting pictures and displaying Chinese garbled characters

How to solve the problem of php outputting pictures and displaying Chinese garbled characters

藏色散人
Release: 2023-03-13 13:12:01
Original
3596 people have browsed it

Solution to php outputting pictures and displaying Chinese garbled characters: 1. Add ob_clean() before the PHP code to clear the buffer; 2. Set UTF-8 encoding.

How to solve the problem of php outputting pictures and displaying Chinese garbled characters

The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer

How to solve the problem of php output pictures And the problem of Chinese garbled characters is displayed?

php uses the GD image library to draw the output image and the problem of garbled characters and the solution to the problem of garbled Chinese characters on the picture

Source code:

<html>
 <head>
  <title>PHP 图像测试</title>
 </head>
 <body>
 <?php 
        #创建背景图像
$im = @imagecreate(500,500) or die("没有安装GD图像库<br>");
#设置背景颜色
        $bgCol=imagecolorallocate($im,255,0,0);
#设置字体颜色
$texCol=imagecolorallocate($im,255,255,0);
$motto = "I love my baby! 我家宝贝聪明可爱漂亮";
        $motto = iconv("gb2312", "utf-8", $motto);
#在背景图像上输入文字
imagestring($im,3,5,5,$motto,$texCol);
#输出图像
header("Content-Type: image/png");
imagepng($im);
#清除所有资源
imagedestroy($im);
 ?>
</body>
</html>
Copy after login

Results obtained after running :

 <html>
 <head>
  <title>PHP ͼ������</title>
 </head>
 <body>
 �PNG


IHDR���M�PLTE���lۜ�IDATx���1
�0�ᔀ]*��W�:/�ر��C����ɥ��1N���?^������M��c��N���>t1�+�w�1
�Z۷�+��<��7���y�ݏ�.�}�T�ݧ_��?�/o�m��IEND�B`�</body>
</html>
Copy after login

(1) Solution: Add ob_clean(); in front of $im = @imagecreate(500,500) or die("GD image library is not installed
"); to clear the buffer first . The picture can be displayed, but the text on the picture can only display English and numbers, and Chinese characters will be garbled.

(2) The default English encoding of imagestring only supports UTF-8, so Chinese characters will be garbled. You should use

imagettftext($im,10,30,0,100,$texCol,"c:/windows/fonts/simhei.ttf",$motto);
Copy after login

"c:/windows/fonts/simhei.ttf", the system automatically With black body.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to solve the problem of php outputting pictures and displaying Chinese garbled characters. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template