Code

Ticket #961: thumbnailing.diff

File thumbnailing.diff, 4.5 KB (added by dcwatson@…, 9 years ago)
Line 
1Index: core/meta/__init__.py
2===================================================================
3--- core/meta/__init__.py       (revision 1491)
4+++ core/meta/__init__.py       (working copy)
5@@ -757,6 +757,9 @@
6                 func.alters_data = True
7                 setattr(new_class, 'save_%s_file' % f.name, func)
8                 if isinstance(f, ImageField):
9+                    if f.thumb_sizes:
10+                        setattr(new_class, 'get_%s_thumbnail_url' % f.name, curry(method_get_image_thumbnail_url, f))
11+                        setattr(new_class, 'get_%s_thumbnail_sizes' % f.name, curry(method_get_image_thumbnail_sizes, f))
12                     # Add get_BLAH_width and get_BLAH_height methods, but only
13                     # if the image field doesn't have width and height cache
14                     # fields.
15@@ -1215,24 +1218,47 @@
16 
17     # Write the file to disk.
18     setattr(self, field.attname, filename)
19-    fp = open(getattr(self, 'get_%s_filename' % field.name)(), 'wb')
20+    path = getattr(self, 'get_%s_filename' % field.name)()
21+    fp = open(path, 'wb')
22     fp.write(raw_contents)
23     fp.close()
24 
25     # Save the width and/or height, if applicable.
26-    if isinstance(field, ImageField) and (field.width_field or field.height_field):
27-        from django.utils.images import get_image_dimensions
28-        width, height = get_image_dimensions(getattr(self, 'get_%s_filename' % field.name)())
29-        if field.width_field:
30-            setattr(self, field.width_field, width)
31-        if field.height_field:
32-            setattr(self, field.height_field, height)
33+    if isinstance(field, ImageField):
34+        if field.thumb_sizes:
35+            from django.parts.media.photos import get_thumbnail_path
36+            from PIL import Image
37+            orig = Image.open(path)
38+            for size in field.thumb_sizes:
39+                thumb_path = get_thumbnail_path(path, str(size))
40+                thumb = orig.copy()
41+                thumb.thumbnail((size,size), Image.ANTIALIAS)
42+                thumb.save(thumb_path)
43+        if field.width_field or field.height_field:
44+            from django.utils.images import get_image_dimensions
45+            width, height = get_image_dimensions(path)
46+            if field.width_field:
47+                setattr(self, field.width_field, width)
48+            if field.height_field:
49+                setattr(self, field.height_field, height)
50 
51     # Save the object, because it has changed.
52     self.save()
53 
54 # IMAGE FIELD METHODS ######################
55 
56+def method_get_image_thumbnail_url(field, self, size=None):
57+    from django.parts.media.photos import get_thumbnail_url
58+    # If they don't specify a size, or the size doesn't exist,
59+    # just give them back the first available thumbnail url.
60+    if (size == None) or (size not in field.thumb_sizes):
61+        size = field.thumb_sizes[0]
62+    url = getattr(self, "get_%s_url" % field.name)()
63+    return get_thumbnail_url(url, str(size))
64+
65+def method_get_image_thumbnail_sizes(field, self):
66+    return field.thumb_sizes
67+
68 def method_get_image_width(field, self):
69     return _get_image_dimensions(field, self)[0]
70 
71Index: core/meta/fields.py
72===================================================================
73--- core/meta/fields.py (revision 1491)
74+++ core/meta/fields.py (working copy)
75@@ -549,8 +549,8 @@
76         return [curry(formfields.FloatField, max_digits=self.max_digits, decimal_places=self.decimal_places)]
77 
78 class ImageField(FileField):
79-    def __init__(self, verbose_name=None, name=None, width_field=None, height_field=None, **kwargs):
80-        self.width_field, self.height_field = width_field, height_field
81+    def __init__(self, verbose_name=None, name=None, width_field=None, height_field=None, thumb_sizes=None, **kwargs):
82+        self.width_field, self.height_field, self.thumb_sizes = width_field, height_field, thumb_sizes
83         FileField.__init__(self, verbose_name, name, **kwargs)
84 
85     def get_manipulator_field_objs(self):
86Index: parts/media/photos.py
87===================================================================
88--- parts/media/photos.py       (revision 1491)
89+++ parts/media/photos.py       (working copy)
90@@ -1,6 +1,9 @@
91-import re
92+import re, os
93 
94+def get_thumbnail_path(photo_url, width, sep=os.path.sep):
95+    bits = photo_url.split(sep)
96+    bits[-1] = re.sub(r'(?i)\.(gif|jpg)$', '_t%s.\\1' % width, bits[-1])
97+    return sep.join(bits)
98+
99 def get_thumbnail_url(photo_url, width):
100-    bits = photo_url.split('/')
101-    bits[-1] = re.sub(r'(?i)\.(gif|jpg)$', '_t%s.\\1' % width, bits[-1])
102-    return '/'.join(bits)
103+    return get_thumbnail_path(photo_url, width, '/')