一个验证码生成类
2019-06-17 本文已影响0人
胡乱唱歌ing
1.定义验证码字符数,生成验证码图片的宽高
public $length = 4; //默认输出4个字符
public $x = 120; //画布长度
public $y = 40; //画布高度
public $str ; //验证码
2.生成验证码字符
public function create_str()
{
$str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$this->str = substr(str_shuffle($str), 0,$this->length);
}
3.创建画布,写入验证码
//创建画布
$im = imagecreatetruecolor($this->x,$this->y);
//创建画布的背景色
$bgcolor = imagecolorallocate($im,255,255,255);
$textcolor = imagecolorallocate($im,0,0,0);
//画布填充背景色
imagefill($im,0,0,$bgcolor);
$space_per_char = $this->x / (strlen($this->str) + 1);
//画布上写验证码
for($i=0;$i<strlen($this->str);$i++) {
$fontsize = 14+mt_rand(0,8);
$angle = -20+rand(0,50);
$x = ($i+0.3)*$space_per_char;
$y = $this->y-($this->y * 0.2+rand(4,14));
imagettftext($im,$fontsize,$angle,$x,$y,$textcolor,'texb.ttf',$this->str[$i]);
}
4.画一些干扰线条
$colors[] = imagecolorallocate($im,128,64,192);
$colors[] = imagecolorallocate($im,192,64,128);
$colors[] = imagecolorallocate($im,108,192,64);
$colors[] = imagecolorallocate($im,138,122,104);
for($i=0;$i<100;$i++)
{
$x1 = rand(5,$this->x - 5);
$y1 = rand(5,$this->y - 5);
$x2 = $x1 - 4 + rand(0,10);
$y2 = $y1 - 4 + rand(0,10);
$color = $colors[rand(0,count($colors)-1)];
imageline($im,$x1,$y1,$x2,$y2,$color);
}
5.输出画布到浏览器
header ('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
完整代码
class VerificationCode {
public $length = 4; //默认输出4个字符
public $x = 120; //画布长度
public $y = 40; //画布高度
public $str ; //验证码
public function __construct($x="",$y="",$length="")
{
$this->x = $x ? $x : $this->x;
$this->y = $y ? $y : $this->y;
$this->length = $length ? $length : $this->length;
$this->create_str();
}
//生成验证码字符
public function create_str()
{
$str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$this->str = substr(str_shuffle($str), 0,$this->length);
}
//获取验证码字符
public function get_str()
{
return $this->str;
}
//创建验证码画布
public function create()
{
//创建画布
$im = imagecreatetruecolor($this->x,$this->y);
//创建画布的背景色
$bgcolor = imagecolorallocate($im,255,255,255);
$textcolor = imagecolorallocate($im,0,0,0);
//画布填充背景色
imagefill($im,0,0,$bgcolor);
$space_per_char = $this->x / (strlen($this->str) + 1);
//画布上写验证码
for($i=0;$i<strlen($this->str);$i++) {
$fontsize = 14+mt_rand(0,8);
$angle = -20+rand(0,50);
$x = ($i+0.3)*$space_per_char;
$y = $this->y-($this->y * 0.2+rand(4,14));
imagettftext($im,$fontsize,$angle,$x,$y,$textcolor,'texb.ttf',$this->str[$i]);
}
//画一些线条
$this->write_line($im);
header ('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
//画一些线条
public function write_line($im)
{
$colors[] = imagecolorallocate($im,128,64,192);
$colors[] = imagecolorallocate($im,192,64,128);
$colors[] = imagecolorallocate($im,108,192,64);
$colors[] = imagecolorallocate($im,138,122,104);
for($i=0;$i<100;$i++)
{
$x1 = rand(5,$this->x - 5);
$y1 = rand(5,$this->y - 5);
$x2 = $x1 - 4 + rand(0,10);
$y2 = $y1 - 4 + rand(0,10);
$color = $colors[rand(0,count($colors)-1)];
imageline($im,$x1,$y1,$x2,$y2,$color);
}
}
}
$obj = new VerificationCode();
$obj->create();