PHP简单生成验证码文件的类,在这个类中可定义生成验证码图片的字符数、干扰像素,设置验证码最终的高度和宽度,还有很多参数设置就不一一说明了,看代码:

<?php class CCheckCodeFile { private $mCheckCodeNum  = 4;//验证码位数 private $mCheckCode   = '';//产生的验证码 private $mCheckImage  = &#39;&#39;;//验证码的图片 private $mDisturbColor  = '';//干扰像素 private $mCheckImageWidth = &#39;80&#39;;//验证码的图片宽度 private $mCheckImageHeight  = '20';//验证码的图片宽度 /@brief   输出头/ private function OutFileHeader() {   header ("Content-type: image/png"); } /产生验证码/ private function CreateCheckCode() {   $this-&gt;mCheckCode = strtoupper(substr(md5(rand()),0,$this->mCheckCodeNum));    return $this-&gt;mCheckCode; } //@brief 产生验证码图片 private function CreateImage() {   $this->mCheckImage = @imagecreate ($this-&gt;mCheckImageWidth,$this->mCheckImageHeight);   imagecolorallocate ($this-&gt;mCheckImage, 200, 200, 200);    return $this->mCheckImage; } //@brief 设置图片的干扰像素 private function SetDisturbColor() {    for ($i=0;$i<=128;$i++)    {    $this->mDisturbColor = imagecolorallocate ($this-&gt;mCheckImage, rand(0,255), rand(0,255), rand(0,255));    imagesetpixel($this->mCheckImage,rand(2,128),rand(2,38),$this-&gt;mDisturbColor);    } }  /** * @brief   设置验证码图片的大小 * @param   $width   宽  @param   $height 高 */ public function SetCheckImageWH($width,$height) {    if($width==''||$height==&#39;&#39;)return false;   $this->mCheckImageWidth  = $width;   $this->mCheckImageHeight = $height;    return true; } //@brief 在验证码图片上逐个画上验证码 private function WriteCheckCodeToImage() {    for ($i=0;$i&lt;=$this->mCheckCodeNum;$i++)    {    $bg_color = imagecolorallocate ($this-&gt;mCheckImage, rand(0,255), rand(0,128), rand(0,255));    $x = floor($this-&gt;mCheckImageWidth/$this->mCheckCodeNum)$i;    $y = rand(0,$this-&gt;mCheckImageHeight-15);    imagechar ($this->mCheckImage, 5, $x, $y, $this-&gt;mCheckCode[$i], $bg_color);    } } //@brief   输出验证码图片 public function OutCheckImage() {   $this ->OutFileHeader();   $this -&gt;CreateCheckCode();   $this ->CreateImage();   $this -&gt;SetDisturbColor();   $this ->WriteCheckCodeToImage();   imagepng($this-&gt;mCheckImage);   imagedestroy($this->mCheckImage); } } ?>

如何使用?

$c_check_code_image = new CCheckCodeFile(); //$c_check_code_image ->SetCheckImageWH(100,50);//设置显示验证码图片的尺寸 $c_check_code_image ->OutCheckImage();

</div>