因其本身有错 无法显示GD库

就因为在输出图片少打  ob_clean(); 各种坑爹有木有!!

 //关键代码,防止出现'图像因其本身有错无法显示'的问题。 

现在还碰到一种新情况:代码的编码格式,编码格式转成以UTF-8 无BOM格式编码 解决的这一显示问题

 

 1 <?php 
 2 session_start(); 
 3 session_register("login_check_number");   
 4 //先成生背景,再把生成的验证码放上去 
 5 $img_height=70;//先定义图片的长、宽 
 6 $img_width=25; 
 7 $authnum=''; 
 8 //生产验证码字符 
 9 $ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"; 
10 $list=explode(",",$ychar); 
11 for($i=0;$i<4;$i++){ 
12     $randnum=rand(0,35); 
13     $authnum.=$list[$randnum]; 
14 } 
15 //把验证码字符保存到session 
16 $_SESSION["login_check_number"] = $authnum;   
17  
18 $aimg = imagecreate($img_height,$img_width);    //生成图片 
19 imagecolorallocate($aimg, 255,255,255);            //图片底色,ImageColorAllocate第1次定义颜色PHP就认为是底色了 
20 $black = imagecolorallocate($aimg, 0,0,0);        //定义需要的黑色 
21  
22 for ($i=1; $i<=100; $i++) { 
23     imagestring($aimg,1,mt_rand(1,$img_height),mt_rand(1,$img_width),"@",imagecolorallocate($aimg,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255))); 
24 } 
25  
26 //为了区别于背景,这里的颜色不超过200,上面的不小于200 
27 for ($i=0;$i<strlen($authnum);$i++){ 
28     imagestring($aimg, mt_rand(3,5),$i*$img_height/4+mt_rand(2,7),mt_rand(1,$img_width/2-2), $authnum[$i],imagecolorallocate($aimg,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200))); 
29 } 
30 imagerectangle($aimg,0,0,$img_height-1,$img_width-1,$black);//画一个矩形 
31 ob_clean();  //关键代码,防止出现'图像因其本身有错无法显示'的问题。
32 Header("Content-type: image/PNG"); 
33 ImagePNG($aimg);//生成png格式 
34 ImageDestroy($aimg); 
35 ?> 


推荐参考:gd应用之--无损缩略:http://www.cnblogs.com/as9203/archive/2012/07/27/2611159.html
原文地址:https://www.cnblogs.com/logon/p/logon.html