PHP uses the GIF dynamic image verification code generated by the GIFEncoder class _ PHP Tutorial

Source: Internet
Author: User
PHP uses the GIF dynamic image verification code generated by the GIFEncoder class. I believe many people have thought about how to use PHP to generate GIF animations to implement dynamic image verification codes. The following is the implementation process. The ImageCode function uses the GIF animation PHP source code implemented by the GIFEncoder class. I believe many people have thought about how to use PHP to generate a GIF animation to implement a dynamic image verification code. The following is the implementation process.
The ImageCode function uses the PHP source code of GIF animation implemented by the GIFEncoder class. if you are interested, you can study it.

Effect

The code is as follows:


/**

* Generate GIF image verification using ImageCode

* @ Param $ string

* @ Param $ width

* @ Param $ height

**/

Function ImageCode ($ string = '', $ width = 75, $ height = 25)

{

$ Authstr = $ string? $ String: (time () % 2 = 0 )? Mt_rand (1000,999 9): mt_rand (10000,999 99 ));

$ Board_width = $ width;

$ Board_height = $ height;

// Generate a 32-frame GIF animation

For ($ I = 0; $ I <32; $ I ++)

{

Ob_start ();

$ Image = imagecreate ($ board_width, $ board_height );

Imagecolorallocate ($ image, 0, 0 );

// Set the text color array

$ ColorList [] = ImageColorAllocate ($ image, 210 );

$ ColorList [] = ImageColorAllocate ($ image, 0, 64, 0 );

$ ColorList [] = ImageColorAllocate ($ image, 0, 64 );

$ ColorList [] = ImageColorAllocate ($ image, 0,128,128 );

$ ColorList [] = ImageColorAllocate ($ image, 27,52, 47 );

$ ColorList [] = ImageColorAllocate ($ image, 102 );

$ ColorList [] = ImageColorAllocate ($ image, 145 );

$ ColorList [] = ImageColorAllocate ($ image, 113 );

$ ColorList [] = ImageColorAllocate ($ image, 0, 51, 51 );

$ ColorList [] = ImageColorAllocate ($ image, 158,180, 35 );

$ ColorList [] = ImageColorAllocate ($ image, 59,59, 59 );

$ ColorList [] = ImageColorAllocate ($ image, 0, 0 );

$ ColorList [] = ImageColorAllocate ($ image, 1,128,180 );

$ ColorList [] = ImageColorAllocate ($ image, 0,153, 51 );

$ ColorList [] = ImageColorAllocate ($ images, 60,131, 1 );

$ ColorList [] = ImageColorAllocate ($ image, 0, 0 );

$ Fontcolor = ImageColorAllocate ($ image, 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; // character spacing

If ($ I> 0) // shield the first frame

{

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]);

}

// Add interference lines

For ($ k = 0; $ k <3; $ k ++)

{

$ ColorRandom = mt_rand (0, sizeof ($ colorList)-1 );

// $ Todrawline = rand (0, 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 ();

}

Source code of the GIFEncoder class:

The code is as follows:


<? Php
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' => "cocould not make animation from animated GIF source ",
);
/*
:::::::::::::::::::::::::::::::::::::::: :::::::::::
::
: GIFEncoder...
::
*/
Function GIFEncoder (
$ GIF_src, $ GIF_dly, $ GIF_lop, $ GIF_dis,
$ GIF_red, $ GIF_grn, $ GIF_blu, $ GIF_mod
){
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; $ I <count ($ GIF_src); $ I ++ ){
If (strToLower ($ GIF_mod) = "url "){
$ This-> BUF [] = fread (fopen ($ GIF_src [$ I], "rb"), filesize ($ GIF_src [$ I]);
}
Else if (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; $ I <count ($ this-> BUF); $ I ++ ){
GIFEncoder: GIFAddFrames ($ I, $ GIF_dly [$ I]);
}
GIFEncoder: GIFAddFooter ();
}
/*
:::::::::::::::::::::::::::::::::::::::: :::::::::::
::
: GIFAddHeader...
::
*/
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/13 NETSCAPE2. 0/3/1 ". GIFEncoder: GIFWord ($ this-> LOP)."/0 ";
}
}
/*
:::::::::::::::::::::::::::::::::::::::: :::::::::::
::
: GIFAddFrames...
::
*/
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;
}
/*
:::::::::::::::::::::::::::::::::::::::: :::::::::::
::
: GIFAddFooter...
::
*/
Function GIFAddFooter (){
$ This-> GIF. = ";";
}
/*
:::::::::::::::::::::::::::::::::::::::: :::::::::::
::
: GIFBlockCompare...
::
*/
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 );
}
/*
:::::::::::::::::::::::::::::::::::::::: :::::::::::
::
: GIFWord...
::
*/
Function GIFWord ($ int ){
Return (chr ($ int & 0xFF). chr ($ int> 8) & 0xFF ));
}
/*
:::::::::::::::::::::::::::::::::::::::: :::::::::::
::
: GetAnimation...
::
*/
Function GetAnimation (){
Return ($ this-> GIF );
}
}
?>

Bytes. The ImageCode function uses the GIF animation PHP source code implemented by the GIFEncoder class...

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.