ed209
ed209

Reputation: 11303

reasonable PHP memory_limit for image resize

I want to enable users on my site to upload images to their accounts. The images get resized into 4 different sizes required across the site.

I have been using Pear Image_Transform but I kept getting "bytes exhausted" fatal errors on certain types of jpgs (all files tried under 2mb). So I moved to a dedicated server with Pentium Dual-Core E5200 @ 2.50GHz and 2GB ram. Uploaded the same image resize code - same error. I upped the RAM in php.ini to 64M but site get the same problem on certain types of jpg. Also tried wideimage class - same error (error is always with imagecreatefromjpeg()). (Using GD2). All works fine locally on my mac.

Is this really a memory issue, what's a reasonable memory_limit for my set up + image resizing?

Upvotes: 0

Views: 7694

Answers (6)

lubosdz
lubosdz

Reputation: 4520

There is no safe way of setting sufficient (or safe, not crashing) memory limit for PHP for image resizing, if the image size may be of arbitrary size.

The best solution would be resizing via command line PHP CLI via installed ImageMagic - this way no PHP memory is used, instead OS memory will be used.

See CLI convert command:

http://www.imagemagick.org/script/convert.php

Upvotes: 2

Max
Max

Reputation: 1195

http://www.dotsamazing.com/en/labs/phpmemorylimit

Here is a nice tool for calculating the memory you will need. It worked perfectly even for my terribly inefficient coding..

It also has some tips about how allocate more memory.

Upvotes: 0

Steve-o
Steve-o

Reputation: 12866

The Image_Transform library misses an important resizing optimization available with ImageMagick and libjpeg. You can resize a JPEG efficiently whilst loading by specifying the size before reading, example in this answer:

Efficient JPEG Image Resizing in PHP

Upvotes: 0

meouw
meouw

Reputation: 42140

A rough guide to how much memory you're going to need can be calculated like this

$imageInfo = getimagesize( $sourceImagePath );

// a check to make sure we have enough memory to hold this image
$requiredMemoryMB = ( $imageInfo[0] * $imageInfo[1] * ($imageInfo['bits'] / 8) * $imageInfo['channels'] * 2.5 ) / 1024;

This is quite rough and includes a fudge factor, 2.5, which you may want to experiment with.

Upvotes: 3

Mario
Mario

Reputation: 1525

90Mb of memory for a 1-2 Mb jpeg seems odd. I haven't seen your code, but perhaps you're opening the file multiple times for each size instance? Try opening the file once, (resize it, save it) X 4, then close it.

I currently have a site where users upload images, and not running into an error, my settings are 16Mb for PHP scripts and 2Mb file upload limit and I use this library instead of pear

code example

$image
->open('uploaded.jpg')
->resize(500, 500)
->save('large.jpg')
->resize(100, 100)
->save('medium.jpg')
->resize(25, 25)
->save('small.jpg')
->close();

Upvotes: 1

ed209
ed209

Reputation: 11303

It seems that ini_set("memory_limit","256M"); has covered it. Also, using memory_get_peak_usage() I discovered that my image resize (with problem jpgs) were using in the region of 90mb for a 1.8mb image.

Also of interest is http://uk.php.net/manual/en/function.imagecreatefromjpeg.php#64155 for a way to dynamically assign a memory limit (which I have yet to try).

Upvotes: 1

Related Questions