The verification code image generated by php is not displayed.

Source: Internet
Author: User

Today, when I was working on a php verification code program, I found that the generated image was not displayed. I thought it was because the php gd library was not opened. I checked it with phpinfo, the following small series will introduce the solution to this problem.

After the bom is cleared, the code is written at the beginning, and the gd library is also enabled. From here, it is estimated that it is not a problem with the gd library, but it may be in the code of the program.

Code for generating the verification code:

The Code is as follows: Copy code

<? Php
/*
* Verification Code Generation Program
*/
$ Letter = '';
// Obtain random numbers
For ($ I = 0; $ I <2; $ I ++ ){
$ Letter. = chr (mt_rand (48, 57 ));
}
// Obtain random letters
For ($ I = 0; $ I <2; $ I ++ ){
$ Letter. = chr (mt_rand (65,90 ));
}
// Reconstruct the Character Sequence
$ Strs = str_split ($ letter );
Shuffle ($ strs );
$ Rndstring = "";
While (list (, $ str) = each ($ strs )){
$ Rndstring. = $ str;
}


// If GD is supported, draw
If (function_exists ("imagecreate "))
{
// Write cookie to the browser
Setcookie ("zjs_ckstr", md5 (strtolower ($ rndstring), time () + 300, '/'); // The verification code is valid for 5 minutes
$ Rndcodelen = strlen ($ rndstring );
// Image size
$ Im = imagecreate (100,30 );
// $ Im = imagecreatefromgif ("code.gif ");
// Font
$ Font_type = dirname (_ FILE _). "/data/font/AvantGardeBookBT. ttf ";
// Background color
$ Backcolor = imagecolorallocate ($ im, 255,255,255 );
// Font color
// Imagettftext is not supported
$ FontColor = ImageColorAllocate ($ im, 0, 0 );
// Supports imagettftext
$ FontColor2 = ImageColorAllocate ($ im, 0, 0 );
// Shadow
$ FontColor1 = ImageColorAllocate ($ im, 255,255, 25 );
// Add background noise
$ PixColor = imagecolorallocate ($ im, 199,199,199); // miscellaneous color
For ($ j = 0; $ j <1000; $ j ++ ){
Imagesetpixel ($ im, rand (0,100), rand (0, 30), $ pixColor );
}
// Add a background line
For ($ j = 0; $ j <= 3; $ j ++ ){
// Background line color
$ LineColor1 = ImageColorAllocate ($ im, rand (0,255), rand (0,255), rand (0,255 ));
// Size of the background line direction
Imageline ($ im, rand (), $ lineColor1 );
}


$ Strposs = array ();
// Text
For ($ I = 0; $ I <$ rndcodelen; $ I ++ ){
If (function_exists ("imagettftext ")){
$ Strposs [$ I] [0] = $ I * 16 + 17; // X axis
$ Strposs [$ I] [1] = mt_rand (20, 23); // y axis
Imagettftext ($ im, 5, 5, $ strposs [$ I] [0] + 1, $ strposs [$ I] [1] + 1, $ fontColor1, $ font_type, $ rndstring [$ I]);
} Else {
Imagestring ($ im, 5, $ I * 16 + 7, mt_rand (2, 4), $ rndstring [$ I], $ fontColor );
}
}
// Text
For ($ I = 0; $ I <$ rndcodelen; $ I ++ ){
If (function_exists ("imagettftext ")){
Imagettftext ($ im, 16, 5, $ strposs [$ I] [0]-1, $ strposs [$ I] [1]-1, $ fontColor2, $ font_type, $ rndstring [$ I]);
}
}


Header ("Pragma: no-cachern ");
Header ("Cache-Control: no-cachern ");
Header ("Expires: 0rn ");
// Output a specific image format with a priority of gif-> jpg
If (function_exists ("imagegif ")){
Header ("content-type: image/gifrn ");
Imagegif ($ im );
} Else {
Header ("content-type: image/policrn ");
Imagejpeg ($ im );
}
ImageDestroy ($ im );
}
?>

I felt that there was no problem. Later, Baidu found that a high person said that the key was to join ob_clean, which gave me the reason.

Solution

Ob_clean (); // key code to prevent the problem that 'images cannot be displayed due to their own mistakes.


Before adding the header to the output

The Code is as follows: Copy code


Header ('content-Type: image/png ');

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.