PHP generates GIF dynamic picture verification code

Source: Internet
Author: User
Tags chr ord rand strlen
The code is as follows Copy Code

<?php
/**
* Call Example
* */
Session_Start ();
$randCode = ';
$chars = ' abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPRSTUVWXYZ23456789 ';
for ($i = 0; $i < 4; $i + +)
{
$randCode. = substr ($chars, Mt_rand (0, strlen ($chars)-1), 1);
}
$_session[' Code ' = Strtoupper ($randCode); Record session
Imagecode ($randCode, 60); Show GIF animation

/**
* 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 [$colorRand OM]);
}
}
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
* */
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 isn't ' a GIF image! ',
' ERR02 ' => "unintelligible flag",
' ERR03 ' => ' could not ' animation from animated GIF source '
);

function Gifencoder ($GIF _src, $GIF _dly = $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 = (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], $cmap);
$this->gif. = "!37713netscape2.031". Gifencoder::gifword ($this->lop). "";
}
}

function Gifaddframes ($i, $d)
{

$Locals _STR = + 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], 3 * (2 << (Ord ($this->buf[0]{10}) & 0x07));
$Locals _rgb = substr ($this->buf[$i], 3 * (2 << (ord ($this->buf[$i]{10}) & 0x07));

$Locals _ext = "!xf9x04". Chr ($this->dis << 2) + 0). Chr ($d >> 0) & 0xFF). Chr ($d >> 8) & 0xFF). "x0x0";

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 = "!xf9x04". 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, strlen ($Locals _tmp)-18);
Break
Case ",":
$Locals _img = substr ($Locals _tmp, 0, 10);
$Locals _tmp = substr ($Locals _tmp, 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 & 0xFF) Chr (($int >> 8) & 0xFF);
}

function Getanimation ()
{
Return ($this->gif);
}
}

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.