Thumbnail support
SVN: svn://trac.studioquattro.biz/django-utils/trunk/nesh/thumbnail
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:
from nesh.thumbnail.field import ImageWithThumbnailField class Foo(models.Model): .... image = ImageWithThumbnailField(<all of ImageField options apply>)
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:
<img src="{{ url|thumbnail:"width=10,height=20" }}" width="10" height="20" /> <img src="{{ url|thumbnail:"width=10" }}" width="10" /> <img src="{{ url|thumbnail:"height=20" }}" height="20" />
Also you can use a filter tag like this:
{% 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:
%(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)::
%(dirname)s/%(basename)s_t%(width)d.%(extension)s
image_width, image_height
Return image width or image height.
Usage:
<img src="{{ url }}" width="{{ url|image_width }}" /> <img src="{{ url }}" height="{{ url|image_height }}" />
Installation
See: App Inst for install instructions.
Then, in your templates use it like this:
{% load nesh.thumbnail %}
<img src="{{ img.get_image_url|thumbnail:"width=10,height=20" }}" />
better yet, define width and/or height for img tag also::
{% load nesh.thumbnail %}
<img src="{{ img.get_image_url|thumbnail:"width=10,height=20" }}" width="10" height="20" />
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/):
# serve media for runserver if settings.LOCAL_DEV: import os urlpatterns += patterns('', (r'^media/(?P<path>.*)$', '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.
Comments
Or you can post to nesh-django-utils mail list.
Recent Changes
