PHP生成验证码不重复,php下生成验证码的类

session_start();

$imgWidth = isset($_REQUEST["width"]) ? intval($_REQUEST["width"]) : 0;

$imgHeight = isset($_REQUEST["height"]) ? intval($_REQUEST["height"]) : 0;

$length = isset($_REQUEST["length"]) ? intval($_REQUEST["length"]) : 0;

$captcha = new VerifyCode(array('width'=>$imgWidth,'height'=>$imgHeight,'length'=>$length));

$captcha->CreateImage();

class VerifyCode {

public $width = 140;

public $height = 60;

public $glbVerifySeed = "123456789abcdefghigklnmpqrstuvxyz";

public $strLength = 6;

public $session_var = 'verifyCode';

public $backgroundColor = array(255, 255, 255);

public $colors = array(

array(27,78,181), // blue

array(22,163,35), // green

array(214,36,7), // red

array(0,0,0), // black

);

public $PolluteNum = 100;

public $Yperiod = 10;

public $Yamplitude = 8;

public $Xperiod = 8;

public $Xamplitude = 4;

public $maxRotation = 8;

public $scale = 2;

public $imageFormat = 'png';

public $im;

public function __construct($config = array()) {

if(intval($config['width'])>50) $this->width = $config['width'];

if(intval($config['height'])>20) $this->height = $config['height'];

if(intval($config['length'])>2) $this->strLength = $config['length'];

if($this->width>150 || $this->height>50) {

$this->scale = 1;

}

$this->PolluteNum = intval($this->width*$this->height/40);

}

// 创建图片

public function CreateImage() {

$ini = microtime(true);

$this->ImageAllocate();

$text = $this->GetCaptchaText();

$_SESSION[$this->session_var] = $text;

$this->WriteText($text);// 写入内容

if($this->height>30) {

$this->WaveImage();// 图片变形

}

//$this->Pollute(); // 画出杂点

$this->Line(); // 画出一条线

$this->ReduceImage();// 缩放图片

$this->WriteImage();// 输出图片

$this->Cleanup();

}

// 初始图片参数

protected function ImageAllocate() {

if (!empty($this->im)) {

imagedestroy($this->im);

}

$this->im = imagecreatetruecolor($this->width*$this->scale, $this->height*$this->scale);

$this->GdBgColor = imagecolorallocate($this->im,

$this->backgroundColor[0],

$this->backgroundColor[1],

$this->backgroundColor[2]

);

imagefilledrectangle($this->im, 0, 0, $this->width*$this->scale, $this->height*$this->scale, $this->GdBgColor);

$color = $this->colors[mt_rand(0, sizeof($this->colors)-1)];

$this->GdFgColor = imagecolorallocate($this->im, $color[0], $color[1], $color[2]);

}

// 获取生成的字母

protected function GetCaptchaText() {

$bgnIdx = 0;

$endIdx = strlen($this->glbVerifySeed)-1;

$code = "";

for($i=0; $istrLength; $i++) {

$curPos = rand($bgnIdx, $endIdx);

$code .= substr($this->glbVerifySeed, $curPos, 1);

}

return $code;

}

// 写入字母

protected function WriteText($text) {

$fontcfg = array(

'spacing' => 0,

'minSize' => $this->height/2+2,

'maxSize' => $this->height/2+4,

'font' => 'Duality.ttf'

);

$x = 10*$this->scale;

$y = round(($this->height*27/40)*$this->scale);

$length = strlen($text);

for ($i=0; $i

$degree = rand($this->maxRotation*-1, $this->maxRotation);

$fontsize = rand($fontcfg['minSize'], $fontcfg['maxSize'])*$this->scale;

$letter = substr($text, $i, 1);

$coords = imagettftext($this->im, $fontsize, $degree,

$x, $y,

$this->GdFgColor, $fontcfg['font'], $letter);

$x += ($coords[2]-$x) + ($fontcfg['spacing']*$this->scale);

}

}

// 图片变形

protected function WaveImage() {

$xp = $this->scale*$this->Xperiod*rand(1,3);

$k = rand(0, 100);

for ($i = 0; $i < ($this->width*$this->scale); $i++) {

imagecopy($this->im, $this->im,

$i-1, sin($k+$i/$xp) * ($this->scale*$this->Xamplitude),

$i, 0, 1, $this->height*$this->scale);

}

$k = rand(0, 100);

$yp = $this->scale*$this->Yperiod*rand(1,2);

for ($i = 0; $i < ($this->height*$this->scale); $i++) {

imagecopy($this->im, $this->im,

sin($k+$i/$yp) * ($this->scale*$this->Yamplitude), $i-1,

0, $i, $this->width*$this->scale, 1);

}

}

// 缩小图片

protected function ReduceImage() {

$imResampled = imagecreatetruecolor($this->width, $this->height);

imagecopyresampled($imResampled, $this->im,

0, 0, 0, 0,

$this->width, $this->height,

$this->width*$this->scale, $this->height*$this->scale

);

imagedestroy($this->im);

$this->im = $imResampled;

}

// 输出图片

protected function WriteImage() {

if ($this->imageFormat == 'png' && function_exists('imagepng')) {

header("Content-type: image/png");

imagepng($this->im);

} else {

header("Content-type: image/jpeg");

imagejpeg($this->im, null, 80);

}

}

// 画出杂点

protected function Pollute() {

$imgWidth = imagesx($this->im);

$imgHeight = imagesy($this->im);

for($j=0; $jPolluteNum; $j++) {

$x = rand(0, $imgWidth);

$y = rand(0, $imgHeight);

imagesetpixel($this->im, $x, $y, $this->GdFgColor);

}

}

// 画出一条线

protected function Line() {

$imgWidth = imagesx($this->im);

$imgHeight = imagesy($this->im);

$y = ceil($imgHeight/2);

$border = floor($imgHeight/20);

for($j=10; $j

$x = $j;

$y = rand(-1,1)+$y;

for($i=0; $i

imagesetpixel($this->im, $x, $y+$i, $this->GdFgColor);

}

}

}

protected function Cleanup() {

imagedestroy($this->im);

}

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值