Php verification code with Chinese letters, numbers, and custom fonts-php Tutorial

Source: Internet
Author: User
Supports php verification code for Chinese letters, numbers, and custom fonts. For more information, see The code is as follows:


/*
* Captcha Class base on php gd Lib
* @ Author Design
* @ Version 1.0
* @ Demo
* Include ('captchaclass. php ');
* $ CaptchaDemo = new Captcha ();
* $ CaptchaDemo-> createImage ();
*/
Class Captcha {
// @ Define the image height of the verification code
Private $ height;
// @ Define the image width of the verification code
Private $ width;
// @ Defines the number of characters in the verification code
Private $ textNum;
// @ Define the verification code characters
Private $ textContent;
// @ Define the character color
Private $ fontColor;
// @ Define random text color
Private $ randFontColor;
// @ Define the font size
Private $ fontSize;
// @ Define the font
Private $ fontFamily;
// @ Defines the background color
Private $ bgColor;
// @ Defines the random background color
Private $ randBgColor;
// @ Defines the character language
Private $ textLang;
// @ Defines the number of interference points
Private $ noisePoint;
// @ Define the number of interference lines
Private $ noiseLine;
// @ Define whether the object is distorted
Private $ distortion;
// @ Define the distorted image source
Private $ distortionImage;
// @ Defines whether a border exists
Private $ showBorder;
// @ Define the verification code Image Source
Private $ image;

// @ Constructor
Public function Captcha (){
$ This-> textNum = 4;
$ This-> fontSize = 16;
$ This-> fontFamily = 'C: \ windows \ fontsSIMYOU. ttf'; // you can change the Chinese font to a linux directory.
$ This-> textLang = 'en ';
$ This-> noisePoint = 30;
$ This-> noiseLine = 3;
$ This-> distortion = false;
$ This-> showBorder = false;
}



// @ Set the image width
Public function setWidth ($ w ){
$ This-> width = $ w;
}

// @ Set the image height
Public function setHeight ($ h ){
$ This-> height = $ h;
}

// @ Set the number of characters
Public function setTextNumber ($ textN ){
$ This-> textNum = $ textN;
}

// @ Set the character color
Public function setFontColor ($ fc ){
$ This-> fontColor = sscanf ($ fc, '# % 2x % 2x % 2x ');
}

// @ Set the font size
Public function setFontSize ($ n ){
$ This-> fontSize = $ n;
}

// @ Set the font
Public function setFontFamily ($ ffUrl ){
$ This-> fontFamily = $ ffUrl;
}

// @ Set the character language
Public function setTextLang ($ lang ){
$ This-> textLang = $ lang;
}

// @ Set the image background
Public function setBgColor ($ bc ){
$ This-> bgColor = sscanf ($ bc, '# % 2x % 2x % 2x ');
}

// @ Set the number of interference points
Public function setNoisePoint ($ n ){
$ This-> noisePoint = $ n;
}

// @ Set the number of interference lines
Public function setNoiseLine ($ n ){
$ This-> noiseLine = $ n;
}

// @ Set whether to distort
Public function setDistortion ($ B ){
$ This-> distortion = $ B;
}

// @ Set whether to display the border
Public function setShowBorder ($ border ){
$ This-> showBorder = $ border;
}

// @ Initialize the verification code Image
Public function initImage (){
If (empty ($ this-> width) {$ this-> width = floor ($ this-> fontSize * 1.3) * $ this-> textNum + 10 ;}
If (empty ($ this-> height) {$ this-> height = $ this-> fontSize * 2 ;}
$ This-> image = imagecreatetruecolor ($ this-> width, $ this-> height );
If (empty ($ this-> bgColor )){
$ This-> randBgColor = imagecolorallocate ($ this-> image, mt_rand (100,255), mt_rand (100,255), mt_rand (100,255 ));
} Else {
$ This-> randBgColor = imagecolorallocate ($ this-> image, $ this-> bgColor [0], $ this-> bgColor [1], $ this-> bgColor [2]);
}
Imagefill ($ this-> image, 0, 0, $ this-> randBgColor );
}

// @ Generate random characters
Public function randText ($ type ){
$ String = '';
Switch ($ type ){
Case 'en ':
$ Str = 'abcdefghjklmnpqrstuvwxy3456789 ';
For ($ I = 0; $ I <$ this-> textNum; $ I ++ ){
$ String = $ string. ','. $ str [mt_rand (0, 29)];
}
Break;
Case 'cn ':
For ($ I = 0; $ I <$ this-> textNum; $ I ++ ){
$ String = $ string. ','. chr (rand (0xB0, 0xCC). chr (rand (0xA1, 0xBB ));
}
$ String = iconv ('gb2312', 'utf-8', $ string); // Convert the code to utf8
Break;
}
Return substr ($ string, 1 );
}

// @ Output text to verification code
Public function createText (){
$ TextArray = explode (',', $ this-> randText ($ this-> textLang ));
$ This-> textContent = join ('', $ textArray );
If (empty ($ this-> fontColor )){
$ This-> randFontColor = imagecolorallocate ($ this-> image, mt_rand (0,100), mt_rand (0,100), mt_rand (0,100 ));
} Else {
$ This-> randFontColor = imagecolorallocate ($ this-> image, $ this-> fontColor [0], $ this-> fontColor [1], $ this-> fontColor [2]);
}
For ($ I = 0; $ I <$ this-> textNum; $ I ++ ){
$ Angle = mt_rand (-) * mt_rand );
Imagettftext ($ this-> image, $ this-> fontSize, $ angle, 5 + $ I * floor ($ this-> fontSize * 1.3 ), floor ($ this-> height * 0.75), $ this-> randFontColor, $ this-> fontFamily, $ textArray [$ I]);
}
}

// @ Generate interference points
Public function createNoisePoint (){
For ($ I = 0; $ I <$ this-> noisePoint; $ I ++ ){
$ PointColor = imagecolorallocate ($ this-> image, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagesetpixel ($ this-> image, mt_rand (0, $ this-> width), mt_rand (0, $ this-> height), $ pointColor );
}

}

// @ Generate interference line
Public function createNoiseLine (){
For ($ I = 0; $ I <$ this-> noiseLine; $ I ++ ){
$ LineColor = imagecolorallocate ($ this-> image, mt_rand (0,255), mt_rand (0,255), 20 );
Imageline ($ this-> image, 0, mt_rand (0, $ this-> width), $ this-> width, mt_rand (0, $ this-> height ), $ lineColor );
}
}

// @ Distort text
Public function distortionText (){
$ This-> distortionImage = imagecreatetruecolor ($ this-> width, $ this-> height );
Imagefill ($ this-> distortionImage, 0, 0, $ this-> randBgColor );
For ($ x = 0; $ x <$ this-> width; $ x ++ ){
For ($ y = 0; $ y <$ this-> height; $ y ++ ){
$ RgbColor = imagecolorat ($ this-> image, $ x, $ y );
Imagesetpixel ($ this-> distortionImage, (int) ($ x + sin ($ y/$ this-> height * 2 * M_PI-M_PI * 0.5) * 3), $ y, $ rgbColor );
}
}
$ This-> image = $ this-> distortionImage;
}

// @ Generate verification code Image
Public function createImage (){
$ This-> initImage (); // create a basic image
$ This-> createText (); // output the verification code character
If ($ this-> distortion) {$ this-> distortionText ();} // distort text
$ This-> createNoisePoint (); // generates interference points
$ This-> createNoiseLine (); // generates interference lines
If ($ this-> showBorder) {imagerectangle ($ this-> image, 0, 0, $ this-> width-1, $ this-> height-1, $ this-> randFontColor);} // add a border
Imagepng ($ this-> image );
Imagedestroy ($ this-> image );
If ($ this-> distortion) {imagedestroy ($ this-> $ distortionImage );}
Return $ this-> textContent;
}

}
?> Usage:

// Session_start ();
Header ("Content-type: image/png ");
Include ('captcha5 _ class. php ');
$ Captcha5 = new Captcha ();

// @ Set the verification code width
// $ Captcha5-> setWidth (200 );

// @ Set the verification code height
// $ Captcha5-> setHeight (50 );

// @ Set the number of characters
$ Captcha5-> setTextNumber (5 );

// @ Set the character color
// $ Captcha5-> setFontColor ('# ff9900 ');

// @ Set the font size
// $ Captcha5-> setFontSize (25 );

// @ Set the font
$ Captcha5-> setFontFamily ('C: \ windows \ fonts \ STXINGKA. ttf ');

// @ Set language
$ Captcha5-> setTextLang ('cn ');

// @ Set the background color
// $ Captcha5-> setBgColor ('#000000 ');

// @ Set the number of interference points
// $ Captcha5-> setNoisePoint (600 );

// @ Set the number of interference lines
// $ Captcha5-> setNoiseLine (10 );

// @ Set whether to distort
// $ Captcha5-> setDistortion (true );

// @ Set whether to display the border
$ Captcha5-> setShowBorder (true );

// Output the verification code
$ Code = $ captcha5-> createImage ();
// $ _ SESSION ['captchacode'] ['content'] = $ code;
// $ _ SESSION ['captchacode'] ['Time'] = microtime ();
?>

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.