4

Opening a JPEG image using imagecreatefromjpeg can easily lead to fatal errors, because the memory needed exeeds the memory_limit.

A .jpg file that is less than 100Kb in size can easily exceed 2000x2000 pixels - which will take about 20-25MB of memory when opened. "The same" 2000x2000px image may take up 5MB on the disk using a different compression level.

So I obviously cannot use the filesize to determine if it can be opened safely.

How can I determine if a file will fit in memory before opening it, so I can avoid fatal errors?

Mikk3lRo
  • 3,407
  • 13
  • 38
  • Note: Q&A posted partly to answer this: https://stackoverflow.com/questions/46797422/php-fatal-error-is-any-way-to-not-stop-script which was (correctly) marked as a duplicate - it did raise this interesting question that I couldn't find answered on SO though. – Mikk3lRo Oct 17 '17 at 20:18
  • 1
    Very nice question and answer – Machavity Oct 17 '17 at 22:37

1 Answers1

4

According to several sources the memory needed is up to 5 bytes per pixel depending on a few different factors such as bit-depth. My own tests confirm this to be roughly true.

On top of that there is some overhead that needs to be accounted for.

But by examining the image dimensions - which can easily be done without loading the image - we can roughly estimate the memory needed and compare it with (an estimate of) the memory available like this:

$filename = 'black.jpg';

//Get image dimensions
$info = getimagesize($filename);

//Each pixel needs 5 bytes, and there will obviously be some overhead - In a
//real implementation I'd probably reserve at least 10B/px just in case.
$mem_needed = $info[0] * $info[1] * 6;

//Find out (roughly!) how much is available
// - this can easily be refined, but that's not really the point here
$mem_total = intval(str_replace(array('G', 'M', 'K'), array('000000000', '000000', '000'), ini_get('memory_limit')));

//Find current usage - AFAIK this is _not_ directly related to
//the memory_limit... but it's the best we have!
$mem_available = $mem_total - memory_get_usage();

if ($mem_needed > $mem_available) {
    die('That image is too large!');
}

//Do your thing
$img = imagecreatefromjpeg('black.jpg');

This is only tested superficially, so I'd suggest further testing with a lot of different images and using these functions to check that the calculations are fairly correct in your specific environment:

//Set some low limit to make sure you will run out
ini_set('memory_limit', '10M');

//Use this to check the peak memory at different points during execution
$mem_1 = memory_get_peak_usage(true);
Mikk3lRo
  • 3,407
  • 13
  • 38