Author Topic: Album thumbnailing idea  (Read 2148 times)

Mike

  • Jackass In Charge
  • Posts: 11248
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Album thumbnailing idea
« on: May 31, 2009, 01:28:00 PM »
So I got a VPS setup and am intending to use it to host my albums.

Right now I'm trying to figure out how to handle multiple sizes of the same image.

A couple of design considerations:
Want to limit the amount of of thumbnails so they don't eat up disk space.
Don't want to use width and height attributes to fake thumbnails (eats up bandwidth and makes the user wait longer).
Easy to do.

So given the first part I think it should be a "on the fly" thing.  However, general caching rules apply in that if it is requested once it is liable to be requested again in the near future.

What I came up with (with some ideas stolen from Google :D) is this:
When an image is added to the system it goes into a separate and unique directory (added benefit of avoiding name clashes).  Then to get a thumbnail a request is made to: dir/sXYZ/name where XYZ is the size.  The size is a single number for the long dimension, that way I can preserve the ratio.

If the thumbnail doesn't exist it'll trigger the custom 404 handler which then figures out if the image they are requesting a thumbnail from is valid, if the thumbnail is smaller then the original, and if so resizes the images and writes the thumbnail.  It'll also create the subdirectory if required.  It then sends back a 302 with the Location set for the thumbnail along with a noretry query parameter so that it doesn't get stuck in a loop of 404s.

The reason for the subdirectories for each size is so that the filename remains the same.

I haven't figured out how I'm gonna go about cleaning up the thumbnails yet.  It might just end up being a nightly cron that just kills them all.

Anyone have any comments or other ideas?

Steve

  • This 49%er supports Romney
  • Just a Jackass
  • *
  • Posts: 16120
  • Karma: +31/-410
  • Mr. Mom
Re: Album thumbnailing idea
« Reply #1 on: May 31, 2009, 06:04:36 PM »
Maybe this is a stupid question, but it seems you are making it more complicated then it has to be. Are you allowing users to input a desired size, or choose from a list of sizes you provide?

Assuming the latter, why not just store one size of each image on the server, and use php to resize it on the fly when the user chooses? PHP resizes things fairly quickly, and you really dont even need to cache it because isnt that the same result (increased storage) as if you just stored all the sizes for each image to begin with?
hey ethic if you and i were both courting lily allen..... oh wait, which one of us has a relationship that lasted more than the bus ride home?

Mike

  • Jackass In Charge
  • Posts: 11248
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: Album thumbnailing idea
« Reply #2 on: May 31, 2009, 06:21:36 PM »
Not allowing the user to choose the size but it does give me some flexibility (i.e. deciding I want an image in a particular size is done automatically).  Thumbnail size is going to be controlled by some mechanism I haven't figured out yet :D

I thought about doing it dynamically but assuming peak hits (i.e. posting a new album on forums) apache serving up existing files is going to have better performance then php serving it up each time.

The caching issue is going to be the balancing act.  Ideally, only the images that are being looked at will have the thumbnails while the others won't.  So I do save some storage vs creating the thumbnail all in one go.  Plus if I change the sizing I'd have to create the new thumbnails for all the existing images.

The thought process is the only complex part of this.  The code itself was pretty simple.  I added a <Directory> section to my vhost for the parent directory of all the images.  It has the ErrorDocument directive.  If the file exists it serves it up, if not the 404 handler creates it and then redirects them back to the image.  If something along the way fails then it just throws up the 404 and stops.

Steve

  • This 49%er supports Romney
  • Just a Jackass
  • *
  • Posts: 16120
  • Karma: +31/-410
  • Mr. Mom
Re: Album thumbnailing idea
« Reply #3 on: May 31, 2009, 07:46:27 PM »
Ideally, only the images that are being looked at will have the thumbnails while the others won't. 

Now i'm with you. Makes sense.
hey ethic if you and i were both courting lily allen..... oh wait, which one of us has a relationship that lasted more than the bus ride home?

Mike

  • Jackass In Charge
  • Posts: 11248
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: Album thumbnailing idea
« Reply #4 on: May 31, 2009, 09:00:48 PM »
Did find a gotcha while working on this.  Resizing 2mb files eats up memory.  Brought the server to its knees trying to load up a bunch at once.

I ended up using a semaphore to limit one process to section that actually loads the image and resizes it.  Even two processes was too much.

It does cause a bit of delay so I'm gonna have to consider how to make it a bit smoother.

Perspective

  • badfish
  • Jackass In Charge
  • Posts: 4635
  • Karma: +64/-22
    • http://jeff.bagu.org
Re: Album thumbnailing idea
« Reply #5 on: June 01, 2009, 06:24:10 AM »
Why bother with all of the 404 passing around? Why not just explicitly check if the file exists in the script, then create it if it doesn't all within the same request?

Mike

  • Jackass In Charge
  • Posts: 11248
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: Album thumbnailing idea
« Reply #6 on: June 01, 2009, 09:07:59 AM »
Seperation of tasks.  Additionally, it allows me to link the image on a forum and not worry about it being deleted.