Recently encountered a problem, is that the verification code on someone else's computer can be displayed, but my own computer up can't. Reason for a long time, haha, finally found! Now let's share:
Program:
<?php
$w = 80; Set picture width and height
$h = 26;
$str = Array (); Used to store random code
$string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";//random selection of 4 characters, you can also choose more, pay attention to the loop when added, the width of the appropriate adjustment
for ($i = 0; $i < 4; $i + +) {
$STR [$i] = $string [rand (0,35)];
$vcode. = $str [$i];
}
Session_Start (); Enable Hyper-Global variables session
$_session["Vcode"] = $vcode;
$im = Imagecreatetruecolor ($w, $h);
$white = Imagecolorallocate ($im, 255,255,255); First Call set background color
$black = Imagecolorallocate ($im, 0,0,0); Border color
Imagefilledrectangle ($im, 0,0, $w, $h, $white); Draw a rectangular fill
Imagerectangle ($im, 0,0, $w-1, $h-1, $black); Draw a rectangular box
Create Snowflake background
for ($i = 1; $i < $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 a captcha to a pattern
for ($i = 0; $i < count ($str); $i + +) {
$x = + $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 it is not possible to print any characters above, otherwise an error
Imagejpeg ($im);
Imagedestroy ($im);
?>
Verified that it works on someone else's computer, but my own computer is not working. The reasons are as follows:
The first thing to declare is that I thought the GD2 Library did not open, but with phpinfo view, found open. The BOM is cleared and the code is written at the top line, so the problem may appear on the code. Later, after research, we find that we still need to change the program, to precede the header with Ob_clean () This statement, so you can run.
Add before the header output:
Ob_clean (); Key code to prevent the ' image cannot be displayed because of its own error '.
Header ("Content-type:image/jpeg");
The problem has been solved.
How to troubleshoot PHP generated CAPTCHA images do not display problems