Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php web host problems 2

Status
Not open for further replies.

Dweezel

Technical User
Feb 12, 2004
428
GB
I'm designing a web gallery for a friend that uses php http uploads to upload jpg's from a digital camera. After the single jpg (about 1MB in size) is uploaded, my script moves it to a directory, then makes two resized copies of it, one thumbnail sized (70px wide) and another for the main image which will display in the gallery once the thumbnail is clicked(about 400px wide).
The problem I am having is that I get the following error:
Code:
Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 2000 bytes) in /home/mysite/public_html/imageup/upload.php on line 152

I've contacted my web hosts technical support via email, and this is the reply I got:
it acutally appears to be a memory limit error, it appears your script is
exhausting the maximum ammount of memory a script can use (16mb currently)

After a few more queries it seems that they are unwilling to do anything to remedy this which I think is suprising. All I'm trying to do is set up a script to upload images one at a time and resize them. I would have thought this was a fairly everyday task for the average server running PHP.
I'm guessing that the 16MB is refering to the amount of RAM that is being used in the moving and resizing calculations.
Does any one have any experience or advice they'd like to pass on. I'm considering changing hosts, but I'm wondering if their script memory limits will be any higher.



 
There is actually no setting (afaik) in PHP that controls the memory limit - it must be set in the web server. We use Apache and have the default memory limit per chld process.
The memory limit is set fairly conservative to keep the servers responding quickly, without having to page to disk if the physical memory is exhausted. I assume that most hosts will also follow this thinking to keep the shared environment operate at acceptable speeds.

However, I can't think why the script would require >16MB to resize a 1MB original. Maybe you can make the script modular and perform one action at a time, so the memory will not be maxed out.
 
I've had this problem while dealing with large images (2800x3400+) using the php gd libraries. There are a few options you have.

One is to optimize your script to pass the images around by reference. I'm guessing that you're passing the image around without using references which results in duplicates of the image and fills up the 16mb of memory set in your php.ini file. This is the best and most time-consuming option.

Two is to use the ini_set function. You can override php's ini settings to change just about any variable you want. So you can call ini_set ("memory_limit", "32M") to set the script to run in 32mb of memory. This is the second best thing because it doesn't increase the memory limit for all your other php scripts. This can be a little annoying at times because I've heard of it not working for some versions.

Three is to edit your php.ini file and change the memory limit to a high value. This is much like the second option, but this changes it globally across all php scripts. This option also is less secure because it allows the scripts to use more memory and so if you have lots of scripts using more memory you end up filling system memory faster and it slows the system down if it gets to be too much. It also potentially allows hackers more memory to mess around with in more locations than not.

Four is to use a commandline tool to edit your images. This way you can call commandline tools to resize your images, and they will use any memory they need in order to complete the task, and php doesn't reach its memory limits. I've used this option and had ImageMagick do various tasks on my images and I find it to be much faster than the GD libraries. This option can be a bit more annoying and more work than any of the other options.

I would recommend starting with the first option and if you still hit memory limits, try the second option, and if that still doesn't help, try the fourth option.

Steve Kiehl
Web Page Designer - Nanovox Productions
Fantasy Artist - Zeadi
 
Thanks for replying guys. I've just tried using ini_set Kiehlster.It worked the first time I used it and I thought that was problem solved. But every time I've tried it since then I'm getting the same old memory limit warnings.

I'm wondering what you mean by 'pass the images around by reference'? At the moment I'm using move_uploaded_file($uploaded_file_name,$full_filepath_of_new_location). I'm then using ImageJpeg ImageCopyResized and ImageCreateTrueColor referencing the full file path that the uploaded file has been moved to.

Is this what you mean by passing around by reference? Or is it another method entirely?

Also what is the output of ImageMagick like? I'm not very impressed with the compression of images using the gd library. The re-sized jpegs look choopy and pixelated, and the file sizes are too large. Similar images compressed with a stand alone graphics program would be higher quality and with a smaller file size. Does ImageMagick solve any of these problems?

Thanks.
 
There's a good document on passing by reference on php.net. You'll find it here:
Passing by reference helps save on memory because you're essentially editing the original variable rather than making a copy of it and editing the copy.

I found that ImageMagick's scaling is slightly more crisp and more readable than gd library, but they are not too much different. I do believe I get more detail out of ImageMagick when compressing images. ImageMagick however doesn't give as much functionality that GD offers as far as creating images and adding in true-type text and such. I find it mostly a good horse for resizing giant images down to a more web-friendly sizes.

The major problem with the GD library is that it requires that you read in the entire image into a fully uncompressed image before it can manipulate it. And this is not as good for a web server. ImageMagick will do many operations in a serial form and does not require you to read in the entire file at once before scaling it. It basically passes the image through a pipe and it comes out smaller. It may take a little longer than GD, but it can handle images that are larger than the amount of ram in your server. I had 256mb of ram in my server at one point and was messing around with images that were effectively 400-500mb uncompressed and GD would die and fill up php's memory space, but ImageMagick would resize them without filling up my server's memory much at all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top