background image

          imagettftext($this->image,$fontsize,rand(-30,   30),$x,$y   ,$fontcolor,   $fontFace,   $this-
>checkCode{$i});
    }
   }
  }
  private function outputImage() {
   if(imagetypes() & IMG_GIF){
    header("Content-Type:image/gif");
    imagepng($this->image);
   }else if(imagetypes() & IMG_JPG){
    header("Content-Type:image/jpeg");
    imagepng($this->image);
   }else if(imagetypes() & IMG_PNG){
    header("Content-Type:image/png");
    imagepng($this->image);
   }else if(imagetypes() & IMG_WBMP){
    header("Content-Type:image/vnd.wap.wbmp");
    imagepng($this->image);
   }else{
    die("PHP 不支持图像创建");
   }
  }
  function __destruct(){
   imagedestroy($this->image);
  }
 }
 
使用如下:
测试,调用验证码类
code.php
<?php
session_start();
include "validationcode.class.php";
$code=new ValidationCode(80, 20, 4);
$code->showImage();   //输出到页面中供

 注册或登录使用

$_SESSION["code"]=$code->getCheckCode();  //将验证码保存到服务器中