PHP verification code image cannot be output
File name global_func.php
Function _code(){
//Run verification code
//Number of random codes
$_rnd_code=4;
for($i=0;$i<$_rnd_code;$i++){
$_nmsg.=dechex(mt_rand(0, 15));
}
//Save in session
$_SESSION['code']=$_nmsg;
//Length and height
$_width=75;
$_height=25;
//Create a picture
$_img=imagecreatetruecolor($_width,$_height);
//White
$_white=imagecolorallocate($_img, 255, 255, 255);
//Filling
Imagefill($_img, 0, 0, $_white);
$_flag=false;
if($_flag){
// Black, border
$_black=imagecolorallocate($_img, 0,0,0);
Imagerectangle($_img, 0,0, $_width-1,$_height-1,$_black);
}
//Draw 6 lines
for($i=0;$i<6;$i++){
$_rnd_color=imagecolorallocate($_img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
Imageline($_img,mt_rand(0,$_width),mt_rand(0,$_height),mt_rand(0,$_width),mt_rand(0,$_height), $_rnd_color);
}
//Snowflakes follow
for($i;$i<100;$i++){
$_rnd_color=imagecolorallocate($_img,mt_rand(200,255), mt_rand(200,255), mt_rand(200,255));
Imagestring($_img,1, mt_rand(1, $_width),mt_rand(1,$_height), '*', $_rnd_color);
}
//Output verification code
for($i=0;$i
$_rnd_color=imagecolorallocate($_img, mt_rand(0, 100), mt_rand(0, 150), mt_rand(0, 200));
Imagestring($_img,5,$i*$_width/$_rnd_code+mt_rand(1,10), mt_rand(1,$_height/2),$_SESSION['code'][$i],$_rnd_color );
}
//Output image
header('Content-Type:image/png');
imagepng($_img);
//Destroy
imagedestroy($_img);
}
If I call function _code(); in the global_func.php file, an error will appear and a red cross will appear.
code.php
_code();
I can use this by writing it directly in code.php.
code.php
//Number of random codes
$_rnd_code=4;
for($i=0;$i<$_rnd_code;$i++){
$_nmsg.=dechex(mt_rand(0, 15));
}
//Save in session
$_SESSION['code']=$_nmsg;
//Length and height
$_width=75;
$_height=25;
//Create a picture
$_img=imagecreatetruecolor($_width,$_height);
//White
$_white=imagecolorallocate($_img, 255, 255, 255);
//Filling
Imagefill($_img, 0, 0, $_white);
$_flag=false;
if($_flag){
// Black, border
$_black=imagecolorallocate($_img, 0,0,0);
Imagerectangle($_img, 0,0, $_width-1,$_height-1,$_black);
}
//Draw 6 lines
for($i=0;$i<6;$i++){
$_rnd_color=imagecolorallocate($_img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
Imageline($_img,mt_rand(0,$_width),mt_rand(0,$_height),mt_rand(0,$_width),mt_rand(0,$_height), $_rnd_color);
}
//Snowflakes follow
for($i;$i<100;$i++){
$_rnd_color=imagecolorallocate($_img,mt_rand(200,255), mt_rand(200,255), mt_rand(200,255));
Imagestring($_img,1, mt_rand(1, $_width),mt_rand(1,$_height), '*', $_rnd_color);
}
//Output verification code
for($i=0;$i
$_rnd_color=imagecolorallocate($_img, mt_rand(0, 100), mt_rand(0, 150), mt_rand(0, 200));
Imagestring($_img,5,$i*$_width/$_rnd_code+mt_rand(1,10), mt_rand(1,$_height/2),$_SESSION['code'][$i],$_rnd_color );
}
//Output image
header('Content-Type:image/png');
imagepng($_img);
//Destroy
imagedestroy($_img);
I run this verification code directly without encapsulating it into a function. I can use the verification code and everything is fine.
But after I use function _code(), a red cross is displayed in the html after calling _code(). I don’t know why
------Solution--------------------
Could it be the reason for header('Content-Type:image/png');?
The BOM header will affect the file type expected by the browser
------Solution--------------------
Even if it is written as a function, there is no problem in calling it.
Function _code(){
//Run verification code
}
?>
Another page
include("./test/test.php");
_code();
------Solution--------------------
If you open the php error message, you will know why..