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

2.8k views Asked by At

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.

5

There are 5 answers

1
Alex Howansky On BEST ANSWER

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.

1
Wayne Whitty On

Resizing once per upload is preferred. Resizing images on-the-fly can be memory and CPU intensive (depending on the number of visits and the size of the images being resized). Not only that, but displaying images directly from disk can be noticeably faster than resizing and then displaying per request. If you look at this answer here, you'll see that he ran a test on a 1.3 megapixel image and that it resulted in a rather noticeable 0.1s:

$ /usr/bin/time --format="%MK mem %Es CPU time" /usr/bin/convert angry_birds_1280x800.jpg -resize 100x100 thumb.jpg
10324K mem 0:00.10s CPU time
0
Ben D On

This totally depends on your application and requirements.

The advantage of scaling on upload is that (a) you use less disk space; and (b) you don't have to worry about resizing/caching the image later.

However, there are lots of cases (the default size you want to use changes, you want to have multiple different sizes, etc etc) when you'll want to keep the original larger version and resize "on demand". BUT resizing is very memory/CPU intensive, so if you go this route you should almost certainly build a cache-system that stores the re-sized versions of the image in a cache file and only processes it if there is no cached version.

2
Laurent S. On

As far as I know security isn't in question here. Regarding performance...it depends what you aim for :

  • generating the images upon request will have an impact on user experience : when he will need it, he will need to wait for it to be resized, then downloaded. But that's a good way to "spread" the resources usage. Note that in that case, you'd want to do it only once, and keep a reference to the generated file so that you can serve it for all following requests.

  • generating the images upon upload will use more resources at the time the user uploads the image, but then you're ok with it

So I wouldn't say one solution is better than the other, but depends on the number of users you expect, the resources you've got, and the user experience you want.

One big advantage of the "resize on upload" solution is that if you're a bit short on resources at some point (in the case for example a lot of users register at the same time), you could just "delay" (=queue) the resize operations to a more quiet period of the day...

In both case though, I would definitely keep the original version so that you can batch resize it in case you change your site...

0
Phil Perry On

Unless you have a huge number of image sizes for a given original image, it's always better to create the various images only once, and serve them up as needed. It's not that much storage on the server, and you'll save a huge amount of server CPU and user wait time.

If you are concerned that a given image at a given size might only be rarely requested, treat your server image repository as a cache. Generate a resized image file only upon demand, and save it in your repository for the next time it's requested. This would also work if you don't want to burn a lot of CPU cycles while uploading a whole bunch of images to the server. If you have a very restrictive host and have to watch your disk usage, you could purge resized image files after some period of no-requests, keeping only the most frequently requested.