最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > 建站教程 > php教程 >

php 生成验证码图片不显示问题

时间:2014-06-09 16:40来源: 作者: 点击: 次
今天在做一个php验证码程序时发现生成出来的图片不显示,开始以为是php gd库未打开,查用phpinfo查了是可以打开的啊,下面小编来给大家介绍此问题解决办法。 清除了bom,代码也是顶行开始写的,

今天在做一个php验证码程序时发现生成出来的图片不显示,开始以为是php gd库未打开,查用phpinfo查了是可以打开的啊,下面小编来给大家介绍此问题解决办法。

清除了bom,代码也是顶行开始写的,gd库也是开启的,从这里来看估计不是gd库的问题了,可能出在程序那句代码上。

生成验证码的代码:

  1. <?php 
  2. /* 
  3. * 验证码产生程序 
  4. */ 
  5. $letter = ''; 
  6. //获取随机数字 
  7. for ($i=0; $i<2; $i++) { 
  8.     $letter .= chr(mt_rand(48,57)); 
  9. } 
  10. //获取随机字母 
  11. for ($i=0; $i<2; $i++) { 
  12.     $letter .= chr(mt_rand(65,90)); 
  13. } 
  14. //重构字符顺序 
  15. $strs = str_split($letter); 
  16. shuffle ($strs); 
  17. $rndstring = ""; 
  18. while (list ( , $str) = each ($strs)) { 
  19.     $rndstring .= $str; 
  20. } 
  21.  
  22. //如果支持GD,则绘图 
  23. if(function_exists("imagecreate")) 
  24. { 
  25.     //向浏览器写入cookie 
  26.     setcookie("zjs_ckstr", md5( strtolower($rndstring) ), time()+300,'/');//验证码有效期5分钟     
  27.     $rndcodelen = strlen($rndstring); 
  28.     //图片大小 
  29.     $im = imagecreate(100,30); 
  30.     //$im = imagecreatefromgif("code.gif"); 
  31.     //字体 
  32.     $font_type = dirname(dirname(__FILE__))."/data/font/AvantGardeBookBT.ttf"; 
  33.     //背景颜色 
  34.     $backcolor = imagecolorallocate($im,255,255,255);    
  35.     //字体色 
  36.     //不支持 imagettftext 
  37.     $fontColor = ImageColorAllocate($im, 0,0,0); 
  38.     //支持 imagettftext 
  39.     $fontColor2 = ImageColorAllocate($im, 0,0,0); 
  40.     //阴影 
  41.     $fontColor1 = ImageColorAllocate($im, 255,255,25); 
  42.     //添加背景杂点 
  43.     $pixColor = imagecolorallocate($im, 199, 199, 199);//杂点颜色 
  44.     for($j=0; $j<1000; $j++){ 
  45.         imagesetpixel($im, rand(0,100), rand(0,30), $pixColor); 
  46.     } 
  47.     //添加背景线 
  48.     for($j=0; $j<=3; $j++){ 
  49.         //背景线颜色 
  50.         $lineColor1 = ImageColorAllocate($im, rand(0, 255),rand(0, 255),rand(0, 255)); 
  51.         //背景线方向大小 
  52.         imageline($im,rand(0,40),rand(3,25),rand(40,88),rand(3,25),$lineColor1); 
  53.     }    
  54.  
  55.     $strposs = array(); 
  56.     //文字 
  57.     for($i=0;$i<$rndcodelen;$i++){ 
  58.       if(function_exists("imagettftext")){ 
  59.           $strposs[$i][0] = $i*16+17;//x轴 
  60.           $strposs[$i][1] = mt_rand(20,23);//y轴 
  61.           imagettftext($im, 5, 5, $strposs[$i][0]+1, $strposs[$i][1]+1, $fontColor1, $font_type, $rndstring[$i]); 
  62.       } else{ 
  63.           imagestring($im, 5, $i*16+7, mt_rand(2, 4), $rndstring[$i], $fontColor); 
  64.       } 
  65.     } 
  66.     //文字 
  67.     for($i=0;$i<$rndcodelen;$i++){ 
  68.       if(function_exists("imagettftext")){ 
  69.           imagettftext($im, 16,5, $strposs[$i][0]-1, $strposs[$i][1]-1, $fontColor2, $font_type, $rndstring[$i]); 
  70.       } 
  71.     } 
  72.   header("Pragma:no-cachern"); 
  73.   header("Cache-Control:no-cachern"); 
  74.   header("Expires:0rn"); 
  75.   //输出特定类型的图片格式,优先级为 gif -> jpg 
  76.   if(function_exists("imagegif")){ 
  77.         header("content-type:image/gifrn"); 
  78.       imagegif($im); 
  79.   }else{ 
  80.         header("content-type:image/jpegrn"); 
  81.       imagejpeg($im); 
  82.   } 
  83.   ImageDestroy($im); 
  84. } 
  85. ?> 

感觉是不是没有问题了,后来百度发现一高人说关键是加入了ob_clean,了这个让我想了原因。

解决办法:ob_clean();关键代码,防止出现'图像因其本身有错无法显示'的问题,加到 header 输出之前,代码如下:header('Content-Type: image/png');

(责任编辑:admin)
------分隔线----------------------------
栏目列表
推荐内容