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.
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>
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>
(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);
"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!