imagecopyresized - Reducing Memory Usage

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
akreider
Forum Commoner
Posts: 46
Joined: Mon May 22, 2006 3:54 pm
Location: USA

imagecopyresized - Reducing Memory Usage

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Choose the Right Board

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Are you destroying the large images when you're done processing them?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
akreider
Forum Commoner
Posts: 46
Joined: Mon May 22, 2006 3:54 pm
Location: USA

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

this might help see where the memory hogging is taking place.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
akreider
Forum Commoner
Posts: 46
Joined: Mon May 22, 2006 3:54 pm
Location: USA

Post 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.
Post Reply