欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php5验证码类(简易实用型)

程序员文章站 2022-06-14 14:58:48
...
为大家介绍一个php验证码类,简易实用型的,有需要的朋友,可以参考下。

PHP验证码类的代码。

mCheckCode = strtoupper(substr(md5(rand()),0,$this->mCheckCodeNum));
   return $this->mCheckCode;
}

/**
*
* @brief   产生验证码图片
*
*/
private function CreateImage()
{
   $this->mCheckImage = @imagecreate ($this->mCheckImageWidth,$this->mCheckImageHeight);
   imagecolorallocate ($this->mCheckImage, 200, 200, 200);
   return $this->mCheckImage;
}

/**
*
* @brief   设置图片的干扰像素
*
*/
private function SetDisturbColor()
{
   for ($i=0;$imDisturbColor = imagecolorallocate ($this->mCheckImage, rand(0,255), rand(0,255), rand(0,255));
    imagesetpixel($this->mCheckImage,rand(2,128),rand(2,38),$this->mDisturbColor);
   }
}

/**
*
* @brief   设置验证码图片的大小
*
* @param   $width   宽
*
* @param   $height 高 
*
*/
public function SetCheckImageWH($width,$height)
{
   if($width==''||$height=='')return false;
   $this->mCheckImageWidth   = $width;
   $this->mCheckImageHeight = $height;
   return true;
}

/**
*
* @brief   在验证码图片上逐个画上验证码
*
*/
private function WriteCheckCodeToImage()
{
   for ($i=0;$imCheckCodeNum;$i++)
   {
    $bg_color = imagecolorallocate ($this->mCheckImage, rand(0,255), rand(0,128), rand(0,255));
    $x = floor($this->mCheckImageWidth/$this->mCheckCodeNum)*$i;
    $y = rand(0,$this->mCheckImageHeight-15);
    imagechar ($this->mCheckImage, 5, $x, $y, $this->mCheckCode[$i], $bg_color);
   }
}

/**
*
* @brief   输出验证码图片
*
*/
public function OutCheckImage()
{
   $this ->OutFileHeader();
   $this ->CreateCheckCode();
   $this ->CreateImage();
   $this ->SetDisturbColor();
   $this ->WriteCheckCodeToImage();
   imagepng($this->mCheckImage);
   imagedestroy($this->mCheckImage);
}
}
$c_check_code_image = new CCheckCodeFile();
//$c_check_code_image ->SetCheckImageWH(100,50);//设置显示验证码图片的尺寸
$c_check_code_image ->OutCheckImage();
?>