Commits

Anonymous committed 27586f3

Added format processor to force a format/extension change

  • Participants
  • Parent commits 721645d

Comments (0)

Files changed (5)

imagekit/defaults.py

 class SampleReflection(processors.Reflection):
     size = 0.5
     background_color = "#000000"
+
+class PNGFormat(processors.Format):
+    format = 'PNG'
+    extension = 'png'
     
 class DjangoAdminThumbnail(ImageSpec):
     access_as = 'admin_thumbnail'
-    processors = [ResizeThumbnail, EnhanceSmall, SampleReflection]
+    processors = [ResizeThumbnail, EnhanceSmall, SampleReflection, PNGFormat]

imagekit/processors.py

     @classmethod
     def process(cls, img, fmt, obj):
         return img, fmt
-
+        
 
 class Adjustment(ImageProcessor):
     color = 1.0
         return img, fmt
 
 
+class Format(ImageProcessor):
+    format = 'JPEG'
+    extension = 'jpg'
+    
+    @classmethod
+    def process(cls, img, fmt, obj):
+        return img, cls.format
+
+
 class Reflection(ImageProcessor):
     background_color = '#FFFFFF'
     size = 0.0

imagekit/specs.py

 """
 import os
 from StringIO import StringIO
+from imagekit import processors
 from imagekit.lib import *
 from imagekit.utils import img_to_fobj
 from django.core.files.base import ContentFile
         for proc in cls.processors:
             img, fmt = proc.process(img, fmt, obj)
         img.format = fmt
-        return img
+        return img, fmt
         
 
 class Accessor(object):
     def __init__(self, obj, spec):
         self._img = None
+        self._fmt = None
         self._obj = obj
         self.spec = spec
         
         fp = self._obj._imgfield.storage.open(self._obj._imgfield.name)
         fp.seek(0)
         fp = StringIO(fp.read())
-        self._img = self.spec.process(Image.open(fp), self._obj)
+        self._img, self._fmt = self.spec.process(Image.open(fp), self._obj)
+        for key, val in Image.EXTENSION.iteritems():
+            if val == self._fmt:
+                extension = key
         # save the new image to the cache
         content = ContentFile(self._get_imgfile().read())
         self._obj._imgfield.storage.save(self.name, content)
     def _basename(self):
         filename, extension =  \
             os.path.splitext(os.path.basename(self._obj._imgfield.name))
+        for processor in self.spec.processors:
+            if issubclass(processor, processors.Format):
+                extension = processor.extension
         return self._obj._ik.cache_filename_format % \
             {'filename': filename,
              'specname': self.spec.name(),

imagekit/tests.py

         Image.new('RGB', (800, 600)).save(self.tmp, 'JPEG')
         self.tmp.seek(0)
         self.p = TestPhoto()
-        self.p.image.save(os.path.basename('test.jpg'),
-                           ContentFile(self.tmp.read()))
+        self.p.image.save(os.path.basename('test.jpeg'),
+                          ContentFile(self.tmp.read()))
         self.p.save()
         # destroy temp file
         self.tmp.close()
         self.assertEqual(self.p.cropped.height, 100)
 
     def test_url(self):
-        tup = (settings.MEDIA_URL, self.p._ik.cache_dir, 'test_to_width.jpg')
+        tup = (settings.MEDIA_URL, self.p._ik.cache_dir, 'test_to_width.jpeg')
         self.assertEqual(self.p.to_width.url, "%s%s/%s" % tup)
  
     def tearDown(self):

imagekit/utils.py

 
 def img_to_fobj(img, format, **kwargs):
     tmp = tempfile.TemporaryFile()
-    if format != 'JPEG':
-        try:
-            img.save(tmp, format, **kwargs)
-        except KeyError:
-            pass
     img.save(tmp, format, **kwargs)
     tmp.seek(0)
     return tmp