Search code examples
phpthumbnailsimage-resizingimagickphpthumb

Is it better to resize Image on demand or on upload?


I'm working on a project that requires using various sizes of user pictures at different sections on the site. The images to be served are thumbnails of the main images in various sizes like 35 x 35 pixels, 50 x 50 pixels and 100 x 100 pixels. I want to allow the user upload only one picture.

I am using PHP and Apache as webserver. The site is estimated to get about 400k visits initially and could go up significantly. My question is:

Should I resize the picture to all the various sizes I need, while keeping the original when uploading? or Should I resize the picture only on demand and display (Using something similar to phpThumb ) ?

Please I would like to know which is better performance or speed wise and resource management wise, considering the level of traffic and there could be as much as 100 concurrent users on the site at a given time.


Solution

  • Disk is cheap and resizing on demand is very expensive. I'd never resize on demand for every request. I think you have two options:

    • Generate all the different sizes once on upload.
    • Store the original, resize on the fly the first time a particular size is requested, and cache that for future requests.

    Update: nginx has a nice module for resizing on the fly. I'd never use it in production by itself, but if you couple it with a reverse proxy, you'll essentially get the second option without having to write any code.