php生成动态验证码
<?php
/*调用示例*/
session_start();
$checkCode='';
$chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPRSTUVWXYZ23456789';
for($i=0;$i<4;$i++){
$checkCode.=substr($chars,mt_rand(0,strlen($chars)-1),1);
}
$_SESSION['code']=strtoupper($checkCode);// 记录session
ImageCode($checkCode,60);// 显示GIF动画
/**
*ImageCode 生成包含验证码的GIF图片的函数
*@param $string 字符串
*@param $width 宽度
*@param $height 高度
**/
function ImageCode($string='',$width=75,$height=25){
$authstr=$string?$string:((time()%2==0)?mt_rand(1000,9999):mt_rand(10000,99999));
$board_width=$width;
$board_height=$height;
// 生成一个32帧的GIF动画
for($i=0;$i<32;$i++){
ob_start();
$image=imagecreate($board_width,$board_height);
imagecolorallocate($image,0,0,0);
// 设定文字颜色数组
$colorList[]=ImageColorAllocate($image,15,73,210);
$colorList[]=ImageColorAllocate($image,0,64,0);
$colorList[]=ImageColorAllocate($image,0,0,64);
$colorList[]=ImageColorAllocate($image,0,128,128);
$colorList[]=ImageColorAllocate($image,27,52,47);
$colorList[]=ImageColorAllocate($image,51,0,102);
$colorList[]=ImageColorAllocate($image,0,0,145);
$colorList[]=ImageColorAllocate($image,0,0,113);
$colorList[]=ImageColorAllocate($image,0,51,51);
$colorList[]=ImageColorAllocate($image,158,180,35);
$colorList[]=ImageColorAllocate($image,59,59,59);
$colorList[]=ImageColorAllocate($image,0,0,0);
$colorList[]=ImageColorAllocate($image,1,128,180);
$colorList[]=ImageColorAllocate($image,0,153,51);
$colorList[]=ImageColorAllocate($image,60,131,1);
$colorList[]=ImageColorAllocate($image,0,0,0);
$fontcolor=ImageColorAllocate($image,0,0,0);
$gray=ImageColorAllocate($image,245,245,245);
$color=imagecolorallocate($image,255,255,255);
$color2=imagecolorallocate($image,255,0,0);
imagefill($image,0,0,$gray);
$space=15;// 字符间距
if($i>0){// 屏蔽第一帧
$top=0;
for($k=0;$k<strlen($authstr);$k++){
$colorRandom=mt_rand(0,sizeof($colorList)-1);
$float_top=rand(0,4);
$float_left=rand(0,3);
imagestring($image,6,$space*$k,$top+$float_top,substr($authstr,$k,1),$colorList[$colorRandom]);
}
}
for($k=0;$k<20;$k++){
$colorRandom=mt_rand(0,sizeof($colorList)-1);
imagesetpixel($image,rand()%70,rand()%15,$colorList[$colorRandom]);
}
// 添加干扰线
for($k=0;$k<3;$k++){
$colorRandom=mt_rand(0,sizeof($colorList)-1);
$todrawline=1;
if($todrawline){
imageline($image,mt_rand(0,$board_width),mt_rand(0,$board_height),mt_rand(0,$board_width),mt_rand(0,$board_height),$colorList[$colorRandom]);
}else{
$w=mt_rand(0,$board_width);
$h=mt_rand(0,$board_width);
imagearc($image,$board_width-floor($w / 2),floor($h / 2),$w,$h, rand(90,180),rand(180,270),$colorList[$colorRandom]);
}
}
imagegif($image);
imagedestroy($image);
$imagedata[]=ob_get_contents();
ob_clean();
++$i;
}
$gif=new GIFEncoder($imagedata);
Header('Content-type:image/gif');
echo $gif->GetAnimation();
}
/**
*GIFEncoder类
**/
Class GIFEncoder{
var $GIF="GIF89a"; /* GIF header 6 bytes */
var $VER="GIFEncoder V2.06"; /* Encoder version */
var $BUF=Array();
var $LOP=0;
var $DIS=2;
var $COL=-1;
var $IMG=-1;
var $ERR=Array(
'ERR00'=>"Does not supported function for only one image!",
'ERR01'=>"Source is not a GIF image!",
'ERR02'=>"Unintelligible flag ",
'ERR03'=>"Could not make animation from animated GIF source",
);
function GIFEncoder($GIF_src,$GIF_dly=100,$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)){
printf("%s: %s",$this->VER,$this->ERR['ERR00']);
exit(0);
}
$this->LOP=($GIF_lop>-1)?$GIF_lop:0;
$this->DIS=($GIF_dis>-1)?(($GIF_dis<3)?$GIF_dis:3):2;
$this->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"){
$this->BUF[]=fread(fopen($GIF_src [$i],"rb"),filesize($GIF_src [$i]));
}elseif(strToLower($GIF_mod)=="bin"){
$this->BUF [ ]=$GIF_src [ $i ];
}else{
printf("%s: %s(%s)!",$this->VER,$this->ERR [ 'ERR02' ],$GIF_mod);
exit(0);
}
if(substr($this->BUF[$i],0,6)!="GIF87a"&&substr($this->BUF [$i],0,6)!="GIF89a"){
printf("%s: %d %s",$this->VER,$i,$this->ERR ['ERR01']);
exit(0);
}
for($j=(13+3*(2<<(ord($this->BUF[$i]{10})&0x07))),$k=TRUE;$k;$j++){
switch($this->BUF [$i]{$j}){
case "!":
if((substr($this->BUF[$i],($j+3),8))=="NETSCAPE"){
printf("%s: %s(%s source)!",$this->VER,$this->ERR ['ERR03'],($i+1));
exit(0);
}
break;
case ";":
$k=FALSE;
break;
}
}
}
GIFEncoder::GIFAddHeader();
for($i=0,$count_buf=count($this->BUF);$i<$count_buf;$i++){
GIFEncoder::GIFAddFrames($i,$GIF_dly[$i]);
}
GIFEncoder::GIFAddFooter();
}
function GIFAddHeader(){
$cmap=0;
if(ord($this->BUF[0]{10})&0x80){
$cmap=3*(2<<(ord($this->BUF [0]{10})&0x07));
$this->GIF.=substr($this->BUF [0],6,7);
$this->GIF.=substr($this->BUF [0],13,$cmap);
$this->GIF.="!\377\13NETSCAPE2.0\3\1".GIFEncoder::GIFWord($this->LOP)."\0";
}
}
function GIFAddFrames($i,$d){
$Locals_str=13+3*(2 <<(ord($this->BUF[$i]{10})&0x07));
$Locals_end=strlen($this->BUF[$i])-$Locals_str-1;
$Locals_tmp=substr($this->BUF[$i],$Locals_str,$Locals_end);
$Global_len=2<<(ord($this->BUF [0]{10})&0x07);
$Locals_len=2<<(ord($this->BUF[$i]{10})&0x07);
$Global_rgb=substr($this->BUF[0],13,3*(2<<(ord($this->BUF[0]{10})&0x07)));
$Locals_rgb=substr($this->BUF[$i],13,3*(2<<(ord($this->BUF[$i]{10})&0x07)));
$Locals_ext="!\xF9\x04".chr(($this->DIS<<2)+0).chr(($d>>0)&0xFF).chr(($d>>8)&0xFF)."\x0\x0";
if($this->COL>-1&&ord($this->BUF[$i]{10})&0x80){
for($j=0;$j<(2<<(ord($this->BUF[$i]{10})&0x07));$j++){
if(ord($Locals_rgb{3*$j+0})==($this->COL>> 0)&0xFF&&ord($Locals_rgb{3*$j+1})==($this->COL>> 8)&0xFF&&ord($Locals_rgb{3*$j+2})==($this->COL>>16)&0xFF){
$Locals_ext="!\xF9\x04".chr(($this->DIS<<2)+1).chr(($d>>0)&0xFF).chr(($d>>8)&0xFF).chr($j)."\x0";
break;
}
}
}
switch($Locals_tmp{0}){
case "!":
$Locals_img=substr($Locals_tmp,8,10);
$Locals_tmp=substr($Locals_tmp,18,strlen($Locals_tmp)-18);
break;
case ",":
$Locals_img=substr($Locals_tmp,0,10);
$Locals_tmp=substr($Locals_tmp,10,strlen($Locals_tmp)-10);
break;
}
if(ord($this->BUF[$i]{10})&0x80&&$this->IMG>-1){
if($Global_len==$Locals_len){
if(GIFEncoder::GIFBlockCompare($Global_rgb,$Locals_rgb,$Global_len)){
$this->GIF.=($Locals_ext.$Locals_img.$Locals_tmp);
}else{
$byte=ord($Locals_img{9});
$byte|=0x80;
$byte&=0xF8;
$byte|=(ord($this->BUF [0]{10})&0x07);
$Locals_img{9}=chr($byte);
$this->GIF.=($Locals_ext.$Locals_img.$Locals_rgb.$Locals_tmp);
}
}else{
$byte=ord($Locals_img{9});
$byte|=0x80;
$byte&=0xF8;
$byte|=(ord($this->BUF[$i]{10})&0x07);
$Locals_img {9}=chr($byte);
$this->GIF.=($Locals_ext.$Locals_img.$Locals_rgb.$Locals_tmp);
}
}else{
$this->GIF.=($Locals_ext.$Locals_img.$Locals_tmp);
}
$this->IMG=1;
}
function GIFAddFooter(){
$this->GIF.=";";
}
function GIFBlockCompare($GlobalBlock,$LocalBlock,$Len){
for($i=0;$i<$Len;$i++){
if($GlobalBlock{3*$i+0}!=$LocalBlock{3*$i+0}||$GlobalBlock{3*$i+1}!=$LocalBlock{3*$i+1}||$GlobalBlock{3*$i+2}!=$LocalBlock{3*$i+2}){
return(0);
}
}
return(1);
}
function GIFWord($int){
return(chr($int&0xFF).chr(($int>>8)&0xFF));
}
function GetAnimation(){
return($this->GIF);
}
}
php生成动态验证码的更多相关文章
- C#之asp.net 及MVC 生成动态验证码:
C#之asp.net 及MVC 生成动态验证码: 1.生成验证码字符串 // 随机生成指定长度的验证码字符串private string RandomCode(int length) { string ...
- .NET中生成动态验证码
.NET中生成动态验证码 验证码是图片上写上几个字,然后对这几个字做特殊处理,如扭曲.旋转.修改文字位置,然后加入一些线条,或加入一些特殊效果,使这些在人类能正常识别的同时,机器却很难识别出来,以达到 ...
- php生成动态验证码 加减算法验证码 简单验证码
预览效果: <?php /** *ImageCode 生成包含验证码的GIF图片的函数 *@param $string 字符串 *@param $width 宽度 *@param $height ...
- java生成动态验证码
import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;impor ...
- Servlet仿CSDN动态验证码的生成-带数字和字母
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 一.实现的思路: (1)首先,须要创建一个Servlet.该Servlet通过字节型响应给cl ...
- iOS 生成本地验证码
在应用程序注册.登陆或者有关支付确认的界面,经常会用到验证码,验证码有的是通过手机发送获取的,有的是在本地点击获取的,通过手机发送获取的动态验证码可以使用第三方类库实现,本地点击获取的是在本地自己绘制 ...
- Linux下使用Google Authenticator配置SSH登录动态验证码
1.一般ssh登录服务器,只需要输入账号和密码.2.本教程的目的:在账号和密码之间再增加一个验证码,只有输入正确的验证码之后,再输入密码才能登录.这样就增强了ssh登录的安全性.3.账号.验证码.密码 ...
- Django之动态验证码的生成
kind.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- 由12306动态验证码想到的ASP.NET实现动态GIF验证码(附源码)
背景: 12306网站推出“彩色动态验证码机制”,新版验证码不但经常出现字符叠压,还不停抖动,不少人大呼“看不清”,称“那个验证码,是毕加索的抽象画么!”铁总客服则表示:为了能正常购票只能这样.而多家 ...
随机推荐
- view
把view添加到某个视图的虾面 [self.superview insertSubview:smallCircle belowSubview:self]; // 返回两个数的根 return sqrt ...
- java io流 数据流传输
java io流 数据流传输 把这段当公式用就可以了 //数据流传输 import java.io.*; public class Index{ public static void main(Str ...
- RESTClient使用
RESTClient使用
- typedef struct 结构体
typedef struct _TTTT_ { int i; }TT_TT; 定义变量如下: struct _TTTT_ NewTT;方法1 TT_TT NewTT;方法2 是声明和定义 ...
- Linux 本人常用到的基本命令
cat -n FileName //查看FileName文件的内容.-n显示对应行号. yum install SoftName //安装软件,切记使用root权限. service //查看服务.例 ...
- 通过案例对 spark streaming 透彻理解三板斧之二:spark streaming运行机制
本期内容: 1. Spark Streaming架构 2. Spark Streaming运行机制 Spark大数据分析框架的核心部件: spark Core.spark Streaming流计算. ...
- vs2013_arcgis_developer_kit_101_install
1.修改注册表以安装AE101 在注册表中HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0增加类型为REG_SZ的 ...
- 窗口 超类化 子类化 HOOK
body { font-family: Bitstream Vera Sans Mono; font-size: 11pt; line-height: 1.5; } html, body { colo ...
- Rserve, java调用R源文件
Rserve安装和加载: install.packages("Rserve") library("Rserve") Rserve() java调用: REn ...
- Python自动化 【第七篇】:Python基础-面向对象高级语法、异常处理、Scoket开发基础
本节内容: 1. 面向对象高级语法部分 1.1 静态方法.类方法.属性方法 1.2 类的特殊方法 1.3 反射 2. 异常处理 3. Socket开发基础 1. ...