Page 1 of 1

Normal for image manipulation script to use more than 20MB?

Posted: Sat Feb 05, 2005 4:46 pm
by voltrader
I have a script that takes an uploaded photo, resizes it to create a thumbnail and also resizes the original photo to fit into max dimensions.

When I upload a photo of about 2MB, I get an oom error. Something to the effect of "...failed to allocate xxx bytes...".

I had ini_set("memory_limit","20M");

When I increased this to 32M, the error disappeared, so I assume that the script used more than 20MB but less than 32MB.

I take the usual precautions, like destroying every image after it's created.

Is it normal for scripts that use the GD library to consume such large amounts of memory?

Posted: Sat Feb 05, 2005 5:05 pm
by feyd
raw images stored in memory can easily take up a lot of space. Considering the compression involved in most images, fully decompressed and allocated a simple 2MB JPEG can balloon to very large sizes.. figure at least 4 bytes for every pixel... plus some added stuff like round up the scan line length to the nearest multiple of 4...

what's the dimensions of this image?

Posted: Sat Feb 05, 2005 5:13 pm
by voltrader
Original image was 2592 x 1944 w/24 bit color depth and just over 2MB in size.

I know that the current server has memory of 512MB. Is there any risk in taking the ini_set("memory_limit","20M"); higher, to say, 128MB?

Posted: Sat Feb 05, 2005 7:20 pm
by feyd
fully decompressed size is 20,155,392 bytes.. minimum.

The only real problem with setting the memory limit that high is taking memory away from other systems. If you set it too high, you may create a swap requirement.. which will dramatically slow the process.

Posted: Sat Feb 05, 2005 9:17 pm
by voltrader
I see... so having memory_limit initally at 20MB was a problem with that large photo.

Thanks.

Posted: Sat Feb 05, 2005 9:34 pm
by feyd
it would seem that, at least, your version of GD is not aware of memory segmenting principles. I am unaware if other graphics tools like ImageMagick support smarter usage of memory, but it may be something to look into..

Posted: Sat Feb 05, 2005 11:45 pm
by voltrader
Thanks for the tip. Will do.

I was sucked-in to moving to a new host by the lure of the Zend Suite + virtual hosting on a segmented portion of a server. Not a true dedicated host, but an "appliance server" where I could put all my various sites.

I had put so much work into these scripts, that I didn't feel "safe" to be in a shared environment any longer.

Well, it turns out that it may have been a mistake, since customer service response times at this new company are measured in days. And I'm not even mentioning their reluctance to upgrading from MySQL 3.23. Yeah, what?!

My next move is going to be migrating to a server at home or office.

Posted: Mon Feb 07, 2005 12:49 am
by Jerryscript1
FYI- ImageMagick is not much more efficient in terms of memory usage than GD. IM makes use of temporary files that are decompressed, as well as the final output file, sometimes taking up 2-3 times the memory.

Your best bet is to restrict the file size of the allowed image uploads.