Example mixin for creating and removing thumbnails on a model. Also adds some methods for accessing an url for an image size. It might be a better idea to use a custom image field instead of this approach.
Idea for getting the image url for different sizes from http://code.google.com/p/django-photologue
Example usage <pre>
p.get_small_image_url() u'http://127.0.0.1:8000/site_media/photos/small_someimage.jpg' </pre>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | from django.utils.functional import curry
import Image
import os
class ThumbnailMixIn(object):
def add_accessor_methods(self, *args, **kwargs):
for size in self.image_sizes:
setattr(self, 'get_%s_image_url' % size, curry(self._get_SIZE_image_url, size=size))
def _get_SIZE_image_url(self, size):
return self.get_image_url(size)
def create_thumbnails(self):
for size in self.image_sizes:
self.create_thumbnail(size)
def delete_thumbnails(self):
try:
for size in self.image_sizes:
os.remove(self.get_image_path(size))
except:
pass
def create_thumbnail(self, size):
image_size = self.image_sizes.get(size)
img = Image.open(self.image.path)
img.thumbnail((
image_size.get('width'),
image_size.get('height')
), Image.ANTIALIAS)
img.save(self.get_image_path(size))
def get_image_path(self, size):
return os.sep.join([
os.path.dirname(self.image.path),
self.get_image_filename(size)
])
def get_image_url(self, size):
return '/'.join([
os.path.dirname(self.image.url),
self.get_image_filename(size)
])
def get_image_filename(self,size):
return '_'.join([size,os.path.basename(self.image.path)])
def admin_thumbnail(self):
if self.image_sizes.has_key('admin'):
return '<img src="%s" alt="" />' % (self.get_image_url('admin'))
return ' '
admin_thumbnail.allow_tags = True
# The model
class Photo(models.Model, ThumbnailMixIn):
title = models.CharField(max_length=200)
image = models.ImageField(upload_to='photos') # Must be named image.
image_sizes = {
'admin': {'width': 50, 'height': 50},
'small': {'width': 300, 'height': 300},
'medium': {'width': 500, 'height': 500}
}
# Signals
def add_accessor_methods(sender, instance, signal, *args, **kwargs):
if hasattr(instance, 'add_accessor_methods'):
instance.add_accessor_methods()
def create_thumbnails(sender, instance, signal, *args, **kwargs):
if hasattr(sender, 'create_thumbnails'):
sender.create_thumbnails(instance)
def delete_thumbnails(sender, instance, signal, *args, **kwargs):
if hasattr(sender, 'delete_thumbnails'):
sender.delete_thumbnails(instance)
post_init.connect(add_accessor_methods)
post_save.connect(create_thumbnails, sender=Photo)
post_delete.connect(delete_thumbnails, sender=Photo)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.