diff --git a/README.rst b/README.rst index 6309947c0..e25447214 100644 --- a/README.rst +++ b/README.rst @@ -1,20 +1,13 @@ -sorl-thumbnail -============== +django-thumbnail +================ -Thumbnails for Django. Totally rewritten. +A fork of sorl-thumbnail; created due to its inactivity. -Features at a glance --------------------- -- Storage support -- Pluggable Engine support (ImageMagick, PIL, pgmagick included) -- Pluggable Key Value Store support (cached db, redis) -- Pluggable Backend support -- Admin integration with possibility to delete -- Dummy generation (placeholders) -- Flexible, simple syntax, generates no html -- ImageField for model that deletes thumbnails -- CSS style cropping options -- Margin calculation for vertical positioning +pip install django-thumbnail -Read more in `the documentation (latest version) `_ +--------------------------------------------------------- + +Format preservation has been added. To preserve format set settings.THUMBNAIL_PRESERVE_FORMAT = True. +Gracefully degrades and defaults to using settings.THUMBNAIL_FORMAT. +Applications that currently use sorl should not notice any difference in functionality unless settings.THUMBNAIL_PRESERVE_FORMAT is set explicitly set to True. diff --git a/setup.py b/setup.py index 1458d11bc..3e955bc39 100644 --- a/setup.py +++ b/setup.py @@ -10,14 +10,14 @@ def run(self): setup( - name='sorl-thumbnail', + name='django-thumbnail', version=sorl.__version__, - description='Thumbnails for Django', + description='Thumbnailing in Django', long_description=open('README.rst').read(), - author='Mikko Hellsing', - author_email='mikko@aino.se', + author='Clinton Christian', + author_email='pygeek@me.com', license='BSD', - url='https://github.com/sorl/sorl-thumbnail', + url='https://github.com/pygeek/django-thumbnail', packages=find_packages(exclude=['tests', 'tests.*']), platforms='any', zip_safe=False, diff --git a/sorl/thumbnail/base.py b/sorl/thumbnail/base.py index 40ffaa529..0ab15b54e 100644 --- a/sorl/thumbnail/base.py +++ b/sorl/thumbnail/base.py @@ -1,3 +1,5 @@ +import re + from sorl.thumbnail.conf import settings, defaults as default_settings from sorl.thumbnail.helpers import tokey, serialize from sorl.thumbnail.images import ImageFile @@ -16,6 +18,7 @@ class ThumbnailBackend(object): The main class for sorl-thumbnail, you can subclass this if you for example want to change the way destination filename is generated. """ + default_options = { 'format': settings.THUMBNAIL_FORMAT, 'quality': settings.THUMBNAIL_QUALITY, @@ -29,6 +32,23 @@ class ThumbnailBackend(object): ('orientation', 'THUMBNAIL_ORIENTATION'), ) + file_extension = lambda inst, file_: str(file_).split('.')[-1].lower() + + def _get_format(self, file_): + file_extension = self.file_extension(file_) + + is_jpeg = re.match('jpg|jpeg', file_extension) + is_png = re.match('png', file_extension) + + if is_jpeg: + format_ = 'JPEG' + elif is_png: + format_ = 'PNG' + else: + format_ = default_settings.THUMBNAIL_FORMAT + + return str(format_) + def get_thumbnail(self, file_, geometry_string, **options): """ Returns thumbnail as an ImageFile instance for file with geometry and @@ -36,6 +56,11 @@ def get_thumbnail(self, file_, geometry_string, **options): secondly it will create it. """ source = ImageFile(file_) + + #preserve image filetype + if settings.THUMBNAIL_PRESERVE_FORMAT: + self.default_options['format'] = self._get_format(file_) + for key, value in self.default_options.iteritems(): options.setdefault(key, value) # For the future I think it is better to add options only if they diff --git a/sorl/thumbnail/conf/defaults.py b/sorl/thumbnail/conf/defaults.py index a039ca9bf..3f9ebb43f 100644 --- a/sorl/thumbnail/conf/defaults.py +++ b/sorl/thumbnail/conf/defaults.py @@ -51,6 +51,8 @@ # Make sure the backend can handle the format you specify THUMBNAIL_FORMAT = 'JPEG' +THUMBNAIL_PRESERVE_FORMAT = False + # Colorspace, backends are required to implement: RGB, GRAY # Setting this to None will keep the original colorspace. THUMBNAIL_COLORSPACE = 'RGB'