当前位置: 移动技术网 > IT编程>开发语言>PHP > php生成复杂验证码(倾斜,正弦干扰线,黏贴,旋转)

php生成复杂验证码(倾斜,正弦干扰线,黏贴,旋转)

2019年05月30日  | 移动技术网IT编程  | 我要评论

mdyd-751,冰冰高清,再一次心动

现在好多网站使用的验证码非常容易被软件自动识别到,本文介绍了一种php生成复杂验证码加入倾斜,弦干扰线,黏贴,旋转等效果

常见的软件不容易自动识别的验证码做法有以下3种:

1、字体变型 (一般通过算法,进行扭曲)

2、字体黏贴 (这里面以qq验证码为代表了,目前网上还是很难找到,破解qq验证码的)

3、干扰线,噪点 (这种识别起来相当容易,很容易被程序自动化识别)

对于上面提到,第1,2 二种方法,在识别时候,是比较困难的。

具体实现代码:

<?php
/**
 *带文字旋转,倾斜,黏贴,加正弦干扰线验证码*
 */
class utils_caption
{
	var $width   = 60;      //图片宽
	var $height   = 30;      //图片高
	var $length   = 4;      //验证码位数
	var $bgcolor  = "#ffffff";  //背景色

	var $tfonts = array("font.ttf");
	var $tfontsize=array(17,20); //字体大小范围
	var $tfontangle=array(-20,20); //旋转角度

	var $chars  = "0123456789";     //验证码范围(字母数字)

	var $code  = array();       //验证码
	var $image  = "";       //图形对象
	var $fontcolors=array('#f36161','#6bc146','#5368bd'); //字体颜色,红绿蓝
	var $tpadden = 0.75;///字符间距,多少个字符
	var $txbase = 5;///x轴两边距离
	var $tybase =5 ;///y轴两边距离
	var $tline =true; ///画干扰线


	public function randrsi() ///生成验证码
	{
		$this->tfontangle=range($this->tfontangle[0],$this->tfontangle[1]);
		$this->tfontsize=range($this->tfontsize[0],$this->tfontsize[1]);

		$arr=array();
		$chars=$this->chars;
		$tfontangle=$this->tfontangle;
		$tfontsize=$this->tfontsize;
		$fontcolors=$this->fontcolors;
		$code="";
		$font=dirname(__file__)."/font/".$this->tfonts[0];

		$charlen=strlen($chars)-1;
		$anglelen=count($tfontangle)-1; // 角度范围
		$fontsizelen=count($tfontsize)-1; // 角度范围
		$fontcolorlen=count($fontcolors)-1; // 角度范围

		for($i=0;$i<$this->length;$i++) ///得到字符与颜色
		{
			$char=$chars[rand(0,$charlen)]; ///得到字符
			$angle=$tfontangle[rand(0,$anglelen)]; ///旋转角度
			$fontsize=$tfontsize[rand(0,$fontsizelen)]; ///字体大小
			$fontcolor=$fontcolors[rand(0,$fontcolorlen)]; ///字体大小

			$bound=$this->_calculatetextbox($fontsize,$angle,$font,$char); ///得到范围

			$arr[]=array($fontsize,$angle,$fontcolor,$char,$font,$bound); ///得到矩形框
			$code.=$char;
		}
		$this->code=$arr; //验证码
		return $code;
	}

	public function draw() ///画图
	{
		if(empty($this->code)) $this->randrsi();
		$codes=$this->code; ///用户验证码


		$wh=$this->_getimagewh($codes);

		$width=$wh[0];
		$height=$wh[1]; ///高度

		$this->width=$width;
		$this->height=$height;

		$this->image = imagecreate( $width, $height );
		$image=$this->image;

		$back = $this->_getcolor2($this->_getcolor( $this->bgcolor)); ///背景颜色
		imagefilledrectangle($image, 0, 0, $width, $height, $back); ///填充背景

		$tpadden=$this->tpadden;

		$basex=$this->txbase;
		$color=null;
		foreach ($codes as $v) ///逐个画字符
		{
			$bound=$v[5];
			$color=$this->_getcolor2($this->_getcolor($v[2]));
			imagettftext($image, $v[0], $v[1], $basex, $bound['height'],$color , $v[4], $v[3]);
			$basex=$basex+$bound['width']*$tpadden-$bound['left'];///计算下一个左边距
		}
		$this->tline?$this->_wirtesinline($color,$basex):null; ///画干扰线
		header("content-type: image/png");
		imagepng( $image);
		imagedestroy($image);

	}

	/**
	 *通过字体角度得到字体矩形宽度*
	 *
	 * @param int $font_size 字体尺寸
	 * @param float $font_angle 旋转角度
	 * @param string $font_file 字体文件路径
	 * @param string $text 写入字符
	 * @return array 返回长宽高
	 */
	private function _calculatetextbox($font_size, $font_angle, $font_file, $text) {
		$box = imagettfbbox($font_size, $font_angle, $font_file, $text);

		$min_x = min(array($box[0], $box[2], $box[4], $box[6]));
		$max_x = max(array($box[0], $box[2], $box[4], $box[6]));
		$min_y = min(array($box[1], $box[3], $box[5], $box[7]));
		$max_y = max(array($box[1], $box[3], $box[5], $box[7]));

		return array(
		'left' => ($min_x >= -1) ? -abs($min_x + 1) : abs($min_x + 2),
		'top' => abs($min_y),
		'width' => $max_x - $min_x,
		'height' => $max_y - $min_y,
		'box' => $box
		);
	}

	private function _getcolor( $color ) //#ffffff
	{
		return array(hexdec($color[1].$color[2]),hexdec($color[3].$color[4]),hexdec($color[5].$color[6]));
	}

	private function _getcolor2( $color ) //#ffffff
	{
		return imagecolorallocate ($this->image, $color[0], $color[1], $color[2]);
	}

	private function _getimagewh($data)
	{
		$tpadden=$this->tpadden;
		$w=$this->txbase;
		$h=0;
		foreach ($data as $v)
		{
			$w=$w+$v[5]['width']*$tpadden-$v[5]['left'];
			$h=$h>$v[5]['height']?$h:$v[5]['height'];
		}
		return array(max($w,$this->width),max($h,$this->height));
	}

	//画正弦干扰线
	private function _wirtesinline($color,$w)
	{
		$img=$this->image;

		$h=$this->height;
		$h1=rand(-5,5);
		$h2=rand(-1,1);
		$w2=rand(10,15);
		$h3=rand(4,6);

		for($i=-$w/2;$i<$w/2;$i=$i+0.1)
		{
			$y=$h/$h3*sin($i/$w2)+$h/2+$h1;
			imagesetpixel($img,$i+$w/2,$y,$color);
			$h2!=0?imagesetpixel($img,$i+$w/2,$y+$h2,$color):null;
		}
	}
}

demo代码:

$rsi = new utils_caption();
$rsi->tfontsize=array(15,17);
$rsi->width=50;
$rsi->height=25;
$code = $rsi->randrsi();
session_start();
$_session["checkcode"] = $code;
$rsi->draw();

运行效果:

 

大家可以修改代码中的相应数值达到自己需要的复杂度

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网