tags:

views:

187

answers:

4

Dear all, I want to create multiple thumbnails using GD library in php, and I already have a script to do this, the question is what is better for me .. is it better to create thumbnail on the fly? or create a physical file on my server each time I want a thumb?? and Why?

Please, consider time consuming and storage capacity and other disadvantages for both

A: 

Always cache (= write out to disk) the results of GD operations. They are too expensive both regarding processor time and memory to be done on the fly every time. This becomes increasingly true the more visitors/hits you have.

Pekka
+1  A: 

GD is really resource heavy, so you should look at if you can use ImageMagick instead (which also has a clearer syntax).

You definitely will be better off caching the created thumbnail after the first run (regardless of if you run GD or ImageMagick) and serve them from the cache. If you are worried about storage, clear out old files from the cache now and then.

Emil Vikström
Although ImageMagick is resource heavy as well, GD consumes even more memory.
Jacco
+1  A: 

Neither/both - don't generate the thumbnails till you need them - but keep the files you generate.

That way you'll minimise the amount of work needed and have a self-repairing system

C.

symcbean
Thanks Oli, Emil, Pekka and symcbean for your quick reply.your detailed reply open many other issues for me to be considered in the future while managing any uploaded photos.for now I think it is really better to create thumbs when required as symcbean mentioned.
Reda.Web
+3  A: 

When you create the thumbnail depends on a couple of factors (that I'll get into) but you should never discard the output of something like this (unless you'll never use it again) as it's a really expensive operation.

Anyway your two main choices for "when to generate the thumbnail" are:

  1. When it's first requested. This is common and it means that you don't generate thumbnails that are never used but it does mean if you have a page full of first-time-thumbnails that the server might become overwhelmed with PHP processes generating the thumbnails.

    I had a similar issue with Sorl+Django where I was generating 100+ thumbnails per request for the first few requests after uploading and it basically made the entire server hang for 20 minutes. Not good.

  2. Generate all required thumbnails when you upload. Because it takes a long time to upload, you break down the processing quite a lot. You can also pull it out-of-process (ie use another script to process uploads - perhaps not even in PHP).

    The obvious downside is you're using up disk space that you otherwise might not need to use up... But unless you're talking about hundreds of thousands of thumbnails, a small percentage of unused ones probably won't break the bank.

    Of course, if disk space is an issue, there might be an argument for pushing the thumbnail up to a CDN at the same time as you process it.

One note when you save the thumbnails, it's fairly common that you'll want to resize the thumbnails at some point down the line or perhaps want two small variants. I find it really useful to make the filenames very specific so if the original image is image.jpg, the 200x200 version is image-200x200.jpg.

Oli
+1 For most comprehensive answer, even mentioning CDN as an option to offload thumbs.
wimvds