Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format Preservation with minimal alterations #135

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 9 additions & 16 deletions 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) <http://sorl-thumbnail.rtfd.org/>`_
---------------------------------------------------------


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.
10 changes: 5 additions & 5 deletions setup.py
Expand Up @@ -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,
Expand Down
25 changes: 25 additions & 0 deletions 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
Expand All @@ -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,
Expand All @@ -29,13 +32,35 @@ 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
options given. First it will try to get it from the key value store,
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
Expand Down
2 changes: 2 additions & 0 deletions sorl/thumbnail/conf/defaults.py
Expand Up @@ -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'
Expand Down