Export to GitHub

django-utils - Thumbnail.wiki


Thumbnail support

This library gives you three new tags and a new ImageWithThumbnailField.

With ImageWithThumbnailField you will get automatic thumbnail creation for admin interface (width=120) and thumbnail removal when image is changed.

All image sizes are cached within private locmem:// instance to reduce filesystem access.

Python imaging library (PIL) is required.

ImageWithThumbnailField

Use this instead of ImageField and your will get (for free ;) ) automatic thumbnail generation and deletion.

Usage: ```

!python

from nesh.thumbnail.field import ImageWithThumbnailField

class Foo(models.Model): .... image = ImageWithThumbnailField() ```

Additonal parameters: * auto_rename: default True - automatically rename image files to <class name>-<field name>-<object pk>.<ext>.

Template filters

thumbnail

Returns thumbnail URL and create it if not already exists.

Usage: ```

!text/html

```

Also you can use a filter tag like this: ```

!text/html

{% filter thumbnail:"width=120" %}foo.png{% endfilter %} ```

I recommend to always use width and/or height attributes for img tag to ensure that proper dimensions are always enforced.

Parameters: * width: requested image width * height: requested image height

Image is proportionally resized to dimension which is no greater than width x height.

Thumbnail file is saved in the same location as the original image and his name is constructed like this: ```

!python

%(dirname)s/%(basename)s_t[_w%(width)d][_h%(height)d].%(extension)s or if only a width is requested (to be compatible with admin interface)::

!python

%(dirname)s/%(basename)s_t%(width)d.%(extension)s ```

image_width, image_height

Return image width or image height.

Usage: ```

!text/html

```

Installation

See: AppInst for install instructions.

Then, in your templates use it like this: ```

!text/html

{% load nesh.thumbnail %} ```

better yet, define width and/or height for img tag also:: ```

!text/html

{% load nesh.thumbnail %} ```

Note:

I recommend to define only your most important dimension (width/height) because resize is done proportionally so there is not guarantee that both dimensions will be exact as you specified.

Serving images with development server (runserver)

Thanks to Chris Moffitt for pointing this out.

If you are using the development server you can add this to your main urlconf to enable access to your media files (from http://www.djangoproject.com/documentation/static_files/): ```

!python

serve media for runserver

if settings.LOCAL_DEV: import os urlpatterns += patterns('', (r'^media/(?P.*)$', 'django.views.static.serve', {'document_root': os.path.join(settings.SITE_ROOT,'media')}), ) # ```

Where LOCAL_DEV is just a flag from my settings to indicate that I'm doing local development and SITE_ROOT is root of my project.