The verification code cannot be displayed ,? Please

Source: Internet
Author: User
The verification code cannot be displayed ,? Please // Verification code
Class ValidateCode {
Private $ charset = 'abcdefghkmnprstuvwxyzabcdefghkmnprstuvwxyz23456789 '; // random factor
Private $ code; // verification code
Private $ codelen = 4; // verification code length
Private $ width = 130; // width
Private $ height = 50; // height
Private $ img; // graphical resource handle
Private $ font; // The specified font.
Private $ fontsize = 20; // specify the font size
Private $ fontcolor; // specifies the font color.

// Constructor initialization
Public function _ construct (){
$ This-> font = ROOT_PATH. '/font/elephant. ttf ';
}

// Generate random code
Private function createCode (){
$ _ Len = strlen ($ this-> charset)-1;
For ($ I = 0; $ I <$ this-> codelen; $ I ++ ){
$ This-> code. = $ this-> charset [mt_rand (0, $ _ len)];
}
}

// Generate background
Private function createBg (){
$ This-> img = imagecreatetruecolor ($ this-> width, $ this-> height );
$ Color = imagecolorallocate ($ this-> img, mt_rand (157,255), mt_rand (157,255), mt_rand (157,255 ));
Imagefilledrectangle ($ this-> img, 0, $ this-> height, $ this-> width, 0, $ color );
}

// Generate text
Private function createFont (){
$ _ X = $ this-> width/$ this-> codelen;
For ($ I = 0; $ I <$ this-> codelen; $ I ++ ){
$ This-> fontcolor = imagecolorallocate ($ this-> img, mt_rand (0,156), mt_rand (0,156), mt_rand (0,156 ));
Imagettftext ($ this-> img, $ this-> fontsize, mt_rand (-30, 30), $ _ x * $ I + mt_rand (1.4), $ this-> height, $ this-> fontcolor, $ this-> font, $ this-> code [$ I]);
}
}

// Generate lines and snowflakes
Private function createLine (){
For ($ I = 0; $ I <6; $ I ++ ){
$ Color = imagecolorallocate ($ this-> img, mt_rand (0,156), mt_rand (0,156), mt_rand (0,156 ));
Imageline ($ this-> img, mt_rand (0, $ this-> width), mt_rand (0, $ this-> height), mt_rand (0, $ this-> width), mt_rand (0, $ this-> height), $ color );
}
For ($ I = 0; I I <100; $ I ++ ){
$ Color = imagecolorallocate ($ this-> img, mt_rand (200,255), mt_rand (200,255), mt_rand (200,255 ));
Imagestring ($ this-> img, mt_rand (1, 5), mt_rand (0, $ this-> width), mt_rand (0, $ this-> height ),'*', $ color );
}
}

// Output
Private function outPut (){
Header ('content-type: image/png ');
Imagepng ($ this-> img );
Imagedestroy ($ this-> img );
}

// Generate externally
Public function doimg (){
$ This-> createBg ();
$ This-> createCode ();
$ This-> createLine ();
$ This-> createFont ();
$ This-> outPut ();
}

// Obtain the verification code
Public function getCode (){
Return strtolower ($ this-> code );
}
}
?>

It is normal to create a file separately and put it in the mvc mode to display the image.
Normal:
Define ('root _ path', dirname (_ FILE __));
Require './core/ValidateCode. class. php'; // first include the class, and modify the actual path according to the actual situation.
$ _ Vc = new ValidateCode (); // instantiate an object
$ _ Vc-> doimg ();
$ _ SESSION ['code'] =_ _ vc-> getCode (); // the verification code is saved to the SESSION.
?>

Display x:

Class UsersController extends Controller {

Public function login (){
$ This-> smarty-> display ('login.html ');
}

Public function verifi (){
$ _ Vc = new ValidateCode (); // instantiate an object
$ _ Vc-> doimg ();
// $ _ SESSION ['code'] =$ _ vc-> getCode (); // the verification code is saved to the SESSION.
}

}

?>


Reply to discussion (solution)

If the code is the same, check the BOM header.

If the code is the same, check the BOM header.



Checked, no bom


If the code is the same, check the BOM header.



Checked, no bom



Do not output images directly and check for errors. Comment out the header () part first and see the error.

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.