views:

62

answers:

2

I'm building a Web application that will eventually contain a lot of images. These images will need to be displayed in different formats across the site. What would be the pros/cons of the two solutions:

  1. Storing various versions of the picture when they are uploaded (e.g. thumb, small, medium, large, verylarge)
  2. Resizing the image through the URL - e.g. /Content/Image/1?height=300

What do you think?

Edit: I had a really hard time accepting one answer over the other, so for anyone reading this q/a, take your time to read both answers because the accepted answer was selected by the flip of a coin :) They're both equally good.

+4  A: 

If i take a look on Wikimedia, they take some kind of combination.

You can define the size through a parameter and in a first step they look if such a file exists and if not create it on the fly and save it for the next request.

Update

After reading your comment ...

That's almost everytime the question: Do i optimize on speed or on size? That's a decision you have to make for yourself and your concrete problem.

Maybe you can implement some additionally checks, like store it on disk if a specific size is requested more then x times or delete all stored images that didn't get a request for a timespan of y.

Oliver
I wouldn't call that a middle-way, I'd call it a combination.. Now all of a sudden we're storing alot of different sizes to disk. This would be quite easy to manipulate by running a script that requests a whole bunch of images in different sizes, hence getting a bunch of images stored to my storage - possibly filling up the quota at my hosting-provider...
Yngve B. Nilsen
+3  A: 

Inevitably anything which provides dynamic sizes based upon a parameter is going to allow an end user to cause a reasonable ('more than usual' perhaps) amount of CPU activity on the server, which might be a problem for you depending on what you are trying to achieve. Caching of generated images, as suggested by Oliver, will help avoid doing the same work twice and should definitely be a part of any dynamic solution.

I think you need to consider how important the dynamic sizing is. In every case I've dealt with predefined sizes (your thumb, small, medium, large, verylarge) have worked fine. The same caching considerations apply, but there far much less potential for a large number of images being created. I've tended to create the various image sizes when the image is uploaded, but a create-on-demand-if-not-already-created solution would work equally well.

Elliveny