Skip to content

Commit

Permalink
Allow configuration of crop gravity when using crop option. Fixed #7.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephrdev committed Mar 13, 2017
1 parent dcc12ae commit 8aedea8
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,21 @@ To resize static images, just prefix the path with ``static:``, for example:
There are many other options/parameters to pass to the templatetag. Please refer
to the codebase until the documentation is more complete.
Options
-------
You can pass some options to the thumbnail tag:
* upscale: Configures if the input should be upscaled if requested sizes are larger than source.
* retina: Option to enable retina support (by providing both url and url_2x)
* crop: Deside if images should be cropped if requested sizes doesn't fit source aspect ratio.
* quality: Configures quality for image compression
* pngquant: Configures the pngquant compression factor
.. hint::
The `crop` option can be set to True for default gravity when cropping (which is `Center`).
You can also pass valid GraphicsMagick gravities (North, NorthEeast, East, SouthEast, ...)
or their abbreviation (N, NE, E, SE, ...)
31 changes: 31 additions & 0 deletions ultimatethumb/tests/test_thumbnail.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,37 @@ def test_gm_options_crop(self):
('quality', 90),
]

@pytest.mark.parametrize('crop,expected', [
(True, 'Center'),
(1, 'Center'),
('C', 'Center'),
('N', 'North'),
('NW', 'NorthWest'),
('NE', 'NorthEast'),
('W', 'West'),
('E', 'East'),
('S', 'South'),
('SW', 'SouthWest'),
('SE', 'SouthEast'),
('Center', 'Center'),
('North', 'North'),
('NorthWest', 'NorthWest'),
('NorthEast', 'NorthEast'),
('West', 'West'),
('East', 'East'),
('South', 'South'),
('SouthWest', 'SouthWest'),
('SouthEast', 'SouthEast'),
(0, None),
(False, None),
(None, None),
('invalid', None),
])
def test_gm_options_crop_gravities(self, crop, expected):
image = ImageModelFactory.create()
thumbnail = Thumbnail(image.file.path, {'size': ['100', '50'], 'crop': crop})
assert thumbnail.get_gm_options().get('gravity', None) == expected

def test_gm_options_quality(self):
image = ImageModelFactory.create()
thumbnail = Thumbnail(image.file.path, {'size': ['100', '50'], 'quality': 5})
Expand Down
30 changes: 28 additions & 2 deletions ultimatethumb/thumbnail.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,35 @@
from .utils import build_url, factor_size, get_size_for_path, get_thumb_data, get_thumb_name


CROP_GRAVITY = {
True: 'Center',
1: 'Center',
'C': 'Center',
'N': 'North',
'NW': 'NorthWest',
'NE': 'NorthEast',
'W': 'West',
'E': 'East',
'S': 'South',
'SW': 'SouthWest',
'SE': 'SouthEast',
'Center': 'Center',
'North': 'North',
'NorthWest': 'NorthWest',
'NorthEast': 'NorthEast',
'West': 'West',
'East': 'East',
'South': 'South',
'SouthWest': 'SouthWest',
'SouthEast': 'SouthEast',
}


Size = namedtuple('Size', ('width', 'height'))


class Thumbnail(object):

def __init__(self, source, opts):
self.source = source

Expand Down Expand Up @@ -170,8 +195,9 @@ def get_gm_options(self, factor=1):
resize_attrs
)

if self.options['crop']:
gm_options['gravity'] = 'Center'
crop = CROP_GRAVITY.get(self.options['crop'], False)
if crop:
gm_options['gravity'] = crop
gm_options['crop'] = '{0}x{1}+0+0'.format(
factor_size(size[0], factor),
factor_size(size[1], factor)
Expand Down

0 comments on commit 8aedea8

Please sign in to comment.