|imagejpeg() can't save in browser|
| 11:15 am on Dec 21, 2011 (gmt 0)|
I have a PHP script that creates an image and simply outputs the image to the browser (imagejpeg($img)). I want the user to be able to preview the image and then right-click Save Image if they like it (so must be content disposition of inline, not attachment). In Firefox right-click save does NOT save the file to the chosen directory (it does nothing). In IE it wants to save as a PNG, not sure if this is normal. If I right-click below the image in the white space, in FF, and choose Save File it DOES save the image correctly.
| 1:32 pm on Dec 21, 2011 (gmt 0)|
Presumably this is being used as part of an HTML IMG element and your script is referenced in the SRC attribute?
Are you sending the correct mime type "image/jpeg"?
I don't think specifying 'attachment' or 'inline' in this context (as part of the Content-Disposition header) makes any difference, unless perhaps you are sending just this image - not part of an HTML document?
For me, the Content-Disposition header is optional, it works with or without this. However, it is useful in order to specify a filename.
header('Content-Type: image/jpeg');// 1st
header('Content-Disposition: inline; filename=myimage.jpg');
| 11:04 pm on Dec 21, 2011 (gmt 0)|
No, I'm not including the image in HTML. I've got a JS pop-up that loads a page that produces the image. So as far as the content, it is only an image. I have the correct content type specified and have spent hours Googling this issue. I found at least one other person with the same problem but when someone suggested using "attachment" disposition he was OK with that as a result, I'm not. I've been writing php for 7 years and rarely run into an issue that I can't solve. This one has me stumped. It does appear to be MORE of an issue in FF, but even IE doesn't want to save as a JPEG when it's .jpp and content type of "jpeg".
| 1:21 am on Dec 22, 2011 (gmt 0)|
If you are referencing the image directly, as you appear to be doing, then you will indeed need to specify a Content-Disposition of 'inline' (as stated in your initial post). Specifying 'attachment' will result in the browser prompting to download/save the image. Or, omitting the Content-Disposition header altogether should also work (it will/should default to 'inline', but the filename might not be as required).
However, since this is coming from your server, it is possible that your server is automatically setting some additional headers, or perhaps overriding what you have specified. I would have thought this is really the only reason why such a script might work for one person and not another. Check what headers are actually being returned using Live HTTP Headers extension for Firefox.
Make sure there is no erroneous content/white-space being served before or after the image data (before or after the PHP tags) that could be corrupting the image.
Make sure error_reporting(E_ALL) is set and check your error logs. Since you're not outputting text/html you're unlikely to see any reported errors if there are any.
Any specific version of FF?
It should work.
| 8:16 am on Dec 22, 2011 (gmt 0)|
I looked at the live headers earlier today and the only suspicious thing was that the content length was zero, which I already knew. I found code to populate that based on the stream output but that didn't help. I think it might just be a FF issue.
Anyway, I changed the way the code works to load an HTML page that then loads the dynamic image, instead, and then FF allows you to right-click and save the image. I'm not happy that I didn't figure out the issue but I probably would have gone this way regardless since I needed to provide download instructions next to the image.
| 1:17 pm on Dec 27, 2011 (gmt 0)|
If the Content-Length header is present and is zero then that would definitely be a problem - but then it wouldn't show in any browser whether referenced directly or as the src of an img element?! In this case you would need to explicitly set this header by capturing the output and checking it's length (in bytes).
In my test I don't set a Content-Length header (and none is added by the server) and it works OK.
|Anyway, I changed the way the code works to load an HTML page that then loads the dynamic image, ... |
TBH I would have said this was the preferred method, rather than referencing the image directly.
|HTTP/1.1 200 OK |
Date: Tue, 27 Dec 2011 12:26:44 GMT
Server: Apache/2.2.9 (Win32) DAV/2 mod_ssl/2.2.9 OpenSSL/0.9.8h mod_autoindex_color PHP/5.2.6
Content-Disposition: inline; filename=myimage.jpg
Content-Length: 7563 (Optional)
Keep-Alive: timeout=5, max=100