PHP generates GIF image verification code _php instance

Source: Internet
Author: User
Look first.


The path to the font and font files needs to be set in $fontfilepath and $fontfilename in the class. Such as:
Copy the Code code as follows:
private static $FontFilePath = "static/font/"; Location relative to this code file
private static $FontFileName = Array ("3.ttf");//Array ("1.ttf", "2.ttf", "3.ttf", "4.ttf", "5.ttf", "6.ttf", "7.ttf", "8.t" TF "); //

The complete code is as follows:
Copy the Code code as follows:

/**
Description: Verification code generation class, support to generate GIF image verification code (with noise, interference lines, grid, random color background, random custom font, tilt, gif animation)

Service side:
$mod = Strtolower (isset ($_request["mod"])? $_request["mod"]: "");
if ($mod = = "Code") {
echo Securitycode::D Raw (4, 1, +, +, 5, ten, +, "Secode");
Die ();
}
Call:
Verify:
$reqCode = Strtolower (Isset ($_request["Secode"])? $_request["Secode"]: ""); The requested verification code
$sessionCode = Strtolower (Isset ($_session["Secode"])? $_session["Secode"]: ""); Verification code generated by the session
if ($reqCode! = $sessionCode) {
echo "Security Captcha Error! ";
Die ();
}
*/
$mod = Strtolower (isset ($_request["mod"])? $_request["mod"]: "");
if ($mod = = "Code") {
echo Securitycode::D Raw (4, 2, +, "Secode");
Die ();
}

Security Verification Code Class
Class Securitycode {

private static $Debug = 0;
private static $Code = ';
private static $Chars = ' bcdefhkmnrstuvwxyABCDEFGHKMNPRSTUVWXY34568 ';
//private static $Chars = ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890 ';
private static $TextGap = 20;
private static $TextMargin = 5;
private static $FontFilePath = "static/font/";//relative to the location of this code file
private static $FontFileName =array ("3.ttf");//arr Ay ("1.ttf", "2.ttf", "3.ttf", "4.ttf", "5.ttf", "6.ttf", "7.ttf", "8.ttf");
private static $IMG = ' gif89a ';//gif header 6 bytes
private static $BUF = Array ();
private static $LOP = 0;
private static $DIS = 2;
private static $COL =-1;
private static $IMG =-1;

/**
Generate GIF picture validation
@param int $L Verification code length
@param int $F The number of frames generated for the GIF graph
@param int $W Width
@param int $H Height
@param int $MixCnt number of interference lines
@param int $lineGap grid line spacing
@param int $noisyCnt Bath points
@param int $sessionName Verification code session name
*/
public static function Draw ($L = 4, $F = 1, $W = $H, $MixCnt = 2, $lineGap = 0, $noisyCnt = ten, $sessionName = " Code ") {
Ob_start ();
Ob_clean ();

for ($i = 0; $i < $L; $i + +) {
Self:: $Code. = SubStr (self:: $Chars, Mt_rand (0, strlen (self:: $Chars)-1), 1);
}

if (!isset ($_session))
Session_Start ();
$_session[$sessionName] = Strtolower (self:: $Code);

$bgRGB = Array (rand (0, 255), rand (0, 255), rand (0, 255));
Create a multi-frame GIF animation
for ($i = 0; $i < $F; $i + +) {
$img = Imagecreate ($W, $H);

Background color
$bgColor = Imagecolorallocate ($img, $bgRGB [0], $bgRGB [1], $bgRGB [2]);
Imagecolortransparent ($img, $bgColor);
Unset ($bgColor);

Add a noise point
$maxNoisy = rand (0, $NOISYCNT);
$noisyColor = Imagecolorallocate ($img, rand (0, 255), rand (0, 255), rand (0, 255));
for ($k = 0; $k <= $maxNoisy; $k + +) {
Imagesetpixel ($img, rand (0, $W), rand (0, $H), $noisyColor);
}

Add Mesh
if ($lineGap > 0) {
for ($m = 0; $m < ($W/$LINEGAP); $m + +) {//vertical bar
Imageline ($img, $m * $lineGap, 0, $m * $lineGap, $H, $noisyColor);
}
for ($n = 0; $n < ($H/$LINEGAP); $n + +) {//Horizontal line
Imageline ($img, 0, $n * $lineGap, $W, $n * $lineGap, $noisyColor);
}
}
Unset ($noisyColor);

//Add interference line
for ($k = 0; $k < $MixCnt; $k + +) {
$wr = Mt_rand (0, $W);
$hr = Mt_rand (0, $W);
$lineColor = Imagecolorallocate ($img, rand (0, 255), rand (0, 255), rand (0, 255));
Imagearc ($img, $W-floor ($WR/2), Floor ($HR/2), $WR, $hr, rand (+), rand (+), $lineColor);
Unset ($lineColor);
unset ($WR, $HR);
}

First frame ignores text
if ($i! = 0 | | $F <= 1) {
Text
$foreColor = Imagecolorallocate ($img, rand (0, 255), rand (0, 255), rand (0, 255));
for ($j = 0; $j < $L; $j + +) {
$fontFile = self:: $FontFilePath. Self:: $FontFileName [Rand (0, COUNT (self:: $FontFileName)-1)];
if (!file_exists ($fontFile))
Imagestring ($IMG, 4, Self:: $TextMargin + $j * Self:: $TextGap, ($H-rand ($H/2, $H)), Self:: $Code [$j], $foreColor);
Else
Imagettftext ($img, rand (+), rand ( -15, +), self:: $TextMargin + $j * Self:: $TextGap, ($H-rand (7)), $foreColor, $ Fontfile, Self:: $Code [$j]);
}
Unset ($foreColor);
}

Imagegif ($IMG);
Imagedestroy ($IMG);
$Imdata [] = Ob_get_contents ();
Ob_clean ();
}

Unset ($W, $H, $B);
if (self:: $Debug) {
echo $_session[' code ';
Echo '

', Var_dump ($Imdata), '
';
Die ();
}
Header (' content-type:image/gif ');
Return Self::creategif ($Imdata, 20);
Unset ($Imdata);
}

private static function Creategif ($GIF _src, $GIF _dly = ten, $GIF _lop = 0, $GIF _dis = 0, $GIF _red = 0, $GIF _grn = 0, $GIF _bl U = 0, $GIF _mod = ' bin ') {
if (!is_array ($GIF _src) &&!is_array ($GIF _tim)) {
Throw New Exception (' Error: '. __line__. ', Does not supported function for only one image!! ');
Die ();
}
Self:: $LOP = ($GIF _lop >-1)? $GIF _lop:0;
Self:: $DIS = ($GIF _dis >-1)? (($GIF _dis < 3) $GIF _dis:3): 2;
Self:: $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 ') {
Self:: $BUF [] = Fread (fopen ($GIF _src[$i], ' RB '), FileSize ($GIF _src[$i]));
} elseif (Strtolower ($GIF _mod) = = ' bin ') {
Self:: $BUF [] = $GIF _src[$i];
} else {
Throw New Exception (' Error: '. __line__. ', unintelligible flag ('. $GIF _mod. ')!');
Die ();
}
if (! ( Substr (self:: $BUF [$i], 0, 6) = = ' gif87a ' Or Substr (self:: $BUF [$i], 0, 6) = = ' gif89a ')} {
Throw New Exception (' Error: '. __line__. ', Source '. $i. ' is not a GIF image! ');
Die ();
}
for ($j = (3 * (2 << (ord:: $BUF [$i]{10}) & 0x07)), $k = TRUE; $k; $j + +) {
Switch (self:: $BUF [$i]{$j}) {
Case '! ':
if (substr (self:: $BUF [$i], ($j + 3), 8)) = = ' NETSCAPE ') {
Throw New Exception (' Error: '. __line__. ', Could not make animation from animated GIF source ('. ($i + 1). ')!');
Die ();
}
Break
Case '; ':
$k = FALSE;
Break
}
}
}
Self::addheader ();
for ($i = 0, $count _buf = count (self:: $BUF); $i < $count _buf; $i + +) {
Self::addframes ($i, $GIF _dly);
}
Self:: $Img. = '; ';
Return (self:: $IMG);
}

private static function AddHeader () {
$i = 0;
if (Ord (self:: $BUF [0]{10}) & 0x80) {
$i = 3 * (2 << (Ord (self:: $BUF [0]{10}) & 0x07));
Self:: $Img. = substr (self:: $BUF [0], 6, 7);
Self:: $Img. = substr (self:: $BUF [0], $i);
Self:: $Img. = "!\377\13netscape2.0\3\1". Chr (self:: $LOP & 0xFF). Chr (self:: $LOP >> 8) & 0xFF). "The";
}
Unset ($i);
}

private static function Addframes ($i, $d) {
$L _STR = 3 * (2 << (Ord (self:: $BUF [$i]{10}) & 0x07));
$L _end = strlen (self:: $BUF [$i])-$L _str-1;
$L _tmp = substr (self:: $BUF [$i], $L _str, $L _end);
$G _len = 2 << (ord (self:: $BUF [0]{10}) & 0x07);
$L _len = 2 << (ord (self:: $BUF [$i]{10}) & 0x07);
$G _rgb = substr (self:: $BUF [0], and 3 * (2 << (Ord (self:: $BUF [0]{10}) & 0x07)));
$L _rgb = substr (self:: $BUF [$i], 3 * (2 << (Ord (self:: $BUF [$i]{10}) & 0x07)));
$L _ext = "!\xf9\x04". Chr (self:: $DIS << 2) + 0). Chr (($d >> 0) & 0xFF). Chr (($d >> 8) & 0xFF). "\x0\x0";
if (self:: $COL >-1 && ord (self:: $BUF [$i]{10}) & 0x80) {
for ($j = 0, $j < (2 << (Ord (self:: $BUF [$i]{10}) & 0x07), $j + +) {
if (ORD ($L _rgb{3 * $j + 0}) = = (self:: $COL >> 0) & 0xFF && ord ($L _rgb{3 * $j + 1}) = = (self:: $COL &GT;&G T 8) & 0xFF && ord ($L _rgb{3 * $j + 2}) = = (self:: $COL >> +) & 0xFF) {
$L _ext = "!\xf9\x04". Chr (self:: $DIS << 2) + 1). Chr (($d >> 0) & 0xFF). Chr (($d >> 8) & 0xFF). Chr ($j). "\x0";
Break
}
}
}
Switch ($L _tmp{0}) {
Case '! ':
$L _img = substr ($L _tmp, 8, 10);
$L _tmp = substr ($L _tmp, strlen ($L _tmp)-18);
Break
Case ', ':
$L _img = substr ($L _tmp, 0, 10);
$L _tmp = substr ($L _tmp, ten, strlen ($L _tmp)-10);
Break
}
if (Ord (self:: $BUF [$i]{10}) & 0x80 && self:: $IMG >-1) {
if ($G _len = = $L _len) {
if (Self::compare ($G _rgb, $L _rgb, $G _len)) {
Self:: $Img. = ($L _ext. $L _img. $L _tmp);
} else {
$byte = Ord ($L _img{9});
$byte |= 0x80;
$byte &= 0xF8;
$byte |= (Ord (self:: $BUF [0]{10}) & 0x07);
$L _img{9} = Chr ($byte);
Self:: $Img. = ($L _ext. $L _img. $L _rgb. $L _tmp);
}
} else {
$byte = Ord ($L _img{9});
$byte |= 0x80;
$byte &= 0xF8;
$byte |= (Ord (self:: $BUF [$i]{10}) & 0x07);
$L _img{9} = Chr ($byte);
Self:: $Img. = ($L _ext. $L _img. $L _rgb. $L _tmp);
}
} else {
Self:: $Img. = ($L _ext. $L _img. $L _tmp);
}
Self:: $IMG = 1;
}

private static function Compare ($G _block, $L _block, $Len) {
for ($i = 0; $i < $Len; $i + +) {
if ($G _block{3 * $i + 0}! = $L _block{3 * $i + 0} | | $G _block{3 * $i + 1}! = $L _block{3 * $i + 1} | | $G _block{3 * $i + 2}! = $L _block{3 * $i + 2}) {
return (0);
}
}
return (1);
}

}

The usage is in the comment at the beginning of the 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.