views:

60

answers:

3

I have a script which displays images like this:

header("Content-Type: image/{$ext}"); readfile($image->path);

This has worked fine for weeks and now suddenly it has stopped working. The response header looks fine (Content-Type: image/jpg), I have no ending php-tag and I have made no changes to my code, server- or php-setup which could have caused this to malfunction. Does anyone have a clue as to what may be going wrong?

Thanks!

======================

UPDATE

The image doesn't display although you can download it (file->save as) and save it to computer. Openeing it locally though won't work either which leads me to think that the image has been corrupted somehow. Anyone experienced something similar? I'm thinking maybe som php errors/warnings get injected into the stream and corrupts the image.

A: 

The right content-type for JPG images is "Content-type: image/jpeg".

Note that the T of type is lower case.

UPDATE

I don't know if it will be useful but try something like this:

$info=pathinfo($image->path);
$ext=strtolower($info["extension"]);
if($ext=="jpg") $ext="jpeg";
header("Content-type: image/$ext");
imagejpeg(imagecreatefromjpeg($image->path));
mck89
Actually, I think it's capital T, but in the past it worked with both lowercase c and t, but wither way it's not working
Hank Denijm
Read the updated part of the question and try it
mck89
Wow, have you read my code? $info = pathinfo($image->path);$ext = strtolower($info['extension']);$image->ext == 'jpg' ? $image->ext = 'jpeg' : null;header("Content-Type: {$image->ext}");readfile($image->path);Tried imagejpeg(imagecreatefromjpeg($image->path)) but didn't work.
Hank Denijm
A: 

One source of possible issues is that the MIME type for JPEG images is image/jpeg, not image/jpg. This is a case where the type doesn't agree with the fairly-common, 3-character version of the file extension.

zerocrates
Tried that, but no dice. I have the same problem with image/png, so that doesn't seem to be the problem.
Hank Denijm
A: 

Some thoughts:

  • File is to big
  • File path causing problems
zaf
Thanks, but it's a no on both.
Hank Denijm
Have you tried hardcoding the header and file path and seeing what happens? Try a small jpg file with a simple name like image.jpg
zaf
Did that now, no luck. This has to be server-related, there's nothing wrong with the code, and it has been working fine for 4-5 weeks now. Truly weird.
Hank Denijm
If you inspect the response (view source or output file in text editor) do you see any warnings/errors? Or is it a 0 byte output file?
zaf
Nothing in page source ("view -> page source" menu option is disabled); firebug displays this:<img src="http://huset.dev/images/get/id/5CCCCN" alt="http://huset.dev/images/get/id/5CCCCN">
Hank Denijm
Can you 'Save File As'? Or wget? and then view the content.
zaf
Yes, I can 'File save as' and get it with curl. File contains binary data, but I can't open it with Preview ("It may be damaged or use a file format that Preview doesn’t recognize.")
Hank Denijm
Strange, opening the same (the original that is) image on my local drive works fine, at what point does it get corrupted when serving from apache? The filesize of the downloaded image is the same as the original.
Hank Denijm
The file sizes match??
zaf
Try opening the downloaded file with Notepad (or some other text editor) and see if there's any garbage at the start of the file (like a PHP warning/error). It may be a perfectly fine image, but has been corrupted because PHP added some text before the image data started.
Marc B
The downloaded file is actually 1 byte bigger than the original, so there is obviously something there. Strange thing is I checked out the source on two other machines and there everything worked fine, same revision and all. I'll try to track down the offending byte. Thanks for all help!
Hank Denijm