Jump to content

[SOLVED] Image resize not returning jpeg image


Chappers

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 2 weeks later...

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/

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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 :P

Link to comment
Share on other sites

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:

 

th_sample.jpg

http://i442.photobucket.com/albums/qq147/chappers27/sample.jpg

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.