diff --git a/README.rst b/README.rst index 8cc56828..8ed9d360 100644 --- a/README.rst +++ b/README.rst @@ -88,9 +88,10 @@ area. Frontend ======== -django-image-cropping provides a templatetag for displaying a cropped thumbnail:: +django-image-cropping provides a templatetag for displaying a cropped thumbnail. +Any other processor parameter (like ``bw=True`` or ``upscale=True``) will be forwarded to ``easy-thumbnails``:: - {% cropped_thumbnail yourmodelinstance "ratiofieldname" [scale=INT|width=INT|height=INT|max_size="INTxINT"] [upscale=True] %} + {% cropped_thumbnail yourmodelinstance "ratiofieldname" [scale=INT|width=INT|height=INT|max_size="INTxINT"] %} Example usage:: diff --git a/example/tests/test_templatetag.py b/example/tests/test_templatetag.py index 0aba47cd..c85e88d5 100644 --- a/example/tests/test_templatetag.py +++ b/example/tests/test_templatetag.py @@ -1,12 +1,16 @@ import itertools +import urllib +from PIL import Image from django.test import TestCase +from django.conf import settings from django.template import Template, Context from django.core.management import call_command from .factory import create_cropped_image + class TemplateTagTestBase(object): def setUp(self): # size of the example image is 400x400 @@ -105,6 +109,25 @@ def test_upscale(self): def test_adapt_rotation(self): pass + def test_parameter_forward(self): + ''' converts image to greyscale ''' + def is_greyscale(img_path): + # http://stackoverflow.com/a/23661373 + im = Image.open(img_path).convert('RGB') + w, h = im.size + for i in range(w): + for j in range(h): + r, g, b = im.getpixel((i, j)) + if r != g != b: + return False + return True + + url = self._test_cropping({'max_size': '"200x200"', + 'bw': True}) + self.assertTrue('120x100' in url) + path = settings.MEDIA_ROOT.rsplit('/', 1)[0] + urllib.unquote(url) + self.assertTrue(is_greyscale(path)) + class FreeCroppingTestCase(TemplateTagTestBase, TestCase): def _test_free_cropping(self, options={}): diff --git a/image_cropping/templatetags/cropping.py b/image_cropping/templatetags/cropping.py index 25d01155..a67af7a2 100644 --- a/image_cropping/templatetags/cropping.py +++ b/image_cropping/templatetags/cropping.py @@ -2,6 +2,7 @@ from easy_thumbnails.files import get_thumbnailer register = template.Library() +VALID_OPTIONS = ('scale', 'width', 'height', 'max_size') @register.simple_tag(takes_context=True) @@ -32,8 +33,7 @@ def cropped_thumbnail(context, instance, ratiofieldname, **kwargs): else: size = (int(ratiofield.width), int(ratiofield.height)) - valid_options = ('scale', 'width', 'height', 'max_size') - if sum(k in kwargs for k in valid_options) > 1: + if sum(k in kwargs for k in VALID_OPTIONS) > 1: raise template.TemplateSyntaxError( 'Only one size modifier is allowed.') @@ -63,7 +63,7 @@ def cropped_thumbnail(context, instance, ratiofieldname, **kwargs): width = max_height * width / height height = max_height - if any(k in kwargs for k in valid_options): + if any(k in kwargs for k in VALID_OPTIONS): # adjust size based on given modifier size = (int(width), int(height)) @@ -77,7 +77,13 @@ def cropped_thumbnail(context, instance, ratiofieldname, **kwargs): 'size': size, 'box': box, 'crop': True, - 'detail': True, + 'detail': kwargs.pop('detail', True), 'upscale': kwargs.pop('upscale', False) } + # remove all cropping kwargs + for k in VALID_OPTIONS: + kwargs.pop(k, None) + # pass remaining arguments to easy_thumbnail + thumbnail_options.update(kwargs) + return thumbnailer.get_thumbnail(thumbnail_options).url