Problem: garbled image output

Source: Internet
Author: User
Tags imagejpeg
Problem: garbled image output Required imageapps'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 this is used directly, a prompt will be prompted.
Warning: Imagecreatefrompng () [function. imagecreatefrompng]: 'new0.jpg 'is not a valid PNG file in E: \ wamp \ www \ test \ imgtest. phpOn line 11
Warning: Imagejpeg () expects parameter 1 to be resource, boolean given in E: \ wamp \ www \ test \ imgtest
Tested Image type: Array
(
[0] = & gt; 479
[1] => 700.
[2] => 2
[3] => width = "479" height = "700"
[Bits] => 8
[Channels] => 3
[Mime] => image/jpeg
)
The problem is that the switch statement does not seem to have been successfully run. Why does it mean to judge imagecreatefrompng?
If you do not want to switch the statement and run imagecreatefromjpeg directly, the output is garbled. why?


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;
}

Thank you. I forgot break.
However, the image output is garbled. if I don't need the header to store the disk directly, the image will be fine, but the image will not be output.

Check whether a bom header exists

Switch ($ img [2]) {
Case 1:
$ Dimg = imagecreatefromgif ($ img );
Case 2:
$ Dimg = imagecreatefromjpeg ($ img );
Case 3:
$ Dimg = imagecreatefrompng ($ img );
}

$ Image is the image to be converted, and $ img is an array. The problem is solved at the beginning, that is, the problem with the wamp browser? The output in the wamp is garbled, but it can be changed to 360. why?

How can I output the wamp in the browser.

Generally, there is an issue of output before the declaration type, which is often encountered when php generates garbled verification codes, including sometimes garbled characters when outputting images. There are many reasons for garbled text. The most common reason is that there are other content output before the image is output, which leads to the normal output of the image. The following are the garbled characters I encountered during the development process:

The reason for this phenomenon lies in the function:

Header ("Content-type: image/jpeg ");
Original article: http://www.phpnewer.com/index.php/Cjwt/detail/id/10.html

Using phpscript or cli has such a problem. if you use phpsever, you can run it.

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.