Download

Check out with Subversion: svn co http://django.org.ua/svn/plesonet_imagefield/trunk/ plesonet_imagefield

README.txt

Image field based on ImageWithThumbnailsField from sorl-thumnail 
(https://sorl-thumbnail.googlecode.com/hg/sorl/thumbnail/fields.py).
Used for Django 1.0.

Functionality added:
 * 'max_width' and 'max_height' optional arguments to fit uploaded image in
specified size.
 * image preview in admin and generated forms
 * 'delete image' checkbox in admin and generated forms wich allows to delete
image file on form save if it is not reqired.

Usage:
 * install sorl-thumnail
 * import ImrpovedImageWithThumbnailsField to your models module
 * use as usual image field
 * see http://thumbnail.sorl.net/docs/#imagewiththumbnailsfield
ImageWithThumbnailsField section to setup thumbnails. 'thumbnail' argument will
be used as preview image in admin.
 * optionally set 'max_width' and 'max_height' to reduce big images

Example usage in model:

...
class MyModel(models.Model)
    photo = ImrpovedImageWithThumbnailsField(_("Photo"), max_width=1024, max_height=1024, upload_to="images/%Y-%m/",
                                             # Thumbnail for admin site.
                                             thumbnail={
                                                 'size': (70, 70),
                                                 'options': ('crop', 'upscale')
                                             },
                                             extra_thumbnails={
                                                 'main': {
                                                     'size': (200, 200),
                                                     'options': ('crop', 'upscale',),
                                                 }
                                             })
    
    ...
    
    def photo_img(self):
        if self.photo:
            return self.photo.thumbnail_tag
        else:
            return ""
    photo_img.short_description = _("Photo")
    photo_img.allow_tags = True
...

Example usage in admin config:

...
class MyModelAdmin(admin.ModelAdmin):
    list_display = ('logo_img', )
...

Example usage of extra thumbnail in template:

...
{% if item.photo %}
    <img src="{{ item.photo.extra_thumbnails.main }}" width="{{ item.photo.extra_thumbnails.main.width }}" height="{{ item.photo.extra_thumbnails.main.height }}" alt="{{ item }}" />
{% endif %}
...
{# Or shorter way, that works same - rendres <img /> tag with proper attributes: #}
{% if item.photo %}
    {{ item.photo.extra_thumbnails_tags.main }}
{% endif %}