...

View Full Version : IE JPEG Save As problem with GD



Ultragames
01-21-2008, 11:45 PM
I am dynamicly creating a JPEG image with the GD library. The image is being called with an image tag with a souce of 'image.jpg' Htaccess is redircting that to image.php. That php page creates the image and prints it out, using these headers:


header('Content-Disposition: attachment; filename="My_Swatch.jpg"');
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Content-Type: image/jpeg");

In Firefox, if you right click the image and choose Save Image as... it defaults to a JPEG image named My_Swatch.jpg. However in IE if you do the same thing, it will default to untitled.bmp, and won't even let you try to save it as a JPEG file.

I know this is in the header information and not the file name I am choosing to put in the source, or IE would use image.jpg as the file name, and it dosn't.

Does anyone know what I need to do header-wise to make IE obey the name and file type that I send it?

Thank you.

Note: The image displays correctly in all browsers, I am simply trying to get Right Click -> Save As working. I always could make a Click To Download link, but I am worried that my users will still try to save it, or even copy and paste it into another application.

Ultragames
01-22-2008, 08:57 PM
[bump]

I really need to get this working. I am worried that my users will try to copy and paste the image I am creating into and email, and it will not work, because IE can't recognise the file type, regarless of the header information and the URL i am using, (both saying jpeg.)

If anyone needs more information to help me out, then by all means let me know. But I could really use the help on this one.

mlseim
01-22-2008, 09:26 PM
I just read something about your problem (what a coincidence).

I think you need to utilize this:
http://fr.php.net/session_cache_limiter

That fixes the problem with IE not recognizing the Content-Type.
Let me know if that helps you out.

This is a quote for .gif ... but same as .jpg?



Using <IMG SRC="image.php"> to dynamically generate images is a bit problematic regarding cache. Unless caching is activated, IE seems to get confused about the type of the image when attempting to save it. A .GIF created in the above way causes the browser to suggest saving the image with .BMP, not .GIF.

A solution is to activate cache with session_cache_limiter('public'); in "image.php", after which IE will correctly save as .GIF. If you do not want the cache to block any changes in the dynamic image, make sure that the SRC keeps changing with every reload. Something like "image.php/" . mt_rand(1,100000) . ".gif" seems to work well.

Might be trivial to some, but I spent a couple of hours figuring out why IE always wants to save my dynamic .GIF's as .BMP's.

Ultragames
01-22-2008, 09:34 PM
Exactly what i was looking for!

Thanks!

Ultragames
01-22-2008, 11:04 PM
Well that didn't seem to have an effect... I made sure to set teh cache limiter before starting the session, but it did not effect IEs handeling of the filename.

Is there a change to my header information that I should make in addition the changing the cache limiter?

Is there a content disposition besides attachment that I can use and still specify the file name?

Is my cache control header conflicting with the cache limiter?

Thanks!

mlseim
01-23-2008, 04:16 AM
Can you post the whole PHP script?

Maybe I can try it on my server with a .jpg image and see what happens.

Ultragames
01-23-2008, 10:18 AM
It refferences a lot of classes, and database information and GET variables and would very difficult for me to have someone else fully emulate. However I am fairly certain that you should be able to help with the following information:

The image is being called using "image.jpg" but htaccess is redirecting the call to "image.php"

And the following sumarized code:


if( $cache_limiter ) session_cache_limiter( (string)$cache_limiter );
session_start();

list($width, $height) = getimagesize('__FILE_GOES_HERE__');

## Convert the Image to PNG-24
$im_tc = imagecreatetruecolor($width,$height);
imagecopy($im_tc,$im,0,0,0,0,$width,$height);
imagedestroy($im);
$im = $im_tc;

$base_color = array(239,45,193);
$base = imagecreatetruecolor($width,$height);
imagecopy($base,$im,0,0,0,0,$width,$height);
imagelayereffect($base, IMG_EFFECT_OVERLAY);
imagefilledrectangle($base, 0, 0, $width, $height, imagecolorallocatealpha($base, $base_color[0], $base_color[1], $base_color[2], 0));
imagecopyresized($im, $base, 0, 0, 0, 0, $width, $height, $width, $height);
imagedestroy($base);

header('Content-Disposition: attachment; filename="My_Swatch.jpg"');
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Content-Type: image/jpeg");
imagejpeg($im);
exit;

Ultragames
01-23-2008, 10:19 AM
Also note: The images I am openeing are PNG images, but I am printing a JPEG image at the end of the script. From my understanding of the way GD works, that dosn't matter. But I may be wrong.

mlseim
01-23-2008, 03:46 PM
I'm beginning to see a lot of things like this:
http://msmvps.com/blogs/spywaresucks/archive/2005/08/21/63560.aspx

As though this is a known IE problem and there's nothing you can do with scripting
to fix this. Other blogs indicate the same thing ... an IE brower problem.

I tested on my IE and I can right-click and save .png just fine, but not .jpg.

Can you display as .png instead of .jpg?

Ultragames
01-23-2008, 10:19 PM
Windows computers still do not have a good way of showing PNG images be default. Users usualy use IE which trys to launch Quicktime or the like, or they use Photoshop, but for the average user with a home install of XP, saving PNG images, or worse... trying to copy and paste them into an email, is just not a good idea.

If you have MSDN evidence to prove me wrong by all means, I would enjoy learning otherwise.

Thanks for the great links. For others who find this post through the search feature, check these links out:

http://support.microsoft.com/default.aspx?scid=kb;en-us;810978
http://support.microsoft.com/kb/q260650/



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum