Home » Php » php – Should I generate each thumbnail dynamically every time it is requested, or store them on image upload?

php – Should I generate each thumbnail dynamically every time it is requested, or store them on image upload?

Posted by: admin July 12, 2020 Leave a comment

Questions:

Problem – I wanted to set up an image-uploading feature in my website. But I wanted to show both- the original image and a small thumbnail of the image.

Choices – Which way is better – to create a separate image (thumbnail) in a directory when the image is uploaded or to show a smaller version by reducing its height and width in the fixed ratio every time the image is requested?

How I am doing it currently – The later one sounds better to me because it won’t be taking much size on the disk but it has to resize the image again and again. Which one do you think is better?

This is a general question for web application, no language in specific.

Any idea how facebook or google do it?

Question – My question is how to generate thumbnails and show them on a website – by creating a copy of the original image with smaller dimension or by generating the thumbnail dynamically every time it is requested.

How to&Answers:

Creating the thumbnail on upload is almost always the better option. If storage is a concern you could convert them on the request and then cache the result in a memory store. If its requested again before the cache expires, no conversion will be needed.

Storage is often pretty cheap, so I would probably not go for that extra complexity.

Answer:

Just create a thumbnail version and save to disk. Hard disk space is very cheap. £70 for a couple of TB.

Answer:

Creating a thumbnail is a better option and it doesn’t cost much disk space. Your client will also load smaller size when opening your pages. converting the image upon request will cost event more time to load your page 😉

Answer:

If you take a look at most CMS with this built in functionality they nearly always create a thumbnail image of the image on upload and store it on the server.

This goes back to the age old saying of “do what google does” but with CMS.

Answer:

“Better” depends on the criteria you set.

For most applications, disk space is not an issue – and if storing a thumbnail is a problem, storing the original must be a huge concern – a decent digital camera photo will run to many megabytes, whereas the thumbnail should not exceed 50K.

Bandwidth and performance (as perceived by the client) are usually bigger concerns. If you have lots of people browsing a gallery of image thumbnails, serving 50Kb thumbnails will be significantly faster (and cheaper in bandwidth) than serving multi-megabyte high resolution images.

In addition, by serving thumbnails on a URL like <img src="images/thumbnail/foobar.jpg"> and setting appropriate cache headers, you should get a lot of downstream caching – this is less likely if you serve the image as <img src="thumbnail.php?image=image/foobar.jpg> because caches treat querystrings rather conservatively.

I used to work on a web site that managed hundreds of thousands of product images; we set up ImageMagick to create thumbnails automatically. Depending on your setup, it may make sense to do this when the thumbnail is first requested, rather than when the file is uploaded, because the conversion can be fairly resource hungry, and doing it at upload time would take longer than we wanted to wait. Modern hardware may make that a non-issue.

There’s also a question about keeping the thumbnails in sync with the originals – if the user uploads a new image, you have to ensure you get the thumbnail updated; if the original is deleted, you must also delete the thumbnail.