溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用php怎么生成一個(gè)復(fù)雜的驗(yàn)證碼

發(fā)布時(shí)間:2021-02-25 15:17:55 來(lái)源:億速云 閱讀:130 作者:戴恩恩 欄目:開發(fā)技術(shù)

這篇文章主要為大家詳細(xì)介紹了使用php怎么生成一個(gè)復(fù)雜的驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,發(fā)現(xiàn)的小伙伴們可以參考一下:

php有什么用

php是一個(gè)嵌套的縮寫名稱,指的是英文超級(jí)文本預(yù)處理語(yǔ)言(php:Hypertext Preprocessor)的縮寫,它的語(yǔ)法混合了C、Java、Perl以及php自創(chuàng)新的語(yǔ)法,主要用來(lái)做網(wǎng)站開發(fā),許多小型網(wǎng)站都用php開發(fā),因?yàn)閜hp是開源的,從而使得php經(jīng)久不衰。

1、字體變型 (一般通過(guò)算法,進(jìn)行扭曲)

使用php怎么生成一個(gè)復(fù)雜的驗(yàn)證碼

2、字體黏貼 (這里面以qq驗(yàn)證碼為代表了,目前網(wǎng)上還是很難找到,破解qq驗(yàn)證碼的)

使用php怎么生成一個(gè)復(fù)雜的驗(yàn)證碼

3、干擾線,噪點(diǎn) (這種識(shí)別起來(lái)相當(dāng)容易,很容易被程序自動(dòng)化識(shí)別)

對(duì)于上面提到,第1,2 二種方法,在識(shí)別時(shí)候,是比較困難的。

具體實(shí)現(xiàn)代碼:

<?php
/**
 *帶文字旋轉(zhuǎn),傾斜,黏貼,加正弦干擾線驗(yàn)證碼*
 */
class Utils_Caption
{
	var $Width   = 60;      //圖片寬
	var $Height   = 30;      //圖片高
	var $Length   = 4;      //驗(yàn)證碼位數(shù)
	var $BgColor  = "#FFFFFF";  //背景色

	var $TFonts = array("font.ttf");
	var $TFontSize=array(17,20); //字體大小范圍
	var $TFontAngle=array(-20,20); //旋轉(zhuǎn)角度

	var $Chars  = "0123456789";     //驗(yàn)證碼范圍(字母數(shù)字)

	var $Code  = array();       //驗(yàn)證碼
	var $Image  = "";       //圖形對(duì)象
	var $FontColors=array('#f36161','#6bc146','#5368bd'); //字體顏色,紅綠藍(lán)
	var $TPadden = 0.75;///字符間距,多少個(gè)字符
	var $Txbase = 5;///x軸兩邊距離
	var $Tybase =5 ;///y軸兩邊距離
	var $TLine =true; ///畫干擾線


	public function RandRSI() ///生成驗(yàn)證碼
	{
		$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)]; ///旋轉(zhuǎn)角度
			$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; //驗(yàn)證碼
		return $code;
	}

	public function Draw() ///畫圖
	{
		if(empty($this->Code)) $this->RandRSI();
		$codes=$this->Code; ///用戶驗(yàn)證碼


		$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) ///逐個(gè)畫字符
		{
			$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'];///計(jì)算下一個(gè)左邊距
		}
		$this->TLine?$this->_wirteSinLine($color,$basex):null; ///畫干擾線
		header("Content-type: image/png");
		imagepng( $image);
		imagedestroy($image);

	}

	/**
	 *通過(guò)字體角度得到字體矩形寬度*
	 *
	 * @param int $font_size 字體尺寸
	 * @param float $font_angle 旋轉(zhuǎn)角度
	 * @param string $font_file 字體文件路徑
	 * @param string $text 寫入字符
	 * @return array 返回長(zhǎng)寬高
	 */
	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;
		$h2=rand(-5,5);
		$h3=rand(-1,1);
		$w2=rand(10,15);
		$h4=rand(4,6);

		for($i=-$w/2;$i<$w/2;$i=$i+0.1)
		{
			$y=$h/$h4*sin($i/$w2)+$h/2+$h2;
			imagesetpixel($img,$i+$w/2,$y,$color);
			$h3!=0?imagesetpixel($img,$i+$w/2,$y+$h3,$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();

以上就是億速云小編為大家收集整理的使用php怎么生成一個(gè)復(fù)雜的驗(yàn)證碼,如何覺(jué)得億速云網(wǎng)站的內(nèi)容還不錯(cuò),歡迎將億速云網(wǎng)站推薦給身邊好友。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI