Ticket #961: thumbnailing.diff

File thumbnailing.diff, 4.5 KB (added by dcwatson@…, 19 years ago)
  • core/meta/__init__.py

     
    757757                func.alters_data = True
    758758                setattr(new_class, 'save_%s_file' % f.name, func)
    759759                if isinstance(f, ImageField):
     760                    if f.thumb_sizes:
     761                        setattr(new_class, 'get_%s_thumbnail_url' % f.name, curry(method_get_image_thumbnail_url, f))
     762                        setattr(new_class, 'get_%s_thumbnail_sizes' % f.name, curry(method_get_image_thumbnail_sizes, f))
    760763                    # Add get_BLAH_width and get_BLAH_height methods, but only
    761764                    # if the image field doesn't have width and height cache
    762765                    # fields.
     
    12151218
    12161219    # Write the file to disk.
    12171220    setattr(self, field.attname, filename)
    1218     fp = open(getattr(self, 'get_%s_filename' % field.name)(), 'wb')
     1221    path = getattr(self, 'get_%s_filename' % field.name)()
     1222    fp = open(path, 'wb')
    12191223    fp.write(raw_contents)
    12201224    fp.close()
    12211225
    12221226    # Save the width and/or height, if applicable.
    1223     if isinstance(field, ImageField) and (field.width_field or field.height_field):
    1224         from django.utils.images import get_image_dimensions
    1225         width, height = get_image_dimensions(getattr(self, 'get_%s_filename' % field.name)())
    1226         if field.width_field:
    1227             setattr(self, field.width_field, width)
    1228         if field.height_field:
    1229             setattr(self, field.height_field, height)
     1227    if isinstance(field, ImageField):
     1228        if field.thumb_sizes:
     1229            from django.parts.media.photos import get_thumbnail_path
     1230            from PIL import Image
     1231            orig = Image.open(path)
     1232            for size in field.thumb_sizes:
     1233                thumb_path = get_thumbnail_path(path, str(size))
     1234                thumb = orig.copy()
     1235                thumb.thumbnail((size,size), Image.ANTIALIAS)
     1236                thumb.save(thumb_path)
     1237        if field.width_field or field.height_field:
     1238            from django.utils.images import get_image_dimensions
     1239            width, height = get_image_dimensions(path)
     1240            if field.width_field:
     1241                setattr(self, field.width_field, width)
     1242            if field.height_field:
     1243                setattr(self, field.height_field, height)
    12301244
    12311245    # Save the object, because it has changed.
    12321246    self.save()
    12331247
    12341248# IMAGE FIELD METHODS ######################
    12351249
     1250def method_get_image_thumbnail_url(field, self, size=None):
     1251    from django.parts.media.photos import get_thumbnail_url
     1252    # If they don't specify a size, or the size doesn't exist,
     1253    # just give them back the first available thumbnail url.
     1254    if (size == None) or (size not in field.thumb_sizes):
     1255        size = field.thumb_sizes[0]
     1256    url = getattr(self, "get_%s_url" % field.name)()
     1257    return get_thumbnail_url(url, str(size))
     1258
     1259def method_get_image_thumbnail_sizes(field, self):
     1260    return field.thumb_sizes
     1261
    12361262def method_get_image_width(field, self):
    12371263    return _get_image_dimensions(field, self)[0]
    12381264
  • core/meta/fields.py

     
    549549        return [curry(formfields.FloatField, max_digits=self.max_digits, decimal_places=self.decimal_places)]
    550550
    551551class ImageField(FileField):
    552     def __init__(self, verbose_name=None, name=None, width_field=None, height_field=None, **kwargs):
    553         self.width_field, self.height_field = width_field, height_field
     552    def __init__(self, verbose_name=None, name=None, width_field=None, height_field=None, thumb_sizes=None, **kwargs):
     553        self.width_field, self.height_field, self.thumb_sizes = width_field, height_field, thumb_sizes
    554554        FileField.__init__(self, verbose_name, name, **kwargs)
    555555
    556556    def get_manipulator_field_objs(self):
  • parts/media/photos.py

     
    1 import re
     1import re, os
    22
     3def get_thumbnail_path(photo_url, width, sep=os.path.sep):
     4    bits = photo_url.split(sep)
     5    bits[-1] = re.sub(r'(?i)\.(gif|jpg)$', '_t%s.\\1' % width, bits[-1])
     6    return sep.join(bits)
     7
    38def get_thumbnail_url(photo_url, width):
    4     bits = photo_url.split('/')
    5     bits[-1] = re.sub(r'(?i)\.(gif|jpg)$', '_t%s.\\1' % width, bits[-1])
    6     return '/'.join(bits)
     9    return get_thumbnail_path(photo_url, width, '/')
Back to Top