本文操作环境:windows7系统、php7.1版、dell g3电脑
如何解决php输出图片并显示中文乱码问题?
php使用gd图像库绘制输出图像出现乱码问题和图片上输出中文出现乱码问题解决方法
源码:
<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>
运行后得到的结果:
<html> <head> <title>php ͼ������</title> </head> <body> �pngihdr���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)解决方法:在$im = @imagecreate(500,500) or die("没有安装gd图像库<br>");前面加上ob_clean();先清除缓冲区。即可显示图片,但图片上的文字只能显示英文和数字,中文会出现乱码。
(2)imagestring 默认英文编码,只支持utf-8,所以中文会出现乱码,应采用
imagettftext($im,10,30,0,100,$texcol,"c:/windows/fonts/simhei.ttf",$motto);
c:/windows/fonts/simhei.ttf,系统自带的黑体。
推荐学习:《php视频教程》
以上就是如何解决php输出图片并显示中文乱码问题的详细内容。
