php生成动态验证码 加减算法验证码

php生成动态验证码 加减算法验证码

PHP实例Git开源网2021-07-09 11:43:47268A+A-

一、动态gif验证码

预览效果:

<?php
/**
*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
namespace mobile\components;
/**
 * @author fenghuo
 *
 * 改造的加减法验证类
 * 使用示例 verifycode::get(1,2);
 * 验证示例 verifycode::check($code);
 */
class verifycode
{
  /**
   * php验证码
   */
  public static function get($one,$two,$prefix = '', $font_size = 28)
  {
    //文件头...
    ob_get_clean();
    header("content-type: image/png;charset=utf-8;");
    //创建真彩色白纸
    $width      = $font_size*5;
    $height      = $font_size+1;
    $im        = @imagecreatetruecolor($width, $height) or die("建立图像失败");
    //获取背景颜色
    $background_color = imagecolorallocate($im, 255, 255, 255);
    //填充背景颜色
    imagefill($im, 0, 0, $background_color);
    //获取边框颜色
    $border_color   = imagecolorallocate($im, 200, 200, 200);
    //画矩形,边框颜色200,200,200
    imagerectangle($im,0,0,$width - 1, $height - 1,$border_color);
    //逐行炫耀背景,全屏用1或0
    for($i = 2;$i < $height - 2;$i++) {
      //获取随机淡色
      $line_color = imagecolorallocate($im, rand(200,255), rand(200,255), rand(200,255));
      //画线
      imageline($im, 2, $i, $width - 1, $i, $line_color);
    }
    //设置印上去的文字
    $firstnum = $one;
    $secondnum = $two;
    $actionstr = $firstnum > $secondnum ? '-' : '+';
    //获取第1个随机文字
    $imstr[0]["s"] = $firstnum;
    $imstr[0]["x"] = rand(2, 5);
    $imstr[0]["y"] = rand(1, 4);
    //获取第2个随机文字
    $imstr[1]["s"] = $actionstr;
    $imstr[1]["x"] = $imstr[0]["x"] + $font_size - 1 + rand(0, 1);
    $imstr[1]["y"] = rand(1,5);
    //获取第3个随机文字
    $imstr[2]["s"] = $secondnum;
    $imstr[2]["x"] = $imstr[1]["x"] + $font_size - 1 + rand(0, 1);
    $imstr[2]["y"] = rand(1, 5);
    //获取第3个随机文字
    $imstr[3]["s"] = '=';
    $imstr[3]["x"] = $imstr[2]["x"] + $font_size - 1 + rand(0, 1);
    $imstr[3]["y"] = 3;
    //获取第3个随机文字
    $imstr[4]["s"] = '?';
    $imstr[4]["x"] = $imstr[3]["x"] + $font_size - 1 + rand(0, 1);
    $imstr[4]["y"] = 3;
    //文字
    $text = '';
    //写入随机字串
    for($i = 0; $i < 5; $i++) {
      //获取随机较深颜色
      $text_color = imagecolorallocate($im, rand(50, 180), rand(50, 180), rand(50, 180));
      $text .= $imstr[$i]["s"];
      //画文字
      imagechar($im, $font_size, $imstr[$i]["x"], $imstr[$i]["y"], $imstr[$i]["s"], $text_color);
    }
    session_start();
    $_session[$prefix.'verifycode'] = $firstnum > $secondnum ? ($firstnum - $secondnum) : ($firstnum + $secondnum);
    //显示图片
    imagepng($im);
    //销毁图片
    imagedestroy($im);
  }
  public static function check($code)
  {
    if(trim($_session[$prefix.'verifycode']) == trim($code)) {
      return true;
    } else {
      return false;
    }
  }
}

点击这里复制本文地址 以上内容由Git开源网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!

支持Ctrl+Enter提交

Git开源网_git开源代码资源网_git开源博客 © All Rights Reserved.  
文章中出现的商标及图像版权属于其合法持有人,只供传递信息之用,非商务用途。本站所上传资源,只用于交流学习所用。本站资源收集于网络以及网友投稿,其版权归原创者所有,如有侵害到你的权益,或有不妥之处,请联系我们删除,敬请谅解。Email:admin#gitoscc.com(#更换成@)
Copyright © Git开源网 All rights reserved. 黑ICP备18006154号-1 由ZBlog驱动 | 本站已安全运行0

网站管理