wamp
今天想写一个关于验证码的代码,但是一直遇到超时问题,
如下我写到干扰像素时测试的时候的代码:
<?phpclass yanzheng{
  private $width;
  private $height;
  private $codenb;
 function __construct($width=80,$height=20,$codenunb=4)
 {
    $this->width=$width;
    $this->height=$height;
    $this->codenumb=$codenb;
  
 }
 //通过这个方法,向浏览器输出验证码图像
 function showimage()
 {
 //第一步:创建图像背景
 $this->createimage();
 //第二步:设置干扰元素
 $this->ganraoyanse();
 //第三步:向图像中随机画出文本
 $this->shuchuwenben();
 //第四步:输出图像
 $this->shuchutuxiang();
  
 }
 private function  createimage()
 {
  $this->image=imagecreatetruecolor($this->width,$this->height);
  //随机背景色
  $bacolor=imagecolorallocate($this->image, rand(225,255), rand(225,255),rand(225,255));
  //为beijing填充颜色
  imagefill($this->image,0, 0, $bacolor);
  //设置边框颜色
  $kuang=imagecolorallocate($this->image,0, 0,0);
  //画出矩矩形边框
  imagerectangle($this->image, 0, 0, $this->width-1,$this->height-1, $kuang);
 }
 
 private function ganraoyanse()
 {
 
  //循环50次,位置随机
  for($i=0;i<=50;$i++){
   $yanse=imagecolorallocate($this->image,rand(0, 255),rand(0, 255), rand(0, 255));
  imagesetpixel($this->image, rand(1,$this->width-2), rand(1,$this->height-2), $yanse);
 
  } 
  
 }
 
 private function shuchuwenben()
 {
  
  
 }
 
 private function shuchutuxiang()
 {
  
  if(imagetypes() & IMG_GIF)
  { 
   header("Content-Type:image/gif");
   imagegif($this->image);
  }
  elseif (imagetypes()& IMG_PNG)
  {
  header("Content-Type:image/png");
  imagepng($this->image);
  }
  elseif(imagetypes() & IMG_JPEG)
  {
   
   header("Content-Type:image/jpeg");
   imagejpeg($this->image);
  }
  elseif(imagetypes() & IMG_WBMP)
  {
   
   header("Content-Type:image/vnd.wap.wbmp");
   imagewbmp($this->image);
  }
  else {
   die("php不支持图像创建");
  }
  
 }
 
 //通过调用这个方法获取随即创建的验证码字符串
 
 function getcode()
 {
  
 }
 
 
 function __destruct()
 {
  imagedestroy($this->image);
  
 }
}  
?>在另外一个php文件中引用这个类,运行出现
Fatal error: Maximum execution time of 60 seconds exceeded in E:\TDDOWNLOAD\PHP\lamp\apache\htdocs\zixue\yanzhengma.class.php on line 46
,第46行是那个for循环,超时了,怎么办,完全没办法了,知道的帮帮忙啊,谢谢 。

解决方案 »

  1.   


    <?php
    //生成验证码图片
    Header("Content-type: image/PNG");
    srand((double)microtime()*1000000);//播下一个生成随机数字的种子,以方便下面随机数生成的使用
    //session_start();//将随机数存入session中
    $_SESSION['authnum']="";
    $im = imagecreate(90,20) or die("Cant's initialize new GD image stream!");  //制定图片背景大小
    $red = ImageColorAllocate($im, 255,0,0); //设定三种颜色
    $white = ImageColorAllocate($im, 255,255,255);
    $gray = ImageColorAllocate($im, 200,200,200);
    //imagefill($im,0,0,$gray); //采用区域填充法,设定(0,0)
    imagefill($im,0,0,$white);//ed
    //生成数字和字母混合的验证码方法
    $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";
    $list=explode(",",$ychar);
    for($i=0;$i<4;$i++){
      $randnum=rand(0,35);
      $authnum.=$list[$randnum]." ";//ed 加入一个空格
    }
    //while(($authnum=rand()%100000)<10000); //生成随机的四位数
    //将四位整数验证码绘入图片
    $_SESSION['authnum']=$authnum;
    //int imagestring(resource image,int font,int x,int y,string s, int col)
    imagestring($im, 5, 10, 3, $authnum, $red);
    //用col颜色将字符串s画到image所代表的图像的x,y座标处(图像的左上角为0,0)。
    //如果 font 是 1,2,3,4 或 5,则使用内置字体for($i=0;$i<400;$i++){ //加入干扰象素 {
    $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
    // imagesetpixel($im, rand()%90 , rand()%30 , $randcolor);
    imagesetpixel($im, rand()%90 , rand()%30 , $gray);
    }
    ImagePNG($im);
    ImageDestroy($im);
    ?>
    好吧。我只是路过学习。把我之前用的发你参考下。