How to solve the problem of not displaying the verification code image generated by php?-php Tutorial

Source: Internet
Author: User
How to solve the problem of not displaying the verification code image generated by php recently, that is, the verification code can be displayed on someone else's computer, but it cannot be displayed on my own computer. The reason has been found for a long time. haha, I finally found it! Now let's share with you the program: & lt ;? Php & nbsp; $ w80; set the image width and height $ h26; $ strArray (); used to store random code $ string & quot; AB to solve the problem of not displaying the verification code image generated by php

Recently, I encountered a problem where the verification code can be displayed on someone else's computer, but it cannot be displayed on my own computer. The reason has been found for a long time. haha, I finally found it! I would like to share with you the following:

Program:


$ W = 80; // set the image width and height.
$ H = 26;
$ Str = Array (); // used to store random codes
$ String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; // you can select four or more of the four characters at random. Note that the width is adjusted as needed when adding a loop.
For ($ I = 0; $ I <4; $ I ++ ){
$ Str [$ I] = $ string [rand (0, 35)];
$ Vcode. = $ str [$ I];
}
Session_start (); // enable the Superglobal variable session
$ _ SESSION ["vcode"] = $ vcode;
$ Im = imagecreatetruecolor ($ w, $ h );
$ White = imagecolorallocate ($ im, 255,255,255); // set the background color for the first call
$ Black = imagecolorallocate ($ im, 0, 0); // border color
Imagefilledrectangle ($ im, $ w, $ h, $ white); // draw a rectangle to fill
Imagerectangle ($ im, $ W-1, $ H-1, $ black); // draw a rectangular box
// Generate a snowflake background
For ($ I = 1; I I <200; $ I ++ ){
$ X = mt_rand (1, $ W-9 );
$ Y = mt_rand (1, $ H-9 );
$ Color = imagecolorallocate ($ im, mt_rand (200,255), mt_rand (200,255), mt_rand (200,255 ));
Imagechar ($ im, 1, $ x, $ y, "*", $ color );
}
// Write the verification code into the pattern
For ($ I = 0; $ I <count ($ str); $ I ++ ){
$ X = 13 + $ I * ($ w-15)/4;
$ Y = mt_rand (3, $ h/3 );
$ Color = imagecolorallocate ($ im, mt_rand (0,225), mt_rand (0,150), mt_rand (0,225 ));
Imagechar ($ im, 5, $ x, $ y, $ str [$ I], $ color );
}
Header ("Content-type: image/jpeg"); // output in jpeg format. Note that no character can be output above; otherwise, an error occurs.
Imagejpeg ($ im );
Imagedestroy ($ im );
?>
I have verified that I can run on another computer, but my computer cannot run. The reason is as follows:

First, I thought the gd2 library was not opened, but I checked it with phpinfo and found it opened. After the bom is cleared, the code is written at the beginning, so the problem may occur in the code. After research, we found that we still need to change the program. we need to add the ob_clean () statement before the header so that we can run it.

Before the header output:

Ob_clean (); // key code to prevent the problem that 'images cannot be displayed due to their own mistakes.
Header ("Content-type: image/jpeg ");

In this way, the problem is solved.

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.