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网站推出“彩色动态验证码机制”,新版验证码不但经常出现字符叠压,还不停抖动,不少人大呼“看不清”,称“那个验证码,是毕加索的抽象画么!”铁总客服则表示:为了能正常购票只能这样.而多家 ...
随机推荐
- ThinkPad E440 Ubuntu 13.1无线网卡 RTL8723BE 驱动解决办法总结
方法一.在使用网线链接到情况下 第一步.执行下边到命令,即从github上下载驱动程序并安装 sudo apt-get install linux-headers-generic build-esse ...
- SQL Server 2008 数据库通过镜像同步备份(数据库热备)
数据库镜像运行模式: 异步数据库镜像模式(异步,高性能模式) 同步数据库镜像模式(同步,高安全模式) 参考资料: http://technet.microsoft.com/zh-cn/library/ ...
- Oracle导入和导出
导出:EXP userid=<username>/<password>@<service_name> file=<dmpname> e.g.exp sa ...
- FCKeditor 2.6.4.1 初始化值不能显示中文问题
最近在学习PHP加入FCKeditor 在线编辑器,发现在初始化Value赋值时,英文可以显示出来,中文怎么都显示不出来,连乱码都不显示. 经过漫长的探索,终于找到原因了! 在fckeditor目录下 ...
- web.config
参数上传和文件上传大小限制调整,参数上传最大2097151 <system.web> <httpRuntime requestValidationMode="2.0&quo ...
- [CF225C] Barcode (简单DAG上dp)
题目链接:http://codeforces.com/problemset/problem/225/C 题目大意:给你一个矩阵,矩阵中只有#和.两种符号.现在我们希望能够得到一个新的矩阵,新的矩阵满足 ...
- 黑马程序员_JAVA之交通灯管理系统
------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 1.一.需求:模拟实现十字路口的交通灯管理系统逻辑,具体需求如下: 1.异步随机生成按照各个路 ...
- Change the Windows 7 Taskbar Thumbnail and List Mode
Manually in Registry Editor 1. Open the Start Menu, then type regedit in the search boxand press Ent ...
- 剑指Offer:面试题24——二叉搜索树的后序遍历序列(java实现)
问题描述: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true,否则返回false.假设输入的数组的任意两个数字都互不相同. 思路: 1.首先后序遍历的结果是[(左子 ...
- POS管理系统之设备出库
JSP: <html> <head> <title>登录</title> <style type="text/css&quo ...