PHP generate dynamic captcha gif picture, _php tutorial

Source: Internet
Author: User
Tags ord

PHP generates dynamic captcha GIF images,


This is an example of a dynamic CAPTCHA image generated by PHP, and the point is that you can run it! The following first sent:

Here are the related classes and functions that PHP needs to generate the dynamic verification code.

<?php/** *imagecode generates a function that contains a GIF picture of the captcha * @param $string String * @param $width Width * @param $height 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;     Generates a 32-frame animated GIF for ($i =0; $i <32; $i + +) {Ob_start ();     $image =imagecreate ($board _width, $board _height);     Imagecolorallocate ($image, 0,0,0);     Sets the text color array $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);     $color 2=imagecolorallocate ($image, 255,0,0);     Imagefill ($image, 0,0, $gray);       $space =15;//Character Spacing if ($i >0) {//Mask first frame $top = 0; for ($k =0; $k
 
  
Getanimation ();       }/** *gifencoder class **/classes 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 ' supported function for only one image! ", ' ERR01 ' = ' = ' Source is not a GIF IM   age! ", ' 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<&LT;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<< ($this->buf[$i]{10})), $k &0x07, $k, $j + +) {switch ($this =true [$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). "     ";     }} function Gifaddframes ($i, $d) {$Locals _str=13+3* (2 << (ord ($this->buf[$i]{10}));     $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}))); $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}) {for ($j =0; $j < (2<< (Ord. $this buf[$i]{10}) ($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> &GT;16) (&0xff) {$Locals _ext= "!\xf9\x04". Chr (($this->dis<<2) +1). Chr (($d >>0) &0xff). chr ($d & GT;&GT;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) {i F (Gifencoder::gifblockcompare ($Global _rgb, $Locals _rgb, $Global _len)) {$this->gif.= ($Locals _ext. $Locals _img. $L         OCALS_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 {$i +0}!= $Lo calblock{3* $i +0}| | $GlobalBlock {$i +1}!= $LocalBlock {$i +1}| |       $GlobalBlock {$i +2}!= $LocalBlock {$i +2}) {return (0);   }} return (1);   } function Gifword ($int) {return (Chr ($int &0xff). Chr (($int >>8) &0xff));   } function Getanimation () {return ($this->gif);  }  }
 

Example of PHP generated dynamic CAPTCHA usage:

/* Call Example */session_start (); $checkCode = "; $chars = ' abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPRSTUVWXYZ23456789 '; for ($i =0; $i <4; $i + +) {   

The above is how PHP generated dynamic verification code implementation method, I hope that everyone's learning is helpful.

http://www.bkjia.com/PHPjc/1062499.html www.bkjia.com true http://www.bkjia.com/PHPjc/1062499.html techarticle PHP generates a dynamic captcha gif image, which is an example of a dynamic CAPTCHA image generated by PHP, with the emphasis on being able to run Oh! The following first: The following is the PHP generation dynamic test ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.