为什么我用PHP绘制图像,浏览器总是显示乱码?

2025-04-11 17:56:40
推荐回答(3个)
回答1:

一个简单的PHP输出图像(含中文字符)的示例,画线之类的一般不会出现乱码
header('Content-type: image/png');
$image = imagecreate(310,150);
$black = imagecolorallocate($image,0,0,0);
$white = imagecolorallocate($image,255,255,255);
$font = 'include/msyhbd.ttf';//中文需要一个字体文件,这里用的是微软雅黑(粗),需要上传到网站的相应目录
imagettftext($image,10,0,0,25,$white,$font,”Hello world“);

imagettftext($image,10,0,0,55,$white,$font,”你好“);

imagepng($image);
imagedestroy($image);
?>

回答2:

设置编码方式和你的Apache的编码一样。

回答3:

输出图像前,先输出 HTTP头,Header("Content-type: image/png"); 图象输出期间请勿使用 echo 等文字输出语句。