<?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生成动态验证码的更多相关文章

  1. C#之asp.net 及MVC 生成动态验证码:

    C#之asp.net 及MVC 生成动态验证码: 1.生成验证码字符串 // 随机生成指定长度的验证码字符串private string RandomCode(int length) { string ...

  2. .NET中生成动态验证码

    .NET中生成动态验证码 验证码是图片上写上几个字,然后对这几个字做特殊处理,如扭曲.旋转.修改文字位置,然后加入一些线条,或加入一些特殊效果,使这些在人类能正常识别的同时,机器却很难识别出来,以达到 ...

  3. php生成动态验证码 加减算法验证码 简单验证码

    预览效果: <?php /** *ImageCode 生成包含验证码的GIF图片的函数 *@param $string 字符串 *@param $width 宽度 *@param $height ...

  4. java生成动态验证码

    import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;impor ...

  5. Servlet仿CSDN动态验证码的生成-带数字和字母

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 一.实现的思路: (1)首先,须要创建一个Servlet.该Servlet通过字节型响应给cl ...

  6. iOS 生成本地验证码

    在应用程序注册.登陆或者有关支付确认的界面,经常会用到验证码,验证码有的是通过手机发送获取的,有的是在本地点击获取的,通过手机发送获取的动态验证码可以使用第三方类库实现,本地点击获取的是在本地自己绘制 ...

  7. Linux下使用Google Authenticator配置SSH登录动态验证码

    1.一般ssh登录服务器,只需要输入账号和密码.2.本教程的目的:在账号和密码之间再增加一个验证码,只有输入正确的验证码之后,再输入密码才能登录.这样就增强了ssh登录的安全性.3.账号.验证码.密码 ...

  8. Django之动态验证码的生成

    kind.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  9. 由12306动态验证码想到的ASP.NET实现动态GIF验证码(附源码)

    背景: 12306网站推出“彩色动态验证码机制”,新版验证码不但经常出现字符叠压,还不停抖动,不少人大呼“看不清”,称“那个验证码,是毕加索的抽象画么!”铁总客服则表示:为了能正常购票只能这样.而多家 ...

随机推荐

  1. My WelcomeApplet

    import java.applet.*; import java.awt.*; import java.awt.event.*; public class WelcomeApplet extends ...

  2. b/s开发者的困境

    作为一个由内而外都贴着web开发者标签的开发人员,注定会被大多数c/s开发者和通讯接口开发者所鄙视. 在他们看来,从事web开发,肯定很少用到算法,设计模式,多线程等等.我只能说,就我个人的经历来说, ...

  3. 用file上传文件时,浏览器返回值总是自动添加<pre>的解决办法

    在返回的JSon字符串里面总是莫名其妙的添加了<pre>标签,例如返回内容为"ok",在浏览器里面就变成了"<pre style="word- ...

  4. MATLAB那些常见的命令

    1.clear :清除内存变量和函数,也就是把WorkSpace的变量给清空: 2.clc :清楚当前Matlab命令窗口的内容:相当于清楚屏幕吧,重新开始,简洁一点,注意clc是不清 楚变量的.一般 ...

  5. php代码运行提速的20个小技巧(转)

    用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则 不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册中 ...

  6. 3,SFDC 管理员篇 - 区域划分

    1,销售区域划分 Setup | Administrator | Manage Territory Territory Type : 帮助用户建立大的销售区域分类,分类顺序按照Priority进行显示 ...

  7. swift 常见报错及解决方案

    当你初学swift,或者你从2.0转到3.0,见点红是在所难免再寻常不过的事情,其实一般也都是一些小的语法问题,度娘一般都有能力告诉你答案,但是凡事做个总结做到心中有数毕竟是比度娘靠谱的. 1.can ...

  8. CentOS6开启FTP及telnet服务教程

    先来开通CentOS6的FTP服务吧.telnet服务也一并学习学习吧.在安装好CentOS以后,需要设置Ftp和Telnet服务文件,才能启动Ftp和Telnet服务,可以通过远程控制进行开启. 开 ...

  9. leetcode刷题: 002 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  10. docker网络配置之自定义网桥

    使用特定范围的 IP (仅适用于v1.x)不适用于新版的v1.1x Docker 会尝试寻找没有被主机使用的 ip 段,尽管它适用于大多数情况下,但是它不是万能的,有时候我们还是需要对 ip 进一步规 ...