Skip to content

Commit

Permalink
Fix oversize cropping.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephrdev committed Nov 3, 2017
1 parent e91a49d commit b6b08b3
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 5 deletions.
88 changes: 85 additions & 3 deletions ultimatethumb/tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def test_oversize(self):
assert context['img'][1].requested_size.width == '200'
assert context['img'][1].requested_size.height == '0'
assert context['img'][2].requested_size.width == '210'
assert context['img'][2].requested_size.height == '100'
assert context['img'][2].requested_size.height == '0'

def test_oversize_upscale(self):
source = ImageModelFactory.create(file__width=210, file__height=100)
Expand All @@ -176,6 +176,88 @@ def test_oversize_upscale(self):
assert context['img'][3].requested_size.width == '400'
assert context['img'][3].requested_size.height == '0'

def test_oversize_exact(self):
source = ImageModelFactory.create(file__width=210, file__height=100)

template = Template((
'{%% load ultimatethumb_tags %%}'
'{%% ultimatethumb "img" "%s" sizes="100x20,200x20,300x20,400x20" retina=False %%}'
) % source.file.path)

context = Context()
assert template.render(context) == ''

assert 'img' in context
assert len(context['img']) == 3
assert context['img'][0].requested_size.width == '100'
assert context['img'][0].requested_size.height == '20'
assert context['img'][1].requested_size.width == '200'
assert context['img'][1].requested_size.height == '20'
assert context['img'][2].requested_size.width == '210'
assert context['img'][2].requested_size.height == '14'

def test_oversize_exact_crop(self):
source = ImageModelFactory.create(file__width=210, file__height=100)

template = Template((
'{%% load ultimatethumb_tags %%}'
'{%% ultimatethumb "img" "%s" sizes="100x20,200x20,300x20,400x20" '
'crop=True retina=False %%}'
) % source.file.path)

context = Context()
assert template.render(context) == ''

assert 'img' in context
assert len(context['img']) == 3
assert context['img'][0].requested_size.width == '100'
assert context['img'][0].requested_size.height == '20'
assert context['img'][1].requested_size.width == '200'
assert context['img'][1].requested_size.height == '20'
assert context['img'][2].requested_size.width == '210'
assert context['img'][2].requested_size.height == '20'

def test_oversize_exact_cross_aspect(self):
source = ImageModelFactory.create(file__width=210, file__height=420)

template = Template((
'{%% load ultimatethumb_tags %%}'
'{%% ultimatethumb "img" "%s" sizes="100x20,200x20,300x20,400x20" retina=False %%}'
) % source.file.path)

context = Context()
assert template.render(context) == ''

assert 'img' in context
assert len(context['img']) == 3
assert context['img'][0].requested_size.width == '100'
assert context['img'][0].requested_size.height == '20'
assert context['img'][1].requested_size.width == '200'
assert context['img'][1].requested_size.height == '20'
assert context['img'][2].requested_size.width == '210'
assert context['img'][2].requested_size.height == '14'

def test_oversize_exact_crop_cross_aspect(self):
source = ImageModelFactory.create(file__width=210, file__height=420)

template = Template((
'{%% load ultimatethumb_tags %%}'
'{%% ultimatethumb "img" "%s" sizes="100x20,200x20,300x20,400x20" '
'crop=True retina=False %%}'
) % source.file.path)

context = Context()
assert template.render(context) == ''

assert 'img' in context
assert len(context['img']) == 3
assert context['img'][0].requested_size.width == '100'
assert context['img'][0].requested_size.height == '20'
assert context['img'][1].requested_size.width == '200'
assert context['img'][1].requested_size.height == '20'
assert context['img'][2].requested_size.width == '210'
assert context['img'][2].requested_size.height == '20'

def test_retina(self):
source = ImageModelFactory.create(file__width=210, file__height=100)

Expand All @@ -194,7 +276,7 @@ def test_retina(self):
assert context['img'][0].url is not None
assert context['img'][0].url_2x is not None
assert context['img'][1].requested_size.width == '105'
assert context['img'][1].requested_size.height == '50'
assert context['img'][1].requested_size.height == '0'
assert context['img'][1].url is not None
assert context['img'][1].url_2x is not None

Expand All @@ -220,5 +302,5 @@ def test_retina_disabled(self):
assert context['img'][1].url is not None
assert context['img'][1].url_2x is None
assert context['img'][2].requested_size.width == '210'
assert context['img'][2].requested_size.height == '100'
assert context['img'][2].requested_size.height == '0'
assert context['img'][2].url is not None
19 changes: 17 additions & 2 deletions ultimatethumb/thumbnail.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,23 @@ def get_thumbnails(self):
oversize = False
for size in self.get_sizes():
if '%' not in size[0] and not self.options.get('upscale', False):
if int(size[0]) > source_size[0] or int(size[1]) > source_size[1]:
size = [str(source_size[0]), str(source_size[1])]
thumb_size = (int(size[0]), int(size[1]))
if thumb_size[0] >= source_size[0] or thumb_size[1] >= source_size[1]:
if self.options.get('crop', False):
size[0] = str(min(source_size[0], thumb_size[0]))
size[1] = str(min(source_size[1], thumb_size[1]))
else:
if thumb_size[0] >= source_size[0]:
factor = float(source_size[0]) / thumb_size[0]
size[0] = str(source_size[0])
size[1] = str(int(round(
thumb_size[1] * factor))) if thumb_size[1] else '0'
else:
factor = float(source_size[1]) / thumb_size[1]
size[0] = str(int(round(
thumb_size[0] * factor))) if thumb_size[0] else '0'
size[1] = str(source_size[1])

oversize = True

options = {'size': size[0:2]}
Expand Down

0 comments on commit b6b08b3

Please sign in to comment.