Skip to content

Commit

Permalink
Merge pull request superdesk#894 from MarkLark86/SDESK-237
Browse files Browse the repository at this point in the history
[SDESK_237] - Image renditions shorter for few px in width
  • Loading branch information
petrjasek authored Apr 24, 2017
2 parents b2f6ab9 + a6ac978 commit 92667e9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
38 changes: 22 additions & 16 deletions apps/picture_crop/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,28 @@ def get_crop_size(crop):
:param size: size dict with `width` or `height`
:param crop: crop specs
"""
size = {}
x = crop['CropRight'] - crop['CropLeft']
y = crop['CropBottom'] - crop['CropTop']

size['width'] = crop.get('width', x)
size['height'] = crop.get('height', y)

if size: # preserve aspect ratio
width, height = size['width'], size['height']
if x > width:
y = int(max(y * width / x, 1))
x = int(width)
if y > height:
x = int(max(x * height / y, 1))
y = int(height)
size['width'], size['height'] = x, y
crop_width = crop['CropRight'] - crop['CropLeft']
crop_height = crop['CropBottom'] - crop['CropTop']

size = {
'width': crop.get('width', crop_width),
'height': crop.get('height', crop_height)
}

crop_ratio = crop_height / crop_width
size_ratio = size['height'] / size['width']

# Keep crop data proportional to the size provided
# i.e. if the rendition is 4x3, make sure the crop data is also a 4x3 aspect ratio
if crop_ratio != size_ratio:
crop_width = int(crop_height / size_ratio)
crop_height = int(crop_width * size_ratio)

# Calculating from the top-left, re-assign the cropping coordinates
# based on the new aspect ratio of the crop
crop['CropRight'] = crop['CropLeft'] + crop_width
crop['CropBottom'] = crop['CropTop'] + crop_height

return size


Expand Down
2 changes: 1 addition & 1 deletion features/content_crop.feature
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ Feature: Cropping the Image Articles
"""
Then we get new resource
"""
{"width": 5, "height": 5, "href": "__any_value__"}
{"width": 5, "height": 10, "href": "__any_value__"}
"""

@auth
Expand Down
27 changes: 24 additions & 3 deletions tests/media/picture_crop_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from superdesk import get_resource_service
from ..media import get_picture_fixture

from apps.picture_crop import get_crop_size


def get_file_mock(rendition, item):
filename = get_picture_fixture()
Expand Down Expand Up @@ -53,12 +55,31 @@ def test_crop_image_copies_metadata(self, get_file_function, crop_image_function
},
'crop': {
'CropLeft': 10,
'CropRight': 10,
'CropTop': 5,
'CropBottom': 5
'CropRight': 100,
'CropTop': 10,
'CropBottom': 100
}
}])

self.assertEqual(images[0].metadata.get('datetime'), '"2015:07:06 16:30:23"')
self.assertEqual(images[0].metadata.get('exifimagewidth'), '400')
self.assertEqual(images[0].metadata.get('exifimageheight'), '300')

def test_get_crop_size_fixes_crop_aspect_ratio(self):
crop_data = {
'CropLeft': 0,
'CropRight': 1620,
'CropTop': 0,
'CropBottom': 1230,
'width': 800,
'height': 600
}
get_crop_size(crop_data)

crop_width = crop_data['CropRight'] - crop_data['CropLeft']
crop_height = crop_data['CropBottom'] - crop_data['CropTop']

crop_ratio = crop_height / crop_width
size_ratio = crop_data['height'] / crop_data['width']

self.assertEqual(crop_ratio, size_ratio)

0 comments on commit 92667e9

Please sign in to comment.