Ticket #961: thumbnailing.diff
File thumbnailing.diff, 4.5 KB (added by , 19 years ago) |
---|
-
core/meta/__init__.py
757 757 func.alters_data = True 758 758 setattr(new_class, 'save_%s_file' % f.name, func) 759 759 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)) 760 763 # Add get_BLAH_width and get_BLAH_height methods, but only 761 764 # if the image field doesn't have width and height cache 762 765 # fields. … … 1215 1218 1216 1219 # Write the file to disk. 1217 1220 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') 1219 1223 fp.write(raw_contents) 1220 1224 fp.close() 1221 1225 1222 1226 # 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) 1230 1244 1231 1245 # Save the object, because it has changed. 1232 1246 self.save() 1233 1247 1234 1248 # IMAGE FIELD METHODS ###################### 1235 1249 1250 def 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 1259 def method_get_image_thumbnail_sizes(field, self): 1260 return field.thumb_sizes 1261 1236 1262 def method_get_image_width(field, self): 1237 1263 return _get_image_dimensions(field, self)[0] 1238 1264 -
core/meta/fields.py
549 549 return [curry(formfields.FloatField, max_digits=self.max_digits, decimal_places=self.decimal_places)] 550 550 551 551 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_field552 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 554 554 FileField.__init__(self, verbose_name, name, **kwargs) 555 555 556 556 def get_manipulator_field_objs(self): -
parts/media/photos.py
1 import re 1 import re, os 2 2 3 def 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 3 8 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) 9 return get_thumbnail_path(photo_url, width, '/')