mirror of
https://github.com/Hopiu/django-imagekit.git
synced 2026-03-16 21:30:23 +00:00
Added format processor to force a format/extension change
This commit is contained in:
parent
81f4b950cd
commit
70da53ba4d
5 changed files with 28 additions and 12 deletions
|
|
@ -15,7 +15,11 @@ class EnhanceSmall(processors.Adjustment):
|
|||
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]
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ class ImageProcessor(object):
|
|||
@classmethod
|
||||
def process(cls, img, fmt, obj):
|
||||
return img, fmt
|
||||
|
||||
|
||||
|
||||
class Adjustment(ImageProcessor):
|
||||
color = 1.0
|
||||
|
|
@ -35,6 +35,15 @@ class Adjustment(ImageProcessor):
|
|||
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
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ spec found.
|
|||
"""
|
||||
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
|
||||
|
|
@ -29,12 +30,13 @@ class ImageSpec(object):
|
|||
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
|
||||
|
||||
|
|
@ -55,7 +57,10 @@ class Accessor(object):
|
|||
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)
|
||||
|
|
@ -69,6 +74,9 @@ class Accessor(object):
|
|||
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(),
|
||||
|
|
|
|||
|
|
@ -53,8 +53,8 @@ class IKTest(TestCase):
|
|||
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()
|
||||
|
|
@ -76,7 +76,7 @@ class IKTest(TestCase):
|
|||
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):
|
||||
|
|
|
|||
|
|
@ -4,11 +4,6 @@ import tempfile
|
|||
|
||||
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
|
||||
|
|
|
|||
Loading…
Reference in a new issue