Forum Moderators: coopster

Message Too Old, No Replies

PHP Image Generation or Static Files?

Create simple images on the fly, or use static files...

         

Mr_PHP

5:09 pm on Apr 8, 2005 (gmt 0)

10+ Year Member



I've created a PHP script which generates images containing email addresses.
Over 1,000 users can change their email address, color and font styles to create a small customized email image.

Currently, the images are dynamically generated by a simple PHP script (using PHP functions: imagecreate, imagecolorallocate, imagettftext, imagegif). It uses/includes a (200 KB) TTF font file to create the text on every request.

I'm thinking of saving static images, recreated every time users make a change (not so often), so that the many image requests are not processed by a PHP script, but lead to static images (saved as GIF image files on the server).

My question is: does the last option use much less server resources?
Will the PHP script use a lot of memory, if requested every few seconds?

Thanks in advance for your insights and experiences!

Arno

orion_rus

7:58 pm on Apr 8, 2005 (gmt 0)

10+ Year Member



May be you should use imagemagick to generate it? it's rather simple fast and easy to understandable utility
Good luck to you!

Mr_PHP

10:24 am on Apr 9, 2005 (gmt 0)

10+ Year Member



That's what I do, or the GD Image Library, one of those.

Anyway, my questions was: which is faster and uses less resources:
static images
- or -
php script generating images?

And is it a large difference?

KingMacro

12:26 am on Apr 10, 2005 (gmt 0)

10+ Year Member



you could impliment a cache type system rather than have it generate them all at once

Check if the file "cache/<user>.gif" (or whatever) exists 
if it exists,
send it to the client
Otherwise
generate the image
send it to the user
save it to "cache/<user>.gif"

this would give you the advantage of only generating the ones that are required and would spread out the generation to when they are needed rather than doing them all at once, but of course isn't as fast as sending the static files directly but it shouldn't have much of an impact considering your server is currently handling generating the images every time :)

if the users are updating the details then you might also need to compare the date/time of the last change with the date/time of the cached image as well and re-generate when required (or simply have the page that updates their details delete the cached image)

although with that many users it would take a lot of storage space to save all of the images so if they aren't all used all of the time then it might make sense to have a script run every few days to clean up the ones that are rarely used just to save clogging up the disk space - assuming you dont have loads of free space to use up

ergophobe

8:48 pm on Apr 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Go ahead and benchmark it, but in general I think generating images on the fly is fairly costly (depending on image size and so forth). How big are these?

I think the generate and cache solution is probably best - that's what Coppermine does, for example, and the initial generation sucks up CPU time. That's in the case of photos, though. Little CAPTCHA style images like you're talking about would be less of a problem obviously.

Mr_PHP

12:57 am on Apr 13, 2005 (gmt 0)

10+ Year Member



Thanks KingMacro & ergophobe.

I am switching to static files now, it's indeed a better idea, certainly now I'm expecting many more users soon. The images aren't changed very often, but they are loaded frequently.
The script also gives a small delay compared to showing static files.

As for the size of the images: they're very small, containing only an email address, so always below 1 KB (normally 400-500 bytes).

Best regards.

killroy

9:22 am on Apr 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Remember to organise the folder where you keep the images. I wasn't thinking ahead and set up a cache folder. Except that soon it contained 10s of 1000s of files and now is a pain to maintain. Now I set up a second folder level according to the first characters of th ehash (or email address in your case).

SN

Mr_PHP

10:58 am on Apr 13, 2005 (gmt 0)

10+ Year Member



I will just create one or more folders. The files are saved as static files (and replaced when changed).
The file names are like: <ID>-<USERCODE>-<IMG NO.>.gif
(user code is 3 chars to prevent any spam bots from requesting all IDs' images)

So all data are stored in a database, and images can be (re)generated by users through a secure personal settings page.

Is it useful to create more folders, to limit the number of images per folder? I mean is that any faster, if images are already ordered by ID there (naming: see above)?