Help: Picture output garbled problem

Source: Internet
Author: User
Tags imagejpeg
$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.

  • 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.