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! Now let's share with you the program :? Php $ w80; set the image width and height $ h26; $ strArray (); used to store random codes $ stringABCDEFGHIJKLMNOPQR
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! Now let's share with you the program :? Php $ w = 80; // set the image width and height $ h = 26; $ str = Array (); // used to store random codes $ string = ABCDEFGHIJKLMNOPQR
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 result is as follows:
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.
Some people say that I have a problem here. I will show you the correct code and result:
$ 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 );
}
Ob_clean (); // the original program does not have this column
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 );
?>
The result is as follows:
Is the result obvious?