Page is a not externally linkable
- Code, Content, and Presentation
-- Site Graphics and Multimedia Design
---- create a JPG with precise filesize. how?


httpwebwitch - 2:51 pm on Jun 19, 2009 (gmt 0)


now things get trickier.

you're right rocknbil, I'm using the GD library in PHP5.

The file upload has its limit of 3Mb. I've got that working OK now, and I managed to create a few images that were close to, above and below the limit. good.

But then...
When I get the image into memory using imagecreatefromjpeg() [ca3.php.net], the image is stored in memory on the server, uncompressed. So what was a 3Mb upload actually becomes a 40-million-something byte binary object or something. In memory.

This leads to memory allocation issues. The default 8Mb allocated by PHP isn't enough for even a typical snapshot from a contemporary point-n-shoot digital camera. To handle one from my little Sony Cybershot, I had to nudge the memory limit up to 40Mb:

<?php
ini_set('memory_limit', '40M');
?>

Giving the user >40Mb of memory space to upload their super-high-rez photos is overly benevolent, IMHO.

Overflowing the memory limit causes a Fatal Error. I've got some error handling in this script, but those Fatal Ones... they have a tendency to blow up and quit page execution before I can output a friendly error message. Three options are: 1) increase that memory limit to something outrageous, 2) capture Fatal Errors and handle them after the script has aborted, or 3) detect or predict the uncompressed size of an image in advance and throw a preemptive error back at the user.

I prefer #3.

Is there any way to predict the uncompressed size of a JPG without actually uncompressing it?


Thread source:: http://www.webmasterworld.com/graphics_multimedia/3935347.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com