This article provides a detailed analysis of php Verification Code (skew, sine interference line, pasting, rotating). For more information, see
This article provides a detailed analysis of php Verification Code (skew, sine interference line, pasting, rotating). For more information, see
I haven't written any post for a long time. I have been busy with new projects. Recently, I have been trying to develop a simple and generous Verification Code Program, which is difficult to identify automatically. After collecting these data, we found that there are several common solutions:
1. Font variants (usually distorted by algorithms. Representative fonts are:
2. paste the font (this is represented by the qq verification code, which is still hard to find online, website space, and crack the qq verification code)
3. Interference line and noise (this recognition is quite easy and easily recognized by programs)
As mentioned above, the second and second methods are difficult to identify. I personally prefer the second method, which does not seem very laborious. The distorted text is strange. Haha, the server space is purely personal.
Implementation Code:
The Code is as follows:
/**
* Text rotation, skew, pasting, and sine interference line verification code *
* @ Version 0.1
* @ Author
* @ Copyright CHENG Mo QQ: 8292669
*/
Class Utils_Caption
{
Var $ Width = 60; // Image Width
Var $ Height = 30; // The Image Height
Var $ Length = 4; // number of digits of the Verification Code
Var $ BgColor = "# FFFFFF"; // background color
Var $ TFonts = array ("font. ttf ");
Var $ TFontSize = array (17,20); // font size range
Var $ TFontAngle = array (-20, 20); // Rotation Angle
Var $ Chars = "0123456789"; // Verification Code range (alphanumeric)
Var $ Code = array (); // Verification Code
Var $ Image = ""; // graphic object
Var $ FontColors = array ('# f36161', '#6bc146', '# 5366bd'); // font color, red, green, and blue
Var $ TPadden = 0.75; // The number of characters in the interval.
Var $ Txbase = 5; // The distance between the two sides of the X axis
Var $ Tybase = 5; // distance between the two sides of the Y axis
Var $ TLine = true; // draw interference lines
Public function RandRSI () // generate Verification Code
{
$ This-> TFontAngle = range ($ this-> TFontAngle [0], $ this-> TFontAngle [1]);
$ This-> TFontSize = range ($ this-> TFontSize [0], $ this-> TFontSize [1]);
$ Arr = array ();
$ Chars = $ this-> Chars;
$ TFontAngle = $ this-> TFontAngle;
$ TFontSize = $ this-> TFontSize;
$ FontColors = $ this-> FontColors;
$ Code = "";
$ Font = dirname (_ FILE _). "/font/". $ this-> TFonts [0];
$ Charlen = strlen ($ Chars)-1;
$ Anglelen = count ($ TFontAngle)-1; // angle range
$ Fontsizelen = count ($ TFontSize)-1; // angle range
$ Fontcolorlen = count ($ FontColors)-1; // angle range
For ($ I = 0; $ I <$ this-> Length; $ I ++) // obtain the character and color
{
$ Char = $ Chars [rand (0, $ charlen)]; // get the character
$ Angle = $ TFontAngle [rand (0, $ anglelen)]; // Rotation angle
$ Fontsize = $ TFontSize [rand (0, $ fontsizelen)]; // font size
$ Fontcolor = $ FontColors [rand (0, $ fontcolorlen)]; // font size
$ Bound = $ this-> _ calculateTextBox ($ fontsize, $ angle, $ font, $ char); // get the range
$ Arr [] = array ($ fontsize, $ angle, $ fontcolor, $ char, $ font, $ bound); // obtain the rectangle.
$ Code. = $ char;
}
$ This-> Code = $ arr; // Verification Code
Return $ code;
}
Public function Draw () // Draw
{
If (empty ($ this-> Code) $ this-> RandRSI ();
$ Codes = $ this-> Code; // user verification Code
$ Wh = $ this-> _ getImageWH ($ codes );
$ Width = $ wh [0];
$ Height = $ wh [1]; // height
$ This-> Width = $ width;
$ This-> Height = $ height;
$ This-> Image = imageCreate ($ width, $ height );
$ Image = $ this-> Image;
$ Back = $ this-> _ getColor2 ($ this-> _ getColor ($ this-> BgColor); // background color
ImageFilledRectangle ($ image, 0, 0, $ width, $ height, $ back); // fill the background
$ TPadden = $ this-> TPadden;
$ Basex = $ this-> Txbase;
$ Color = null;
Foreach ($ codes as $ v) // draw characters one by one
{
$ Bound = $ v [5];
$ Color = $ this-> _ getColor2 ($ this-> _ getColor ($ v [2]);
Imagettftext ($ image, $ v [0], $ v [1], $ basex, $ bound ['height'], $ color, $ v [4], $ v [3]);
$ Basex = $ basex + $ bound ['width'] * $ TPadden-$ bound ['left']; // calculates the next left margin.
}
$ This-> TLine? $ This-> _ wirteSinLine ($ color, $ basex): null; // draw interference lines
Header ("Content-type: image/png ");
Imagepng ($ image );
Imagedestroy ($ image );
}
/**
* Obtain the font rectangle width from the font angle *
*
* @ Param int $ font_size font size
* @ Param float $ font_angle Rotation Angle
* @ Param string $ font_file font file path
* @ Param string $ text write character
* @ Return array returns the length, width, and height.
*/
Private function _ calculateTextBox ($ font_size, $ font_angle, $ font_file, $ text ){
$ Box = imagettfbbox ($ font_size, $ font_angle, $ font_file, $ text );
$ Min_x = min (array ($ box [0], $ box [2], $ box [4], $ box [6]);
$ Max_x = max (array ($ box [0], $ box [2], $ box [4], $ box [6]);
$ Min_y = min (array ($ box [1], $ box [3], $ box [5], $ box [7]);
$ Max_y = max (array ($ box [1], $ box [3], $ box [5], $ box [7]);
Return array (
'Left' => ($ min_x> =-1 )? -Abs ($ min_x + 1): abs ($ min_x + 2 ),
'Top' => abs ($ min_y ),
'Width' => $ max_x-$ min_x,
'Height' => $ max_y-$ min_y,
'Box' => $ box
);
}
Private function _ getColor ($ color) // # ffffff
{
Return array (hexdec ($ color [1]. $ color [2]), hexdec ($ color [3]. $ color [4]), hexdec ($ color [5]. $ color [6]);
}
Private function _ getColor2 ($ color) // # ffffff
{
Return imagecolorallocate ($ this-> Image, $ color [0], $ color [1], $ color [2]);
}
Private function _ getImageWH ($ data)
{
$ TPadden = $ this-> TPadden;
$ W = $ this-> Txbase;
$ H = 0;
Foreach ($ data as $ v)
{
$ W = $ w + $ v [5] ['width'] * $ TPadden-$ v [5] ['left'];
$ H = $ h> $ v [5] ['height']? $ H: $ v [5] ['height'];
}
Return array (max ($ w, $ this-> Width), max ($ h, $ this-> Height ));
}
// Draw a sine disturbance line
Private function _ wirteSinLine ($ color, $ w)
{
$ Img = $ this-> Image;
$ H = $ this-> Height;
$ H1 = rand (-5, 5 );
$ H2 = rand (-1, 1 );
$ W2 = rand (10, 15 );
$ H3 = rand (4, 6 );
For ($ I =-$ w/2; $ I <$ w/2; $ I = $ I + 0.1)
{
$ Y = $ h/$ h3 * sin ($ I/$ w2) + $ h/2 + $ h1;
Imagesetpixel ($ img, $ I + $ w/2, $ y, $ color );
$ H2! = 0? Imagesetpixel ($ img, $ I + $ w/2, $ y + $ h2, $ color): null;
}
}
}
Out-of-band Font:
Font. ttf, a simple bold file.
Note: Let's take a look at the running effect first. You should not be busy copying and running it.
The main feature is: rotating, then sticking, the interference line is that the line width can be changed, and then the sine wave can be changed.
The complexity is: calculateTextBox, which is the width and height after the character is rotated.
Demo:
The Code is as follows: