tags:

views:

1223

answers:

3

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?

A: 

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).

ed209
+1  A: 

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();
Mario
I ran your class/code with the same memory usage results. A 85Mb memory usage for a 1.8Mb jpg. I think it depends on the type of jpg uploaded. After searching forums, this does not seem uncommon.
ed209
Ah, good to know. You mind uploading the evil jpg somewhere? Am curious on this behavior.
Mario
email me here and I'll reply with the jpg in question - interested to see what you find [email protected] (expires in 1hr)
ed209
+1  A: 

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.

meouw