code.class.php image = imagecreatetruecolor($this-width,$this-height); $bgColor = imagecolorallocate($this-image,255,255,255); imagefill($this-image,0,0,$bgColor); } /** * 生成验证码字符 */ private function createCode() { for ($i=0;$
image = imagecreatetruecolor($this->width,$this->height);
$bgColor = imagecolorallocate($this->image,255,255,255);
imagefill($this->image,0,0,$bgColor);
}
/**
* 生成验证码字符
*/
private function createCode()
{
for ($i=0;$i<$this->codeLeng;$i++)
{
$fontContent = $this->codeString[rand(0, 35)];
$this->code .= $fontContent;
$fontColor = imagecolorallocate($this->image, rand(0, 120), rand(0, 120), rand(0, 120));
$x = ($i * $this->width / $this->codeLeng) + rand(10, 10);
$y = rand(10, 10);
imagestring($this->image, $this->fontSize, $x, $y, $fontContent, $fontColor);
}
$_SESSION['code'] = $this->code;
}
/**
* 添加圆点干扰项
*/
private function setPixel()
{
for ($i=0;$i<200;$i++)
{
$pixelColor = imagecolorallocate($this->image,rand(100,200),rand(100,200),rand(100,200));
imagesetpixel($this->image,rand(0,$this->width-1),rand(1,$this->height-1),$pixelColor);
}
}
/**
* 线性干扰
*/
private function setLine()
{
for ($i=0;$i<20;$i++)
{
$linelColor = imagecolorallocate($this->image,rand(100,200),rand(100,200),rand(100,200));
imageline($this->image,rand(0,$this->width-1),rand(1,$this->height-1),rand(0,$this->width-1),rand(1,$this->height-1),$linelColor);
}
}
/**
* 输出验证码
*
*/
private function outPut()
{
header('content-type: image/png');
imagepng($this->image);
$this->destroyImage();
}
/**
* 显示验证码
*/
public function showCode()
{
$this->createImage();
$this->setPixel();
$this->setLine();
$this->createCode();
$this->outPut();
}
/**
* 销毁图片资源
*/
private function destroyImage()
{
imagedestroy($this->image);
}
}
