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 PHP source code of GIF animation implemented by the GIFEncoder class. if you are interested, you can study it. Effect/*** Image... "> <LINKhref =" http://www.php100.com//s
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 PHP source code of GIF animation implemented by the GIFEncoder class. if you are interested, you can study it.
Effect
/**
* 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 ();
}
You can download the GIFEncoder class from the network ~