Page 1 of 1

imagecopyresized - Reducing Memory Usage

Posted: Tue Oct 30, 2007 6:56 pm
by akreider
Is there a way to resize images and use less memory?

I want to be able to handle users that upload 5-10 megapixel images (for my open source project). Resize them first (to save cpu), and then resample them. I'm creating two images - one with a maximum width of 800 pixels, and a more thumbnail version with a width of 200 pixels.

MySpace seems to handle large images - for instance, I successfully uploaded a 40 megapixel (4mb) jpg image - and it resized fine (with php this could have used 300 mb memory). Which makes me think there might be a better way of doing this.

My resizing seems to use around 7-8 bytes per pixel - as I'm using imagecreatetruecolor() to create the images. So a 10 megapixel image can use 70-80mb of memory - and several years from now, it's possible that people wll upload 20 megapixel images.

Would only resizing part of the image at a time help? Can I do it, while mantaining a decent quality, without using imagecreatetruecolor? Any other ideas? I'm not that familiar with the GD library or how php image functions work in general.

Choose the Right Board

Posted: Tue Oct 30, 2007 11:57 pm
by s.dot
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:1. Select the correct board for your query. Take some time to read the guidelines in the sticky topic.

Posted: Wed Oct 31, 2007 12:18 am
by Kieran Huggins
there's some interesting user-contributed functions in the manual:

http://ca.php.net/manual/en/function.im ... omjpeg.php

They claim to handle 10 MP images, but don't seem to offer much of a workaround memory-wise. Maybe a more efficient server-side executable could be shell_exec()'d in less memory?

Posted: Wed Oct 31, 2007 12:24 am
by s.dot
Are you destroying the large images when you're done processing them?

Posted: Wed Oct 31, 2007 5:24 pm
by akreider
I'm going to look around to see what is using all the memory. I was creating an output image before I needed to - so moving that reduced peak memory usage by about 10%.

I'm using Apache 2, PHP 5, on XP.

I'm running a client/server - so I have a client that calls webservices (using NuSOAP).

Is it possible that any of that is messing up memory calculations (eg. is php correctly seperating my client and server php usage)?

For a large (5mp) image - the server part of my application starts off using 23 mb (right after the webservice is called, before any image processing). Whereas for a small image (700,000 pixels) it uses 8mb. That makes no sense because the 5mp image file is only 1mb.

Still most of the memory usage is in the image-resizing (about 30 mb out of 53 mb). So that is where the biggest memory savings could be made. It looks like there might not be any way of improving this.

Posted: Wed Oct 31, 2007 5:44 pm
by s.dot
this might help see where the memory hogging is taking place.

Posted: Wed Oct 31, 2007 5:52 pm
by Benjamin
A 5mb jpeg will probably use 50mb of ram or so. That's just the nature of the beast. Jpegs are highly compressed - on disk - when displayed or modified, they must be decompressed. I don't think there is a whole lot you can do.

Posted: Mon Nov 05, 2007 2:57 pm
by akreider
I think NuSOAP is using a good deal of memory. I was able to reduce memory usage by 2 mb by turning off debugging in NuSOAP.

nusoap_base::setGlobalDebugLevel(0);

I estimate it's using around 15 mb of memory to deal with a 2mb upload (a very big incoming variable to the web service).

Otherwise, 60-70% of my memory problem is just how the image resizing/resampling functions work - so there isn't anything to be done.