好久没来了,分享点东西顺便散分

www_7di_net 2013-08-01 04:01:59
纯PHP手工打造的 会动的多帧GIF图片验证码.
效果演示:http://pcik.7di.net/pcik_reg

百度的效果演示:
https://passport.baidu.com/cgi-bin/genimage?captchaservice63636236364e55367233302f31673844526b664451665a5a4d4977466974376b707a754466777934697449455561625171346c725055444b51734a35376d2b4f744b6d303238315341382b354675344c3153745869487252376169752b437450515138574972436752584f53717849726f48593258666c373574593753614f4d32703831724e51722b694a31756b67467137644c30506979496639594e504931536732687a5a505379305544554245724f76694a307247632b4f76426165663144732b595359394950413470476453787865564f6b506e674f7537637264526d716541377a384b487445624f365a4774657a58502b7347703763696d5752614171615747784e5a6c5042336634766a5049766853365974444258577968645a4f654f55

由於我水平不行,所以做不到百度那麼絢的效果,請海涵

验证码图片由PHP生成的多帧构成,所以验证码是一个真实存在的可以动的gif图
使用方法非常简单:
說明:
Gif驗證碼創建類
作者:
7di.net QQ群:223494678
调用:
<img src="class_code.php?code=code" onclick="this.src=this.src+'&'+Math.round(Math.random(0)*1000)" style="cursor:pointer;">
验证:
IF(!isSet($_SESSION['code']) Or StrToLower($_SESSION['code'])!=StrToLower($cod)){
throw New Exception('Error:'.__LINE__.',驗證碼錯誤!');Die();
}unSet($cod,$_SESSION['code']);

下载地址:http://download.csdn.net/detail/sibang/5852531
...全文
434 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
happytonice 2013-08-07
  • 打赏
  • 举报
回复
lz加油!
qeorpieewtp 2013-08-07
  • 打赏
  • 举报
回复
收藏了 以后多交流
d_erse 2013-08-07
  • 打赏
  • 举报
回复
不错啊。。。
少装点怪 2013-08-07
  • 打赏
  • 举报
回复
不解释!接分!支持楼主
feraner 2013-08-06
  • 打赏
  • 举报
回复
森之树 2013-08-06
  • 打赏
  • 举报
回复
接分
jasenlee 2013-08-06
  • 打赏
  • 举报
回复
很好很强大!!
jueduibing 2013-08-06
  • 打赏
  • 举报
回复
不错不错, 改进下更好
丿九零 2013-08-05
  • 打赏
  • 举报
回复
谢啦,学习了
www_7di_net 2013-08-03
  • 打赏
  • 举报
回复
引用 14 楼 endl001 的回复:
虽然看不懂,但是感觉好厉害哦
回忆那么久 2013-08-02
  • 打赏
  • 举报
回复
我果断收藏它
公西雒 2013-08-02
  • 打赏
  • 举报
回复
对我这种新手来说,这个是好东西啊!果断收藏。
风一阵 2013-08-02
  • 打赏
  • 举报
回复
虽然看不懂,但是感觉好厉害哦
fjufo 2013-08-02
  • 打赏
  • 举报
回复
不错,收藏了
www_7di_net 2013-08-02
  • 打赏
  • 举报
回复
引用 11 楼 yubinding 的回复:
我果断收藏它
嘿嘿
www_7di_net 2013-08-01
  • 打赏
  • 举报
回复
修改完毕,现在第一帧为干扰码,请众位指点:
<?PHP
/**
	說明:
		Gif驗證碼創建類
	作者:
		7di.net  QQ群:223494678
	调用:
		<img src="class_code.php?code=code" onclick="this.src=this.src+'&'+Math.round(Math.random(0)*1000)" style="cursor:pointer;">
	验证:
		IF(!isSet($_SESSION['code']) Or StrToLower($_SESSION['code'])!=StrToLower($cod)){
			throw New Exception('Error:'.__LINE__.',驗證碼錯誤!');Die();
		}unSet($cod,$_SESSION['code']);
/**/
//ini_set('display_errors',True);
//Error_reporting(E_ALL);

$code=(isSet($_REQUEST['code'])) ? $_REQUEST['code'] : '';
IF($code=='code'){
	OB_start();OB_clean();
	//设置SessionID
	IF(!isSet($_SESSION)){Session_start();}

	//GIF类
	Class GIF{
		Private Static $Txt='';			//GIF mess
		Private Static $Img='GIF89a';	//GIF header 6 bytes
		Private Static $Debug=False;	//Is open Debug?
		Private Static $BUF=Array();
		Private Static $LOP= 0;
		Private Static $DIS= 2;
		Private Static $COL=-1;
		Private Static $IMG=-1;

		/**
			生成GIF图片验证
			@param $W 宽度
			@param $H 高度
			@param $B 背景色
		/**/
		Public Static Function init($W=75,$H=25,$B=''){
			$chars='bcdefhkmnrstuvwxyABCDEFGHKMNPRSTUVWXY34568';
			For($i=0;$i<3;$i++){
				self::$Txt .= SubStr($chars,mt_rand(0,strlen($chars)-1),1);
			}unSet($chars);
			$_SESSION['code']=StrToLower(self::$Txt);			// 记录session,全部小寫
			IF($B=='' Or StriStr($B,',')==False Or Substr_Count($B,',')!=2){$B='255,255,255';}
			$B=Explode(',',$B);

			//生成一个多帧的GIF动画
			For($i=0;$i<7;$i++){
				$Im=ImageCreate($W,$H);

				//背景
				$bg=ImageColorAllocate($Im,$B[0],$B[1],$B[2]);
				ImageColorTransparent($Im,$bg);
				unSet($bg);

				IF($i==0) {//第一幀為干擾碼
					$txt=ImageColorAllocate($Im,35,35,35);
						ImageTTFtext($Im,15,Rand(-15,25),5,($H-3),$txt,'class_code.ttf',self::$Txt[1]);
						ImageTTFtext($Im,15,Rand(-10,20),21,($H-4),$txt,'class_code.ttf',self::$Txt[1]);
						ImageTTFtext($Im,15,Rand(-25,15),38,($H-4),$txt,'class_code.ttf',self::$Txt[1]);
					unSet($txt);
				}Else{//驗證碼
					$txt=ImageColorAllocate($Im,35,35,35);
						//ImageString($Im,7,Rand(0,($W/2)),Rand(-3,5),self::$Txt,$txt);
						ImageTTFtext($Im,15,Rand(-15,25),5,($H-3),$txt,'class_code.ttf',self::$Txt[0]);
						ImageTTFtext($Im,15,Rand(-10,20),21,($H-4),$txt,'class_code.ttf',self::$Txt[1]);
						ImageTTFtext($Im,15,Rand(-25,15),38,($H-4),$txt,'class_code.ttf',self::$Txt[2]);
					unSet($txt);
				}
				ImageGif($Im);Imagedestroy($Im);

				$Imdata[]=ob_get_contents();
				OB_clean();
			}unSet($W,$H,$B);
			IF(self::$Debug){Echo '<pre>',Var_Dump($Imdata),'</pre>';Die();}
			Header('Content-type:image/gif');
			Return GIF::CreatGif($Imdata,20);
			unSet($Imdata);
		}

		Private Static Function CreatGif($GIF_src,$GIF_dly=10,$GIF_lop=0,$GIF_dis=0,$GIF_red=0,$GIF_grn=0,$GIF_blu=0,$GIF_mod='bin'){
			IF(!is_array($GIF_src) && !is_array($GIF_tim)){
				throw New Exception('Error:'.__LINE__.',Does not supported function for only one image!!');Die();
			}
			self::$LOP=($GIF_lop>-1) ? $GIF_lop : 0;
			self::$DIS=($GIF_dis>-1) ? (($GIF_dis<3) ? $GIF_dis : 3) : 2;
			self::$COL=($GIF_red>-1 && $GIF_grn>-1 && $GIF_blu>-1) ? ($GIF_red | ($GIF_grn << 8) | ($GIF_blu << 16)) : -1;
			For($i=0,$src_count=count($GIF_src);$i<$src_count;$i++){
				IF(strToLower($GIF_mod) == 'url'){
					self::$BUF[]=fread(fopen($GIF_src[$i],'rb'),filesize($GIF_src[$i]));
				}Elseif(strToLower($GIF_mod) == 'bin'){
					self::$BUF[]=$GIF_src[$i];
				}Else{
					throw New Exception('Error:'.__LINE__.',Unintelligible flag ('.$GIF_mod.')!');Die();
				}
				IF(!(Substr(self::$BUF[$i],0,6)=='GIF87a' Or Substr(self::$BUF[$i],0,6)=='GIF89a')){
					throw New Exception('Error:'.__LINE__.',Source '.$i.' is not a GIF image!');Die();
				}
				For($j=(13+3*(2 << (ord(self::$BUF[$i]{10})& 0x07))),$k=TRUE;$k;$j++){
					switch(self::$BUF[$i]{$j}){
					case '!':
						IF((substr(self::$BUF[$i],($j+3),8))== 'NETSCAPE'){
							throw New Exception('Error:'.__LINE__.',Could not make animation from animated GIF source ('.($i+1).')!');Die();
						}
						break;
					case ';':
						$k=FALSE;
						break;
					}
				}
			}
			GIF::AddHeader();
			For($i=0,$count_buf=count(self::$BUF);$i<$count_buf;$i++){
				GIF::AddFrames($i,$GIF_dly);
			}
			self::$Img .= ';';
			Return (self::$Img);
		}

		Private Static Function AddHeader(){
			$i=0;
			IF(ord(self::$BUF[0]{10}) & 0x80){
				$i=3*(2 << (ord(self::$BUF[0]{10}) & 0x07));
				self::$Img .= substr(self::$BUF[0],6,7);
				self::$Img .= substr(self::$BUF[0],13,$i);
				self::$Img .= "!\377\13NETSCAPE2.0\3\1".chr(self::$LOP & 0xFF).chr((self::$LOP >> 8) & 0xFF)."\0";
			}unSet($i);
		}

		Private Static Function AddFrames($i,$d){
			$L_str=13+3*(2 <<(ord(self::$BUF[$i]{10}) & 0x07));
			$L_end=strlen(self::$BUF[$i])-$L_str-1;
			$L_tmp=substr(self::$BUF[$i],$L_str,$L_end);
			$G_len=2 << (ord(self::$BUF[0]{10}) & 0x07);
			$L_len=2 << (ord(self::$BUF[$i]{10}) & 0x07);
			$G_rgb=substr(self::$BUF[0],13,3*(2 << (ord(self::$BUF[0]{10}) & 0x07)));
			$L_rgb=substr(self::$BUF[$i],13,3*(2 << (ord(self::$BUF[$i]{10}) & 0x07)));
			$L_ext="!\xF9\x04".chr((self::$DIS << 2)+ 0).chr(($d >> 0) & 0xFF).chr(($d >> 8) & 0xFF)."\x0\x0";
			IF(self::$COL>-1 && ord(self::$BUF[$i]{10}) & 0x80){
				For($j=0;$j<(2 << (ord(self::$BUF[$i]{10}) & 0x07));$j++){
					IF(ord($L_rgb{3*$j+0})==(self::$COL >>  0) & 0xFF && ord($L_rgb{3*$j+1})== (self::$COL >>  8) & 0xFF && ord($L_rgb{3*$j+2}) == (self::$COL >> 16) & 0xFF){
						$L_ext="!\xF9\x04".chr((self::$DIS << 2)+1).chr(($d >> 0) & 0xFF).chr(($d >> 8) & 0xFF).chr($j)."\x0";
						break;
					}
				}
			}
			switch($L_tmp{0}){
			case '!':
				$L_img=substr($L_tmp,8,10);
				$L_tmp=substr($L_tmp,18,strlen($L_tmp)-18);
				break;
			case ',':
				$L_img=substr($L_tmp,0,10);
				$L_tmp=substr($L_tmp,10,strlen($L_tmp)-10);
				break;
			}
			IF(ord(self::$BUF[$i]{10}) & 0x80 && self::$IMG>-1){
				IF($G_len == $L_len){
					IF(GIF::Compare($G_rgb,$L_rgb,$G_len)){
						self::$Img .= ($L_ext.$L_img.$L_tmp);
					}Else{
						$byte =ord($L_img{9});
						$byte |= 0x80;
						$byte &= 0xF8;
						$byte |= (ord(self::$BUF[0]{10}) & 0x07);
						$L_img{9}=chr($byte);
						self::$Img .= ($L_ext.$L_img.$L_rgb.$L_tmp);
					}
				}Else{
					$byte =ord($L_img{9});
					$byte |= 0x80;
					$byte &= 0xF8;
					$byte |= (ord(self::$BUF[$i]{10}) & 0x07);
					$L_img{9}=chr($byte);
					self::$Img .= ($L_ext.$L_img.$L_rgb.$L_tmp);
				}
			}Else{
				self::$Img .= ($L_ext.$L_img.$L_tmp);
			}
			self::$IMG =1;
		}

		Private Static Function Compare($G_Block,$L_Block,$Len){
			For($i=0;$i<$Len;$i++){
				IF($G_Block{3*$i+0} != $L_Block{3*$i+0} || $G_Block{3*$i+1} != $L_Block{3*$i+1} || $G_Block{3*$i+2} != $L_Block{3*$i+2}){
					Return (0);
				}
			}
			Return (1);
		}
	}

	//取得自定義背景色
	$bgcolor=(isSet($_REQUEST['c'])) ? $_REQUEST['c'] : '';
	//輸出GIF
	Echo Gif::init(60,23,$bgcolor);
	Die();
}
www_7di_net 2013-08-01
  • 打赏
  • 举报
回复
引用 7 楼 hellodifa 的回复:
[quote=引用 5 楼 sibang 的回复:] [quote=引用 3 楼 xuzuning 的回复:] 您的验证码文字都出现在第一帧,因此只是好玩而已 望改进!
額,沒太聽懂,希望斑竹老大詳細指點一下.[/quote] 意思是不是验证码出现在了第一帧,因此破解验证码只要拿到第一帧即可,所以改进,不让他在第一帧出现。[/quote] ok,馬上改之
夏之冰雪 2013-08-01
  • 打赏
  • 举报
回复
引用 5 楼 sibang 的回复:
[quote=引用 3 楼 xuzuning 的回复:] 您的验证码文字都出现在第一帧,因此只是好玩而已 望改进!
額,沒太聽懂,希望斑竹老大詳細指點一下.[/quote] 意思是不是验证码出现在了第一帧,因此破解验证码只要拿到第一帧即可,所以改进,不让他在第一帧出现。
www_7di_net 2013-08-01
  • 打赏
  • 举报
回复
引用 3 楼 xuzuning 的回复:
您的验证码文字都出现在第一帧,因此只是好玩而已 望改进!
額,沒太聽懂,希望斑竹老大詳細指點一下.
print_r 2013-08-01
  • 打赏
  • 举报
回复
看一眼拿点分
加载更多回复(3)

20,359

社区成员

发帖
与我相关
我的任务
社区描述
“超文本预处理器”,是在服务器端执行的脚本语言,尤其适用于Web开发并可嵌入HTML中。PHP语法利用了C、Java和Perl,该语言的主要目标是允许web开发人员快速编写动态网页。
phpphpstorm 技术论坛(原bbs)
社区管理员
  • 开源资源社区
  • phpstory
  • xuzuning
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧