Ticket #961: thumbnailing2.diff
File thumbnailing2.diff, 5.8 KB (added by , 19 years ago) |
---|
-
core/meta/__init__.py
832 832 func.alters_data = True 833 833 setattr(new_class, 'save_%s_file' % f.name, func) 834 834 if isinstance(f, ImageField): 835 if f.thumb_sizes: 836 setattr(new_class, 'get_%s_thumbnail_url' % f.name, curry(method_get_image_thumbnail_url, f)) 837 setattr(new_class, 'get_%s_thumbnail_sizes' % f.name, curry(method_get_image_thumbnail_sizes, f)) 835 838 # Add get_BLAH_width and get_BLAH_height methods, but only 836 839 # if the image field doesn't have width and height cache 837 840 # fields. … … 1066 1069 # delete it from the filesystem. 1067 1070 if os.path.exists(file_name) and not opts.get_model_module().get_list(**{'%s__exact' % f.name: getattr(self, f.name)}): 1068 1071 os.remove(file_name) 1072 if isinstance(f, ImageField) and f.thumb_sizes: 1073 from django.parts.media.photos import get_thumbnail_path 1074 for size in f.thumb_sizes: 1075 thumb_path = get_thumbnail_path(file_name, str(size)) 1076 os.remove(thumb_path) 1069 1077 # Run any post-delete hooks. 1070 1078 if hasattr(self, '_post_delete'): 1071 1079 self._post_delete() … … 1290 1298 1291 1299 # Write the file to disk. 1292 1300 setattr(self, field.attname, filename) 1293 fp = open(getattr(self, 'get_%s_filename' % field.name)(), 'wb') 1301 path = getattr(self, 'get_%s_filename' % field.name)() 1302 fp = open(path, 'wb') 1294 1303 fp.write(raw_contents) 1295 1304 fp.close() 1296 1305 1297 # Save the width and/or height, if applicable. 1298 if isinstance(field, ImageField) and (field.width_field or field.height_field): 1299 from django.utils.images import get_image_dimensions 1300 width, height = get_image_dimensions(getattr(self, 'get_%s_filename' % field.name)()) 1301 if field.width_field: 1302 setattr(self, field.width_field, width) 1303 if field.height_field: 1304 setattr(self, field.height_field, height) 1306 # Save the thumbnails, width, and height, if applicable. 1307 if isinstance(field, ImageField): 1308 if field.thumb_sizes: 1309 from django.parts.media.photos import generate_thumbnail 1310 for size in field.thumb_sizes: 1311 generate_thumbnail(path, size, field.thumb_mode) 1312 if field.width_field or field.height_field: 1313 from django.utils.images import get_image_dimensions 1314 width, height = get_image_dimensions(path) 1315 if field.width_field: 1316 setattr(self, field.width_field, width) 1317 if field.height_field: 1318 setattr(self, field.height_field, height) 1305 1319 1306 1320 # Save the object, because it has changed. 1307 1321 self.save() 1308 1322 1309 1323 # IMAGE FIELD METHODS ###################### 1310 1324 1325 def method_get_image_thumbnail_url(field, self, size=None): 1326 from django.parts.media.photos import get_thumbnail_url 1327 # If they don't specify a size, or the size doesn't exist, 1328 # just give them back the first available thumbnail url. 1329 if (size == None) or (size not in field.thumb_sizes): 1330 size = field.thumb_sizes[0] 1331 url = getattr(self, "get_%s_url" % field.name)() 1332 return get_thumbnail_url(url, str(size)) 1333 1334 def method_get_image_thumbnail_sizes(field, self): 1335 return field.thumb_sizes 1336 1311 1337 def method_get_image_width(field, self): 1312 1338 return _get_image_dimensions(field, self)[0] 1313 1339 -
core/meta/fields.py
19 19 # Values for Relation.edit_inline. 20 20 TABULAR, STACKED = 1, 2 21 21 22 # Values for ImageField's thumb_mode. 23 WIDTH, HEIGHT, BOTH = 1, 2, 3 24 22 25 RECURSIVE_RELATIONSHIP_CONSTANT = 'self' 23 26 24 27 # prepares a value for use in a LIKE query … … 549 552 return [curry(formfields.FloatField, max_digits=self.max_digits, decimal_places=self.decimal_places)] 550 553 551 554 class 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_field555 def __init__(self, verbose_name=None, name=None, width_field=None, height_field=None, thumb_sizes=None, thumb_mode=BOTH, **kwargs): 556 self.width_field, self.height_field, self.thumb_sizes, self.thumb_mode = width_field, height_field, thumb_sizes, thumb_mode 554 557 FileField.__init__(self, verbose_name, name, **kwargs) 555 558 556 559 def get_manipulator_field_objs(self): -
parts/media/photos.py
1 import re 1 from django.core import meta 2 import re, os 2 3 4 def get_thumbnail_path(photo_path, width, sep=os.path.sep): 5 bits = photo_path.split(sep) 6 bits[-1] = re.sub(r'(?i)\.(gif|jpg|png)$', '_t%s.\\1' % width, bits[-1]) 7 return sep.join(bits) 8 3 9 def 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) 10 return get_thumbnail_path(photo_url, width, '/') 11 12 def generate_thumbnail(photo_path, size, mode): 13 from PIL import Image 14 im = Image.open(photo_path) 15 thumb_path = get_thumbnail_path(photo_path, str(size)) 16 thumb_size = (size, size) 17 if mode == meta.WIDTH: 18 thumb_size = (size, 9999) 19 elif mode == meta.HEIGHT: 20 thumb_size = (9999, size) 21 im.thumbnail(thumb_size, Image.ANTIALIAS) 22 im.save(thumb_path)