Php verification code (skew, sine interference line, pasting, rotating) _ PHP Tutorial

Source: Internet
Author: User
Tags getcolor
Php verification code (tilt, sine interference line, pasting, rotating ). 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, I have not posted 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 variant (generally through the algorithm, distortion, representative is: http://code.google.com/p/cool-php-captcha/

2. paste the font (this is represented by the qq verification code. it is still hard to find on the Internet to 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, it's purely personal.
Implementation code:

The code is as follows:


/**
* Text rotation, skew, pasting, and sine interference line verification code *
* @ Version 0.1
* @ Author http://www.cnblogs.com/chengmo
* @ 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, and we should not be busy copying and running.


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:


$ Rsi = new Utils_Caption ();
$ Rsi-> TFontSize = array (15, 17 );
$ Rsi-> Width = 50;
$ Rsi-> Height = 25;
$ Code = $ rsi-> RandRSI ();
Session_start ();
$ _ SESSION ["CHECKCODE"] = $ code;
$ Rsi-> Draw ();


Well, let's write this. there are still many shortcomings in the code.
Click to download the code

Bytes. 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. Sort and collect these data and send them...

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.