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:
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.
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.
After reading your comment.
That question comes up almost every time: 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 additional checks, like store it on disk if a specific size is requested more than x times or delete all stored images that didn't get a request for a timespan of y.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With