原生框架好像都会遇到这个问题,今天找了一下相关资料和度娘,发现是输出缓冲区问题,只要用ob_clean()这个函数丢弃一下缓冲区就可以正常显示了。
贴代码:

<?php// 1.创建画布资源$img=imagecreatetruecolor(150,50);
// 2.准备颜色$black=imagecolorallocate($img,0,0,0);
$white=imagecolorallocate($img,255,255,255);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,180,180,180);
// 3.填充画布
imagefill($img,0,0,$black);
$arr=array_merge(range(0,9),range(a,z),range(A,Z));
shuffle($arr);
$str=join(' ',array_slice($arr,0,4));
//画字
imagettftext($img,20,0,20,35,$white,'ms.ttf',$str);
//干扰素for($i=0;$i<30;$i++){
    imagearc($img,mt_rand(0,150),mt_rand(0,50),mt_rand(0,150),mt_rand(0,50),mt_rand(0,360),mt_rand(0,360),$white);
}
// 丢弃输出缓冲区内容
ob_clean();
// 5.输出最终图像或保存最终图像
header('content-type:image/png');
// 图片从浏览器上输出
imagepng($img);
// 把图片保存到本地// imagejpeg($img,'jin.jpg');// 6.释放画布资源
imagedestroy($img);
 ?>