Workaround for low memory hosting services

EiNSTeiN_

Joined: 2008-06-22
Posts: 5
Posted: Sun, 2008-06-22 05:22

I am not a Gallery user, but I did try it while I was searching what is the best way to share my pictures. I have a photo album and the hosting service that I use allows 40 MB of memory for PHP scripts to execute (it's 1and1). This is far too less to make a thumbnail out of any decently sized picture, and pretty much any picture over 2 MB won't even load with createimagefromjpeg() with that kind of memory limit. While searching on google for a workaround I noticed that many users of Gallery are facing the same problem (from the many threads on this forum related to this issue and the same hosting service).

The workaround I found after many hours of thinking is to use Perl instead of PHP to generate the image's thumbnails. On 1and1, and I believe on some other hosting services, there is a Perl interpreter available with a truckload of modules including GD and GraphicsMagick and the nice thing is the Perl interpreter doesn't enforce any memory limit. It's a pretty straighforward script to write and it would probably solve the memory limit issue in many cases. I wrote my own script but it's a three-liner, so i'll let someone else write a more elaborate script for Gallery.

I'd have loved to learn that workaround sooner so now I hope this will help someone else.

 
alecmyers

Joined: 2006-08-01
Posts: 4342
Posted: Sun, 2008-06-22 07:31

Why don't you post your three line script please?

 
EiNSTeiN_

Joined: 2008-06-22
Posts: 5
Posted: Sun, 2008-06-22 13:55

First it's not a script for Gallery, it's for my own personal usage, and maybe it's a little more than three lines. But this is about it:

Quote:
#! /usr/bin/perl

print "Content-type: image/jpg\n\n";
use Graphics::Magick;
$image = Graphics::Magick->new;
$image->Read('image.jpg');
$image->Resize(width=>'200',height=>'150');
@blobs = $image->ImageToBlob();
print $blobs[0];

 
alecmyers

Joined: 2006-08-01
Posts: 4342
Posted: Sun, 2008-06-22 18:24

OK I see... I'm slightly confused about what you say about the Perl interpreter not enforcing any memory limits... what makes you think that's the case? I wonder what would happen if you used Gallery to invoke perl to invoke ImageMagick?

 
EiNSTeiN_

Joined: 2008-06-22
Posts: 5
Posted: Sun, 2008-06-22 18:54

well with the perl script at least I can make thumbnails out of 6 MB photos without the "Allowed memory size of .... bytes exhausted (tried to allocate ....)" error like I get with PHP. If there's a memory limit for the perl scripts, then 1and1 don't mention it.

 
alecmyers

Joined: 2006-08-01
Posts: 4342
Posted: Sun, 2008-06-22 19:15

OK, so in what context are you using your script? How do you tie it in to Gallery? I think I'm missing something!

 
EiNSTeiN_

Joined: 2008-06-22
Posts: 5
Posted: Sun, 2008-06-22 19:26

I don't even use Gallery. I'm simply pointing out that if the developers here need a workaround for the memory limit problem that seems to be so annoying to lots of Gallery users, then they should try to implement the thumbnail script in Perl.

 
alecmyers

Joined: 2006-08-01
Posts: 4342
Posted: Sun, 2008-06-22 20:00

Now I'm *really* confused! Are you suggesting that Gallery should be rewritten in Perl!?

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Mon, 2008-06-23 00:45

First: Yes, image processing outside of the PHP process is a good idea. And that's why it's available in Gallery since Gallery's inception. Perl isn't necessary, but it's one of many good alternatives.

Quote:
I have a photo album and the hosting service that I use allows 40 MB of memory for PHP scripts to execute (it's 1and1). This is far too less to make a thumbnail out of any decently sized picture, and pretty much any picture over 2 MB won't even load with createimagefromjpeg() with that kind of memory limit.

40 MB is a lot though, and I can't believe that a php memory limit of 40M is insufficient for GD functions such as createImageFromJpeg() for a 2MB jpeg file.

Anyhow, there are many limits involved here: the memory limit (usually PHP's and often a per process limit), the file size limits (php.ini), CPU time (php's execution time, php's upload time, per user / process), apache 2's request body limit, and some that I forgot right now.

You mention the magic number 2MB, which is PHP's default limit for the size for file uploads. And which is conveniently often a few 100kb too low. But that's not what you're talking about. So let's forget about that issue for now.

You're talking about image processing. The relevant limits here are usually the PHP memory limit and PHP's execution time limit, if you want to do the processing in PHP and not out of process. With PHP's GD functions you're usually fine with a memory limit of around 20M to 30M and an execution time limit of 30 seconds.

If you experience problems, e.g. errors due to insufficient memory, I'd first look into changing the limits and testing whether the limits have actually been changed and taken effect or if the server needs to be restarted or another php.ini needs to be edited.

Now you're suggesting the use of a Perl wrapper. Obviously your PHP setup didn't disallow the exec() function, thus you can start external tools to do the image processing. You suggest Perl. And that may be a good option for you. We do the same, just without Perl. G2's has modules for imagemagick, netpbm, gd, and other toolkits to perform image processing. GD is the only toolkit that is in-process, that runs within PHP. All other toolkits run as a separate process, just like the Perl wrapper you suggest.

--------------
Documentation: Support / Troubleshooting | Installation, Upgrade, Configuration and Usage

 
EiNSTeiN_

Joined: 2008-06-22
Posts: 5
Posted: Mon, 2008-06-23 01:14

Ok, everybody is assuming a lot about what I meant. 2 MB images can be processed just fine. A 5 MB image however can't be loaded with createimagefromjpeg(), at least not on 1and1 with a memory limit of 40 MB (i suppose it depends on the image compression and some other factors, but anyways). Also I didn't suggest a wrapper around PHP, what I did is rewrite, in Perl, a small part of my application that generate a thumbnail and cache it, and I wanted to point it out since lots of people are reporting the issue on this forum and they all seem to be using the same hosting service I use. Maybe the hosting service is the problem but that's another story, it's cheap and the only problem I had with them is this very low memory limit with PHP. The point that external tools might exist is good but it won't help on a shared hosting service where you can't install new modules or tools like on 1and1 (at least not with the package i got).

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Tue, 2008-06-24 00:33
Quote:
The point that external tools might exist is good but it won't help on a shared hosting service where you can't install new modules or tools like on 1and1 (at least not with the package i got).

the point I intended to make is that G2 has already built-in wrappers for external tools, such as imagemagick or netpbm. and that these external tools are usually already preinstalled on shared webhosts. all you need to do is enter the filesystem path to such tools in your g2 admin section.

for 1and1, these external tools might not be installed on the server yet, i don't know. the below thread shows that one can install such tools on 1and1 though.
http://gallery.menalto.com/node/42148

--------------
Documentation: Support / Troubleshooting | Installation, Upgrade, Configuration and Usage

 
alecmyers

Joined: 2006-08-01
Posts: 4342
Posted: Tue, 2008-06-24 13:33

I think the OP was making an observation that Gallery runs out of memory on 1and1 when using ImageMagick (it is installed) to resize his large images, yet needs less memory when run from Perl (or at least doesn't fail with an out of memory condition.) And therefore hoping/expecting/suggesting that somehow it would/should be possible for Gallery somehow to use Perl instead for this part of the operation. Knowing the little that I do about how Gallery works I'd say that was unlikely, and since the OP doesn't use Gallery at all I don't think it's worth pursuing with him...