tags:

views:

124

answers:

4

My question is about displaying thumbnails and storage.

Let's say I have a website where users can upload photos and view them in albums.

How are the photos usually stored in this scenario? Are the images themselves or are the file paths usually stored in the database?

If the photos are large and you want to display thumbnails, is it better to:

  • save a copy of the image and a reduced size image, only displaying the larger if requested?
  • use HTML to reduce the size?
A: 

Some software such as TikiWiki stores the photos in a database. It then also caches thumbnail sized photos in the database.

Other software stores it in a directory. This is the way Gallery2 operates. I find the directory approach more scaleable. If a different size than the original is requested, typically the app will use ImageMagick to resize the photo, and then store a copy of the resized photo.

Another alternative is to re-upload the photo to a service like S3, and not store the photo locally at all.

brianegge
A: 

This is common question and the basic answer is that it depends. You need to give more information. What database are you planning on using? SQL Server 2008 has some good new features for handling this scenario with FILESTREAM function. Generally I prefer to put them in the database, but if you just stuff them in their without thinking about design and access requirements you could have poor performance as the number of photos increases.

Craig
A: 

IF you are absolutely positively sure that your web server will always have access to the file system hosting the images, then go that route. Maybe.

However, if at any time you think you might need to, i don't know, create an image server because the hard drive on your web server is running out of space OR that you need to run multiple web servers, then save yourself the trouble and store them in a database. The hard part in storing on a file system is the security requirements of crossing the network.

Also, bear in mind that not all database servers are created equal in this regard. SQL 2008 introduced a FILESTREAM data type which actually stores the images on the local file system while allowing all read / write access through the db server. This has the added benefit of allowing you to run virus scanners on the incoming files while in storage.

Oracle has had some nice file storage facilities for awhile now. MySQL? I don't think I'd want to try, but you might be okay.

As to the second question: save a thumbnail along with the image. This process occurs only once per image and saves on presentation bandwidth. Using HTML to size an image down really does nothing for the client.

Chris Lively
+2  A: 

It's almost always a bad idea to store images in a database. BLOBs can really slow down a database something fierce. It also limits your ability to spread storage around different drives. When the files are separate, you can even have one or more separate image servers to reduce the load on the main dynamic server. My recommendations are:

  • In your database table, have columns for both the directory the image resides in and the image name. That way you are free to change where images are stored, round-robin drives, add more storage later and put new images in the new storage, or whatever you want. Storing the path and the filename in separate fields makes it trivial to move images from one directory to another.

  • You definitely want to generate thumbnail images to reduce your network bandwidth and make your application run faster. However, you can generate the thumbnails on demand, or when the system load is low. If you're on Linux, ImageMagick is wonderful at automated batch resizing of images. It can even resize by a percentage instead of an absolute amount.

dj_segfault