1 (edited by BlakeG 2009-02-20 3:37:19 PM)

Topic: Image is one size when written but almost 100x larger when opened?!

I'm not sure if this is really a PHP question, but it's causing PHP errors, so I thought I'd post in here requesting help.

I have a JPG that's ~1.5MB on disk. It's 300 dpi and roughly 5700 x 4900 pixels when opened in Photoshop. When I try creating a new image from it, I get a fatal error:

Fatal error: Allowed memory size of 94371840 bytes exhausted (tried to allocate 22800 bytes) in *** on line 204

My code is set to reject anything larger than 4 MB, so the aforementioned image "sneaks through" since, again, it's only 1.5 according to the file system.

Does anyone know what would cause PHP to see the file as ~90MB when trying to use imagecreatefromjpg? If there's a way to "reveal" this information to PHP before opening the file, I'd happily reject it, but I'm not sure how to do that...

I'd appreciate any advice or direction anyone can provide! Thanks in advance!!!

Edit: here's the offending line:

    // Use relevant function based on $mime.
    if (stristr($mime, "png")) $imgB=imagecreatefrompng(T_DIR.$fname);
    else $imgB=imagecreatefromjpeg(T_DIR.$fname); // This is line "204" referenced in the error message above.
    
    imagecopyresampled($imgA, $imgB, 0, 0, 0, 0, $iWW, $iHH, $iW, $iH);
    imagepng($imgA, T_DIR.basename($fname));
    imagedestroy($imgA);
    imagedestroy($imgB);

Re: Image is one size when written but almost 100x larger when opened?!

You can check the dimensions of the image before opening it: http://de3.php.net/manual/en/function.getimagesize.php

Re: Image is one size when written but almost 100x larger when opened?!

Thank you, q_no: I'm aware of getimagesize, but I was thinking that dimensions should have been irrelevant; meaning, if a file is, say, 2MB, its dimensions shouldn't matter -- 2MB is 2MB. Obviously, my thinking is incorrect some how, but I'm honestly not sure why. Is there a formula for determining "real" file size (not merely image dimensions) if, in fact, what $_FILES[file]['size'] isn't trustworthy?

Am I as clear as mud? sad

Re: Image is one size when written but almost 100x larger when opened?!

The image files are compressed and when you open an image file to modify it it's converted into raw data. There's no formula to calculate the raw image size - JPGs for instance vary per compression level.

Re: Image is one size when written but almost 100x larger when opened?!

Ah, that makes sense -- especially with JPGs. I ended up ditching the resample idea and just going with resize. And instead of using the GD methods I'm now using ImageMagick. I'm not sure if just using resize rather than resample (imagecopyresized) or if its the difference between IM and GD, but now it's behaving more predictably. Anyway, thanks for putting up with the likes of me! big_smile