$image = ' new0.jpg ';
$img =getimagesize ($image);
Print_r ($IMG);
Switch ($img [2]) {
Case 1:
$dimg =imagecreatefromgif ($image);
Case 2:
$dimg =imagecreatefromjpeg ($image);
Case 3:
$dimg =imagecreatefrompng ($image);
}
Header ("Content-type:image/jpeg");
Imagejpeg ($DIMG);
?>
The above is the source code, if such direct use, will prompt
Warning: Imagecreatefrompng () [function.imagecreatefrompng]: ' new0.jpg ' isn't a valid PNG file in
E:\wamp\www\test\imgtest.phpOn line
One
Warning: imagejpeg () expects parameter 1 to BES resource, boolean given in
E:\wamp\www\test\imgtest
The image type has been tested: Array
(
[0] = 479
[1] = 700
[2] = 2
[3] = = width= "479" height= "700"
[Bits] = 8
[Channels] = 3
[MIME] = Image/jpeg
)
Now the problem is that the switch statement does not seem to run successfully, do not know why the old to Judge Imagecreatefrompng this statement?
And if you do not switch statements, directly run Imagecreatefromjpeg, the output is garbled, which is why AH? Reply to discussion (solution)
Switch ($img [2]) {
Case 1:
$dimg =imagecreatefromgif ($image);
Break
Case 2:
$dimg =imagecreatefromjpeg ($image);
Break
Case 3:
$dimg =imagecreatefrompng ($image);
Break
}
Thanks, I forgot the break.
But the picture output is garbled, if I do not use the header directly to save the picture is not a problem, but can not output pictures
Check if there is a BOM header
Switch ($img [2]) {
Case 1:
$dimg =imagecreatefromgif ($IMG);
Case 2:
$dimg =imagecreatefromjpeg ($IMG);
Case 3:
$dimg =imagecreatefrompng ($IMG);
}
$image is the need to convert the picture, $img is an array, the problem is now initially resolved, that is, Wamp browser problem? The output in the Wamp is garbled, but it's better to switch to 360. What's the reason?
Wamp inside how to output ah, the browser output no problem on it.
Generally is in the declaration type before the output of the problem, for PHP generated code garbled problem often encountered, including sometimes output pictures will also appear garbled phenomenon. There are many reasons for garbled, the most common is because the output picture before the output of other content, resulting in the normal output of the picture. Here are some of the garbled characters I encountered during the development process:
This behavior occurs because of this function:
Header ("Content-type:image/jpeg");
Original: http://www.phpnewer.com/index.php/Cjwt/detail/id/10.html
With Phpscript or CLI, that would be a problem if you run with Phpsever.