Skip to content

Commit

Permalink
pass additional arguments to as easy-thumbnail options, solves #38, m…
Browse files Browse the repository at this point in the history
…ay solve #48
  • Loading branch information
escaped committed Sep 5, 2014
1 parent 290182b commit 3359fd5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
5 changes: 3 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::

Expand Down
23 changes: 23 additions & 0 deletions example/tests/test_templatetag.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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={}):
Expand Down
14 changes: 10 additions & 4 deletions image_cropping/templatetags/cropping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.')

Expand Down Expand Up @@ -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))

Expand All @@ -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

0 comments on commit 3359fd5

Please sign in to comment.