PHP generates GIF image verification code _php instance

Source: Internet
Author: User
Tags ord rand strlen

Look at the effect chart first


The path to the font and font files needs to be set in $fontfilepath and $fontfilename in the class. Such as:

Copy Code code as follows:

private static $FontFilePath = "static/font/"; Relative to the location of 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 Code code as follows:



? Php

/**
Description: Validation code generation class, support to generate GIF image verification code (with noise, interference line, mesh, random color background, random custom fonts, tilt, gif animation)

Service side:
$mod = strtolower ($_request["mod") isset "mod"]: "");
if ($mod = = "Code") {
echo Securitycode::D Raw (4, 1,,, 5, ten, "Secode");
Die ();
}
Call: *1000)"
Verify:
$reqCode = Strtolower (Isset ($_request["Secode"]) $_request["Secode"]: ""); The requested authentication code
$sessionCode = Strtolower (Isset ($_session["Secode"]) $_session["Secode"]: ""); Session-generated authentication code
if ($reqCode!= $sessionCode) {
echo "Security Authentication code Error! ";
Die ();
}
*/
$mod = strtolower ($_request["mod") isset "mod"]: "");
if ($mod = = "Code") {
echo Securitycode::D Raw (4, MB, 2, "Secode");
Die ();
}

Security Authentication 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");//Array ("1.ttf", "2.ttf", "3.ttf", "4.ttf", "5.ttf", "6.ttf", "7.ttf", "8.ttf");
    private static $Img = ' gif89a ';//gif header 6 bytes   
    PR ivate 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 Authenticode length
      @param int $F generate the number of frames in GIF
      @param int $W width
      @param int $H height
      @param int $MixCnt number of interference lines
      @param int $lineGap grid line interval
      @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, $se Ssionname = "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));
Generate a multiple-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 grid


if ($lineGap &gt; 0) {


for ($m = 0; $m &lt; ($W/$LINEGAP); $m + +) {//vertical bar


Imageline ($img, $m * $lineGap, 0, $m * $lineGap, $H, $noisyColor);


}


for ($n = 0; $n &lt; ($H/$LINEGAP); $n + +) {//Horizontal line


Imageline ($img, 0, $n * $lineGap, $W, $n * $lineGap, $noisyColor);


}


}


Unset ($noisyColor);

Add interference Line


for ($k = 0; $k &lt; $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 (180), rand (180, 270), $lineColor);


Unset ($lineColor);


Unset ($WR, $HR);


}

First frame ignores text


if ($i!= 0 | | $F &lt;= 1) {


Text


$foreColor = Imagecolorallocate ($img, rand (0, 255), rand (0, 255), rand (0, 255));


for ($j = 0; $j &lt; $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 (a), 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 ' <pre> ', Var_dump ($Imdata), ' </pre> ';
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) &amp;&amp;!is_array ($GIF _tim)) {


Throw New Exception (' Error: '. __line__. ', does not supported function for only one image!! ');


Die ();


}


Self:: $LOP = ($GIF _lop &gt; 1)? $GIF _lop:0;


Self:: $DIS = ($GIF _dis &gt; 1)? (($GIF _dis &lt; 3)? $GIF _dis:3): 2;


Self:: $COL = ($GIF _red &gt; 1 &amp;&amp; $GIF _grn &gt;-1 &amp;&amp; $GIF _blu &gt;-1)? ($GIF _red | ($GIF _grn &lt;&lt; 8) | ($GIF _blu &lt;&lt; 16)) :-1;


for ($i = 0, $src _count = count ($GIF _src); $i &lt; $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 &lt;&lt; (Ord (self:: $BUF [$i]{10}) &amp; 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-animation from animated GIF source ('. ($i + 1). ')!');


Die ();


}


Break


Case '; ':


$k = FALSE;


Break


}


}


}


Self::addheader ();


for ($i = 0, $count _buf = count (self:: $BUF); $i &lt; $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 &lt;&lt; (Ord (self:: $BUF [$i]{10}) &amp; 0x07));


$L _end = strlen (self:: $BUF [$i])-$L _str-1;


$L _tmp = substr (self:: $BUF [$i], $L _str, $L _end);


$G _len = 2 &lt;&lt; (ord (self:: $BUF [0]{10}) &amp; 0x07);


$L _len = 2 &lt;&lt; (ord (self:: $BUF [$i]{10}) &amp; 0x07);


$G _rgb = substr (self:: $BUF [0], 3 * (2 &lt;&lt; (Ord (self:: $BUF [0]{10}) &amp; 0x07)));


$L _rgb = substr (self:: $BUF [$i], 3 * (2 &lt;&lt; (Ord (self:: $BUF [$i]{10}) &amp; 0x07));


$L _ext = "!\xf9\x04". Chr (self:: $DIS &lt;&lt; 2) + 0). Chr ($d &gt;&gt; 0) &amp; 0xFF). Chr ($d &gt;&gt; 8) &amp; 0xFF). "\x0\x0";


if (self:: $COL &gt; 1 &amp;&amp; ord (self:: $BUF [$i]{10}) &amp; 0x80) {


for ($j = 0; $j &lt; (2 &lt;&lt; (Ord (self:: $BUF [$i]{10}) &amp; 0x07); $j + +) {


if (ORD ($L _rgb{3 * $j + 0}) = = (self:: $COL &gt;&gt; 0) &amp; 0xFF &amp;&amp; ord ($L _rgb{3 * $j + 1}) = = (self:: $COL &GT;&G T 8 &amp; 0xFF &amp;&amp; ord ($L _rgb{3 * $j + 2}) = = (self:: $COL &gt;&gt;) &amp; 0xFF) {


$L _ext = "!\xf9\x04". Chr (self:: $DIS &lt;&lt; 2) + 1). Chr ($d &gt;&gt; 0) &amp; 0xFF). Chr ($d &gt;&gt; 8) &amp; 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, strlen ($L _tmp)-10);


Break


}


if (Ord (self:: $BUF [$i]{10}) &amp; 0x80 &amp;&amp; self:: $IMG &gt;-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 &amp;= 0xf8;


$byte |= (Ord (self:: $BUF [0]{10}) &amp; 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 &amp;= 0xf8;


$byte |= (Ord (self:: $BUF [$i]{10}) &amp; 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.