您的位置:首页 > 其它

验证码图片类的编写

2019-12-22 18:17 1221 查看

1.配置文件config.php

<?php

/**
* 验证码类型 codeType int 0:纯数字 1:纯字符串 2:数字和字符串混合
* 验证码长度 length   int
* 图片宽度  width int
* 图片高度 height int
*/
return
[
'codeType' => 2,
'length' => 5,
'width' => 400,
'height' => 200,
];
config.php

2.生成验证码类

<?php
//定义验证码类
class Code{
private $length;    //验证码长度
private $codeType;  //类型
private $code;      //验证码
private $width;     //宽度
private $height;    //高度
private $img;       //图片资源

public function __construct()
{
//引入配置文件
$this->config = require_once './config.php';
$this->length = $this->config['length'];
$this->codeType = $this->config['codeType'];
$this->width = $this->config['width'];
$this->height = $this->config['height'];
$this->createCode();
}

protected function createCode()
{
switch($this->codeType){
case 0:
$this->code = $this->getNumberCode();
break;
case 1:
$this->code = $this->getCharCode();
break;
case 2:
$this->code = $this->getNumCharCode();
break;
default:
die('验证码类型错误,请重新输入!');
break;
}
}

public function getCode()
{
return $this->code;
}

private function getNumberCode()
{
$number = join('',range(0,9));
return $this->setCode($number);
}

private function getCharCode()
{
$str = join('',range('a','z'));
$str .= strtoUpper($str);
return $this->setCode($str);
}

private function getNumCharCode()
{
$number = join('',range(0,9));
$str = join('',range('a','z'));
$code = strtoUpper($str).$str.$number;
return $this->setCode($code);
}

private function setCode($string)
{
return substr(str_shuffle($string),0,$this->length);
}

//输出图像
public function getImg()
{
//新建画布
$this->createImg();
//画布填充背景色
$this->fillBackground();
//将验证码写入画布
$this->fillCode();
//加入干扰点
$this->setDistubPoint();
//设置干扰线
$this->setDisEarc();
//显示图像
$this->showImg();
}

protected function createImg()
{
$this->img = imagecreatetruecolor($this->width,$this->height);
}

protected function fillBackground()
{
imagefill($this->img,0,0,$this->lightColor());
}

private function lightColor()
{
return imagecolorallocate($this->img,mt_rand(130,255),mt_rand(130,255),mt_rand(130,255));
}

private function darkColor()
{
return imagecolorallocate($this->img,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));
}

protected function fillCode()
{
$width = ceil($this->width/$this->length);
$height = $this->height/2;
for($i=0;$i<$this->length;$i++){
$x = mt_rand($i*$width+10,($i+1)*$width-10);
$y = mt_rand($height-10,$height+10);
imagechar($this->img,5,$x,$y,$this->code[$i],$this->darkColor());
}
}

//设置干扰点
protected function setDistubPoint()
{
for($i=0;$i<1000;$i++){
$x = mt_rand(0,$this->width);
$y = mt_rand(0,$this->height);
imagesetpixel($this->img,$x,$y,$this->darkColor());
}
}

//设置干扰线段
protected function setDisEarc()
{
for($i=0;$i<2;$i++){
imagearc ( $this->img ,  rand(0,$this->width) ,  rand(0,$this->height) ,  rand($this->width,$this->width*2) ,  rand($this->height,$this->height*2) ,  rand(0,100) ,  rand(280,270) ,  $this->darkColor() );
}
}

protected function showImg()
{
header('Content-type:image/png');
imagepng($this->img);
}
}

$code = new Code();

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