JPEG - Resize Image to filesize?

Soldato
Joined
18 Oct 2002
Posts
16,008
Location
The land of milk & beans
'Noon all.

I'm currently creating an image gallery system where someone uploads a high-res image and they system automatically makes a mid and low res version of it.

The problem I have is that the each res (high-mid-low) is set by the user. So mid res images could be 5Mb for one user, and 500Kb for another.

Is there any algorithm to determine the filesize from the resolution of the image.

E.g. If i wanted a 750k image, I'd resize to X by Y px?

btw. The images to be dealt with are JPG, GIF, PNG and TIFF

TIA

*edit - i meant to put ASP, not JPEG in the thread title :doh:*
 
Last edited:
It's certainly possible if you are dealing with uncompressed image file formats, e.g a 24bpptif can be worked out at 3bytes per pixel, + I think about 540 byte header (depending on Tif Spec/program), but with things like GIF,PNG,JPG the compression ratio will vary with image contents, as well as image size.

The only way is to write a script that will save the image at a compression level, check the filesize, if too big, save again with more compression, if too small save again with less compression.

I had to do this for something (resize 22+KB jpegs down to no more than 17KB, I wrote a small batch script to iterate over the images and running image magick on each one until it's file size hit 17408 bytes) Fortunately these are small files and it took no time.
 
thanks for the reply whitecrook. I though compression would be a factor in the calculations, but couldn't think of a bullet proof way of taking it into account.

I think because of the timescales involved with this project, the high/mid/low res's will have to be set on an image width/height basis rather than filesize which will make the resizing process far quicker and simpler.
 
Back
Top Bottom