Chappers Posted October 4, 2008 Share Posted October 4, 2008 Hi, I'm using this code to resize images: <?php // The file $filename = 'image2.jpg'; // Set a maximum height and width $width = 200; $height = 200; // Content type header('Content-type: image/jpeg'); // Get new dimensions list($width_orig, $height_orig) = getimagesize($filename); $ratio_orig = $width_orig/$height_orig; if ($width/$height > $ratio_orig) { $width = $height*$ratio_orig; } else { $height = $width/$ratio_orig; } // Resample $image_p = imagecreatetruecolor($width, $height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); // Output imagejpeg($image_p, null, 100); ?> Thing is, when I right-click on the displayed image to save it, it's coming up as a bmp image. I want to tailor the code to save the file after resizing, but it needs to be in jpeg format. This code is from the PHP manual, so what's going wrong? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/127032-solved-image-resize-not-returning-jpeg-image/ Share on other sites More sharing options...
zq29 Posted October 4, 2008 Share Posted October 4, 2008 Strange, I just ran your code, right clicked it and it correctly wants to save it as a jpeg... Quote Link to comment https://forums.phpfreaks.com/topic/127032-solved-image-resize-not-returning-jpeg-image/#findComment-657089 Share on other sites More sharing options...
Chappers Posted October 4, 2008 Author Share Posted October 4, 2008 I've tried it and tried it. I don't know much about it all, but could it be how PHP is set up with the host I'm with? Incidentally, if I save it and try to open it with Paint Shop Pro in its image.bmp format, it opens properly. If I rename the image to end with .jpg, PSP won't open it, saying it isn't a jpeg image. Quote Link to comment https://forums.phpfreaks.com/topic/127032-solved-image-resize-not-returning-jpeg-image/#findComment-657119 Share on other sites More sharing options...
Chappers Posted October 14, 2008 Author Share Posted October 14, 2008 I have made a test page so you can see it in action. The index page contains all information required, such as what browser I'm using, etc. There's a link on the page to go to the outputted resized image. As I say on the page, there is someone else on a forum suffering this problem who believes header information is lost or damaged during the resize and hence why this trouble occurs. Why doesn't everyone get the same trouble with these resize scripts then? http://www.demo.freephphostonline.com/ Quote Link to comment https://forums.phpfreaks.com/topic/127032-solved-image-resize-not-returning-jpeg-image/#findComment-664599 Share on other sites More sharing options...
MadTechie Posted October 14, 2008 Share Posted October 14, 2008 Yeah that due to crappy IE i just played with it and come up with a fix // Content type header('Content-type: image/jpeg'); header("Content-Disposition: filename=\"" . $filename . "\";" ); //Add this line (NAME not path) EDIT: more detail Okay, heres the problem ie see the file without a jpeg extension, thus we add the name of the file to the header and IE7 now know what format it is Quote Link to comment https://forums.phpfreaks.com/topic/127032-solved-image-resize-not-returning-jpeg-image/#findComment-665117 Share on other sites More sharing options...
discomatt Posted October 14, 2008 Share Posted October 14, 2008 I wouldn't call failing to provide proper headers an IE issue Quote Link to comment https://forums.phpfreaks.com/topic/127032-solved-image-resize-not-returning-jpeg-image/#findComment-665132 Share on other sites More sharing options...
MadTechie Posted October 14, 2008 Share Posted October 14, 2008 No but considering the mime is supplied and JPEG and IE7 converts the file to BMP..thus ignoring the mime.. every other browser will use the file name from the url as the filename.. and the mime type for the file type it seams to be out of the standards.. yeah i dislike IE Quote Link to comment https://forums.phpfreaks.com/topic/127032-solved-image-resize-not-returning-jpeg-image/#findComment-665140 Share on other sites More sharing options...
PFMaBiSmAd Posted October 14, 2008 Share Posted October 14, 2008 There is a second line in the code in MadTechie's post that he is referring to (only the header() line is showing unless you scroll the code box down.) FF does save the OP's demo image as the .php file name of the file that is outputting the image. Quote Link to comment https://forums.phpfreaks.com/topic/127032-solved-image-resize-not-returning-jpeg-image/#findComment-665189 Share on other sites More sharing options...
Chappers Posted October 14, 2008 Author Share Posted October 14, 2008 Hi, thanks for the replies. I have tried adding what madtechie suggested: header("Content-Disposition: filename=\"" . $filename . "\";" ); exactly as written above, since I'm using the variable $filename. Still no difference. Out of interest, I had already tried adding this: header('Content-Disposition: attachment; filename="newimage.jpg"'); to the file before posting on here, after Googling the problem. That also made no difference. I just always get this: http://i442.photobucket.com/albums/qq147/chappers27/sample.jpg Quote Link to comment https://forums.phpfreaks.com/topic/127032-solved-image-resize-not-returning-jpeg-image/#findComment-665364 Share on other sites More sharing options...
Chappers Posted October 14, 2008 Author Share Posted October 14, 2008 I read somewhere that IE requires an image to be cached before it can correctly recognise the filetype. So I've downloaded a toolbar to show headers and stuff sent when on a webpage. Here's what came up for my test-upload page: HTTP/1.1 200 OK Date: Tue, 14 Oct 2008 20:33:35 GMT Server: Apache Cache-Control: no-store, must-revalidate, max-age=0, proxy-revalidate, no-transform Expires: Tue, 14 Oct 2008 20:33:35 GMT Pragma: no-cache Keep-Alive: timeout=1000, max=99971 Connection: Keep-Alive Transfer-Encoding: chunked Content-Type: image/jpeg No caching is turned on. Could this be the problem, and how do I change that behaviour to test if it is anyway? Thanks a lot to everyone trying to help. Quote Link to comment https://forums.phpfreaks.com/topic/127032-solved-image-resize-not-returning-jpeg-image/#findComment-665427 Share on other sites More sharing options...
MadTechie Posted October 14, 2008 Share Posted October 14, 2008 Hummm, Heres my script, which works fine in IE7 & FF <?php // The file $filename = "company/uploads/girl2.jpg"; // Set a maximum height and width $width = 200; $height = 200; // Content type header('Content-type: image/jpeg'); $file = basename($filename); header("Content-Disposition: filename=\"$file\";" ); $filename = 'company/uploads/girl2.jpg'; // Get new dimensions list($width_orig, $height_orig) = getimagesize($filename); $ratio_orig = $width_orig/$height_orig; if ($width/$height > $ratio_orig) { $width = $height*$ratio_orig; } else { $height = $width/$ratio_orig; } // Resample $image_p = imagecreatetruecolor($width, $height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); // Output imagejpeg($image_p, null, 100); ?> Quote Link to comment https://forums.phpfreaks.com/topic/127032-solved-image-resize-not-returning-jpeg-image/#findComment-665590 Share on other sites More sharing options...
Chappers Posted October 14, 2008 Author Share Posted October 14, 2008 Tried your code but it's still happening in IE7. Must be a local problem then... Quote Link to comment https://forums.phpfreaks.com/topic/127032-solved-image-resize-not-returning-jpeg-image/#findComment-665649 Share on other sites More sharing options...
MadTechie Posted October 15, 2008 Share Posted October 15, 2008 Can someone else please test my code.. to confirm it works or fails.. (out of intrest) Quote Link to comment https://forums.phpfreaks.com/topic/127032-solved-image-resize-not-returning-jpeg-image/#findComment-665686 Share on other sites More sharing options...
Chappers Posted October 18, 2008 Author Share Posted October 18, 2008 Finally fixed this annoying fault! Here's what you need to do: Within Internet Explorer: Tools > Internet Options Go to Advanced tab Go to Reset Internet Explorer settings section Click on Reset... button Tick Delete Personal Settings box Click the Reset button Follow instructions that appear and restart IE. It is ESSENTIAL that you tick the box to delete personal settings, or the problem will remain! The above steps finally cured this problem. Original images displayed simply using img src="" were saving correctly, but any image, in any format, outputting from PHP imagecreatefromjpeg () and imagejpeg() or other formats (imagegif, etc), were saving as untitled.bmp when right-clicked and 'saved as...'. Hope this helps others! And thanks to all those who tried to help me with this. Quote Link to comment https://forums.phpfreaks.com/topic/127032-solved-image-resize-not-returning-jpeg-image/#findComment-668715 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.