您的位置:首页 > 编程语言 > PHP开发

一个强大的PHP验证码

2012-03-29 09:30 120 查看
来源:网络

authcode.class.php
<?php

//验证码 类

class Vcode{

private $width; //验证图片宽度,程序自动计算

private $height; //验证图片高度

private $length; //验证码长度

private $bgColor; //验证图片背景色

private $fontColor; //验证码颜色

private $dotNoise; //噪点数量

private $lineNoise; //干扰线数量

private $im; //GD图像操作资源

public function Vcode()

{

$this->dotNoise = 20; //初始化噪点数量

$this->lineNoise = 2; //初始化干扰线数量

}

//设置长度

public function setLength($length)

{

$this->length = $length;

}

//设置背景色

public function setBgColor($bgColor)

{

$this->bgColor = sscanf($bgColor, '#%2x%2x%2x');

}

//设置前景色

public function setFontColor($fontColor)

{

$this->fontColor = sscanf($fontColor, '#%2x%2x%2x');

}

//手动设置噪点数量后,会覆盖初始设置

public function setDotNoise($num)

{

$this->dotNoise = $num;

}

//手动设置干扰线数量后,会覆盖初始设置

public function setLineNoise($num)

{

$this->lineNoise = $num;

}

//生成随机字符串

private function randString()

{

$string = strtoupper(md5(microtime().mt_rand(0,9)));

return substr($string, 0, $this->length);

}

//根据制定的数量随机画噪点,噪点颜色也随机

private function drawDot()

{

for($i=0; $i<$this->dotNoise; $i++)

{

$color = imagecolorallocate($this->im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //生成随机颜色

imagesetpixel($this->im, mt_rand(0, $this->width), mt_rand(0, $this->height), $color); //在随机生成的坐标上画噪点

}

}

//随机颜色随机画干扰线

private function drawLine()

{

for($i=0; $i<$this->lineNoise; $i++)

{

$color = imagecolorallocate($this->im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //随机生成颜色

imageline($this->im, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color); //在随机生成的坐标上画干扰线

}

}

//设计验证码的图

public function paint()

{

if(empty($this->length)) $this->length = 4; //验证码默认长度为4

$this->width = $this->length*12+4 ; //计算验证图片宽度

$this->height = 20; //制定验证码图片高度

$this->im = imagecreate($this->width, $this->height); //创建画布

if(empty($this->bgColor) || empty($this->fontColor))

{

//如果没有设置前景色和背景色则全部随机

//避免前景色和背景色过于接近,背景色(0-130)的随机范围与前景色(131-255)分开

imagecolorallocate($this->im, mt_rand(0, 130), mt_rand(0, 130), mt_rand(0, 130));

$randString = $this->randString();

for($i=0; $i<$this->length; $i++)

{

$fontColor = imagecolorallocate($this->im, mt_rand(131, 255), mt_rand(131, 255), mt_rand(131, 255));

imagestring($this->im, 3,$i*10+8,mt_rand(0,8),

$randString{$i},

$fontColor); //单个验证码字符高度随机,避免被OCR

}

}

else

{

//如果设置了背景色和前景色,则不使用随机颜色

imagecolorallocate

(

$this->im,

$this->bgColor[0],

$this->bgColor[1],

$this->bgColor[2]

);

$randString = $this->randString();

$fontColor = imagecolorallocate

(

$this->im,

$this->fontColor[0],

$this->fontColor[1],

$this->fontColor[2]

);

for($i=0;$i<$this->length;$i++)

{

imagestring($this->im, 3,

$i*10+8,

mt_rand(0,8),

$randString{$i},

$fontColor);//每个验证码字符高度仍然随机

}

}

$this->drawDot();//绘制噪点

$this->drawLine();//绘制干扰线

imagepng($this->im);

imagedestroy($this->im);

return md5($randString);//返回MD5加密后的验证码,可直接放入session

}

}

authcode.php
<?php
//生成验证码()
require_once 'authcode.class.php';
header("Content-Type:image/png");

session_start();

$vcode = new Vcode();

$vcode->setLength(5);

$_SESSION['vcode'] = $vcode->paint();

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: