I believe many people have thought about how to use PHP to generate GIF animation to achieve dynamic picture verification code, the following is the implementation process.
Imagecode function through the Gifencoder class to achieve the GIF animation of the PHP source code, interested friends can study.
effect as shown:
/**
* Imagecode generated GIF image validation
* @param $string string
* @param $width width
* @param $height Height
* */
function Imagecode ($string = ', $width = +, $height = 25)
{
$authstr = $string? $string: ((Time ()% 2 = 0)? Mt_rand (1000, 9999): Mt_rand (10000, 99999));
$board _width = $width;
$board _height = $height;
Generate a 32-frame GIF animation
for ($i = 0; $i < $i + +)
{
Ob_start ();
$image = Imagecreate ($board _width, $board _height);
Imagecolorallocate ($image, 0,0,0);
Set 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)//Shield 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 < $k + +)
{
$colorRandom = Mt_rand (0,sizeof ($colorList)-1);
Imagesetpixel ($image, Rand ()%70, Rand ()%15, $colorList [$colorRandom]);
}
Add interference Line
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 [$colorRando M]);
}
}
Imagegif ($image);
Imagedestroy ($image);
$imagedata [] = Ob_get_contents ();
Ob_clean ();
+ + $i;
}
$gif = new Gifencoder ($imagedata);
Header (' content-type:image/gif ');
echo $gif->getanimation ();
}
About the Gifencoder class, can be downloaded in the network ~