views:

629

answers:

4

I would like to dynamically create thumbnails based on parameters in the URL. For example, http://mysite.com/images/1234/120x45.jpg would create a 120x45 thumbnail for image id 1234.

The obvious solution to this is to have a django view which does the following:

  1. Look for a previously cached version of the image at this size.
  2. Create a thumbnail if it's not cached (some logic for locking so that only 1 process creates the thumbnail and other processes wait).
  3. Pipe the results through django.

That should "work", but I'm concerned about performance. I don't like the idea of using django to serve static content. What are some other ways to accomplish this problem?

+1  A: 

Check out django imagekit. http://bitbucket.org/jdriscoll/django-imagekit/wiki/Home

ablerman
+2  A: 

You can also try sorl, it is being used by Satchmo.

jpartogi
+3  A: 

You don't have to use Django to serve the static content directly. Simply have your server route 404 requests for your images folder to a Django view, where it splits apart the filename and generates the appropriate thumbnail, before redirecting back to the original URL (which hopefully will no longer be a 404).

As for the other answer's django-imagekit suggestion, I'm not sure it does anything to let you dynamically generate image thumbs based on URL, but I certainly do recommend using it for all the features it does have.

Edit:

As for the actual URL structure, I feel a more typical /images/filename-120x45.jpg would allow you to more easily filter out 404 requests that have nothing to do with dynamic thumbnail generation. Say, for instance, that there are tons of 404 errors for /images/original_size_image.jpg. You wouldn't want those being routed to Django, and you could only match filenames of that format with regex. [end edit]

You have to be careful though about letting anybody aware of this feature spam your Django app. They could potentially kill it with an infinite number of image size and filename combinations at their fingertips. You would need to figure how to put upper limits on these requests, like redirecting back to a 404 if either dimension is larger than the original, or even figuring out how to cap requests for multiple dimensions of the same image. Maybe this was what you were you getting at when mentioning "locking" though.

As an aside, I see you've tagged Apache but I would really like to recommend that you serve static content through something like Nginx. You could maybe negate the extra overhead of the dynamic image requests if you use a static file server that isn't complete crap at serving static files.

jonwd7
Great answer. One solution to the spam issue is to have a predefined list of allowed image/thumbnail sizes. Also, I agree on nginx. I am using nginx for serving static content currently and will add the nginx tag to this question.
Gattster
Ah, yes… but I was unsure of how "dynamic" a predefined list of allowed sizes would be to you, so I didn't mention it. I was also unsure of your image formats and if they are consistent or standard ratios, or if you were planning on cropping the thumb if it were to not fit a predefined size, etc, etc. Also, I'll be editing my answer momentarily to add something about the URL format you discuss.
jonwd7
A: 

you can take a peek @ http://thumbnail.sorl.net/docs/, i use it in almost all of my projects together with serving static content with nginx from the /media/ dir :)

Michał Ludwiński