Verification code that has appeared in the back-end video

Source: Internet
Author: User
Tags asin
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn about the verification code class I found on a forum. I modified it myself, the effect is similar to that in the back-end video, but the font will be a little different. I put five fonts in the appendix, which are easy to recognize.

Please study the usage of the class. This is very simple.
/**
* Security verification code
*
* For a secure verification code, the text of the verification code is distorted and rotated. Different fonts are used to add interference codes.
* If you use Chinese as the verification code (I am not here, but you are interested in changing it to Chinese), the security will be better, but the distortion and rotation of the Verification Code are king, having used a font is also a distortion of the font, and I will not add a twist to him any more.
* Configurable attributes are simple and intuitive variables, so I don't need to get a bunch of setter/getter.
*
* @ Author streamline Meng Chun
* @ Copyright NEW BSD
* @ Link http://labs.yulans.cn/YL_Security_Secoder
* @ Link http://wiki.yulans.cn/docs/yl/security/secoder
*
* @ Editor
* @ Copyright Apache
* This file was modified on April 9, December 14, 2013 to beautify the generated verification code and adapt to ThinkPHP.
*/
Class VerifyCode {
/**
* Subscripts of the Verification Code session
*
* @ Var string
*/
Public static $ seKey = 'vcode ';
Public static $ expire = 100; // verification code expiration time (s)
/**
* The characters used in the verification code are easy to confuse with 01IO. We recommend that you do not use them.
*
* @ Var string
*/
Public static $ codeSet = '{89abcdefghjklmnpqrtuvwxy ';
Public static $ fontSize = 25; // The font size of the Verification Code (px)
Public static $ useCurve = true; // whether to draw a confusion Curve
Public static $ useNoise = true; // whether to add a noise
Public static $ imageH = 0; // Verification Code Image Width
Public static $ imageL = 0; // The image length of the Verification Code
Public static $ length = 4; // number of digits of the Verification Code
Public static $ bg = array (243,251,254); // background

Protected static $ _ image = null; // Verification Code image instance
Protected static $ _ color = null; // The font color of the verification code.

Public static function createImage (){
// Image width (px)
Self: $ imageL | self: $ imageL = self: $ length * self: $ fontSize * 1.5 + self: $ fontSize * 1.5;
// Image Height (px)
Self: $ imageH | self: $ imageH = self: $ fontSize * 2;
// Create an image self: $ imageL x self: $ imageH
Self: $ _ image = imagecreate (self: $ imageL, self: $ imageH );
// Set the background
Imagecolorallocate (self: $ _ image, self: $ bg [0], self: $ bg [1], self: $ bg [2]);
// Random font color of the Verification Code
Self: $ _ color = imagecolorallocate (self: $ _ image, mt_rand (1,120), mt_rand (1,120), mt_rand (1,120 ));
// Use a random font for the verification code
$ Ttf = APP_PATH. '/cms/tpl/Index/Public/font/vcode/'. mt_rand (1, 5). '. ttf ';

If (self ::$ useNoise ){
// Scatter
Self: _ writeNoise ();
}
If (self: $ useCurve ){
// Draw interference lines
Self: _ writeCurve ();
}

// Print the verification code
$ Code = array (); // Verification code
$ CodeNX = 0; // left margin of the nth character of the Verification Code
For ($ I = 0; $ I $ Code [$ I] = self: $ codeSet [mt_rand (0, 27)];
$ CodeNX + = mt_rand (self: $ fontSize * 1.2, self: $ fontSize * 1.6 );
// Write a verification code character
Imagettftext (self: $ _ image, self: $ fontSize, mt_rand (-40, 70), $ codeNX, self: $ fontSize * 1.5, self :: $ _ color, $ ttf, $ code [$ I]);
}
// Save the verification code
XS (self: $ seKey, strtolower (join ('', $ code), $ expire );

Header ('cache-Control: private, max-age = 0, no-store, no-Cache, must-revalidate ');
Header ('cache-Control: post-check = 0, pre-check = 0', false );
Header ('pragma: no-cache ');
Header ("content-type: image/png ");

// Output image
Imagepng (self ::$ _ image );
Imagedestroy (self ::$ _ image );
}

/**
* Draw a random sine function curve composed of two connected functions as an interference line (you can change it to a more handsome curve function)
*
* I forgot all the mathematical formulas in high school.
* Sine function analysis: y = Asin (ω x + PHI) + B
* Effects of constant values on function images:
* A: determines the peak value (that is, the multiple of the vertical tensile compression)
* B: the position of the waveform on the Y axis or the vertical moving distance (plus or minus)
* Phi: determines the relationship between the waveform and the X-axis position or the horizontal moving distance (plus or minus the left side)
* ω: determines the cycle (minimum positive cycle T = 2 π/∣ ω ∣)
*
*/
Protected static function _ writeCurve (){
$ A = mt_rand (1, self: $ imageH/2); // Amplitude
$ B = mt_rand (-self: $ imageH/4, self: $ imageH/4); // y axis offset
$ F = mt_rand (-self: $ imageH/4, self: $ imageH/4); // offset of the X axis direction
$ T = mt_rand (self: $ imageH * 1.5, self: $ imageL * 2); // cycle
$ W = (2 * M_PI)/$ T;

$ Px1 = 0; // start position of the curve abscissa
$ Px2 = mt_rand (self: $ imageL/2, self: $ imageL * 0.667); // The end position of the abscissa of the curve.
For ($ px = $ px1; $ px <= $ px2; $ px = $ px + 0.9 ){
If ($ w! = 0 ){
$ Py = $ A * sin ($ w * $ px + $ f) + $ B + self: $ imageH/2; // y = Asin (ω x + PHI) + B
$ I = (int) (self: $ fontSize-6)/4 );
While ($ I> 0 ){
Imagesetpixel (self: $ _ image, $ px + $ I, $ py + $ I, self: $ _ color ); // The image pixels are much better than those of imagettftext and imagestring.
$ I --;
}
}
}

$ A = mt_rand (1, self: $ imageH/2); // Amplitude
$ F = mt_rand (-self: $ imageH/4, self: $ imageH/4); // offset of the X axis direction
$ T = mt_rand (self: $ imageH * 1.5, self: $ imageL * 2); // cycle
$ W = (2 * M_PI)/$ T;
$ B = $ py-$ A * sin ($ w * $ px + $ f)-self: $ imageH/2;
$ Px1 = $ px2;
$ Px2 = self: $ imageL;
For ($ px = $ px1; $ px <= $ px2; $ px = $ px + 0.9 ){
If ($ w! = 0 ){
$ Py = $ A * sin ($ w * $ px + $ f) + $ B + self: $ imageH/2; // y = Asin (ω x + PHI) + B
$ I = (int) (self: $ fontSize-8)/4 );
While ($ I> 0 ){
Imagesetpixel (self: $ _ image, $ px + $ I, $ py + $ I, self: $ _ color); // here (while) the performance of looping pixel points is much better than that of imagettftext and imagestring in the font size.
$ I --;
}
}
}
}

/**
* Miscellaneous
* Write letters or numbers of different colors to the image.
*/
Protected static function _ writeNoise (){
For ($ I = 0; $ I <10; $ I ++ ){
// Miscellaneous color
$ NoiseColor = imagecolorallocate (
Self: $ _ image,
Mt_rand (150,225 ),
Mt_rand (150,225 ),
Mt_rand (150,225)
);
For ($ j = 0; $ j <5; $ j ++ ){
// Scatter
Imagestring (
Self: $ _ image,
5,
Mt_rand (-10, self: $ imageL ),
Mt_rand (-10, self: $ imageH ),
Self: $ codeSet [mt_rand (0, 27)], // The Miscellaneous text is a random letter or number.
$ NoiseColor
);
}
}
}

/**
* Verify that the verification code is correct.
*
* @ Param string $ code user verification code
* @ Return whether the bool user verification code is correct
*/
Public static function check ($ code ){
// The Verification Code cannot be blank.
If (empty ($ code ))
Return false;
If (session (self ::$ seKey) = strtolower ($ code )){
Session ('vcode', null );
Return true;
} Else {
Return false;
}
}
}

Vcode.zip (376.13 KB download: 266 times)

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.