From 4e7eada2c56766975baa8287f8c0f278e3160402 Mon Sep 17 00:00:00 2001 From: jagaSto Date: Thu, 23 May 2019 09:50:57 +0200 Subject: [PATCH 1/5] move safety checks behind sanitization of crop values --- packages/plugin-crop/src/index.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/plugin-crop/src/index.js b/packages/plugin-crop/src/index.js index 4a2b65ed8..8a0475ab0 100644 --- a/packages/plugin-crop/src/index.js +++ b/packages/plugin-crop/src/index.js @@ -221,18 +221,18 @@ export default function pluginCrop(event) { southPixelsToCrop = vertical; } - // safety checks - const widthOfRemainingPixels = - w - (westPixelsToCrop + eastPixelsToCrop); - const heightOfRemainingPixels = - h - (southPixelsToCrop + northPixelsToCrop); - // make sure that crops are > 0 westPixelsToCrop = westPixelsToCrop >= 0 ? westPixelsToCrop : 0; eastPixelsToCrop = eastPixelsToCrop >= 0 ? eastPixelsToCrop : 0; northPixelsToCrop = northPixelsToCrop >= 0 ? northPixelsToCrop : 0; southPixelsToCrop = southPixelsToCrop >= 0 ? southPixelsToCrop : 0; + // safety checks + const widthOfRemainingPixels = + w - (westPixelsToCrop + eastPixelsToCrop); + const heightOfRemainingPixels = + h - (southPixelsToCrop + northPixelsToCrop); + if (cropOnlyFrames) { // crop image if all sides should be cropped doCrop = From 0b0e87e78331e2f3ea0176e7bce62fbf1e1425f7 Mon Sep 17 00:00:00 2001 From: jagaSto Date: Thu, 23 May 2019 09:51:17 +0200 Subject: [PATCH 2/5] add test case with leaveBorder larger than pixeslToCrop --- packages/plugin-crop/test/autocrop.test.js | 36 +++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/plugin-crop/test/autocrop.test.js b/packages/plugin-crop/test/autocrop.test.js index f4370d1ad..3b73c1dba 100644 --- a/packages/plugin-crop/test/autocrop.test.js +++ b/packages/plugin-crop/test/autocrop.test.js @@ -250,7 +250,7 @@ describe('Autocrop', () => { ); }); - it('image without frame and with with some border left', async () => { + it('image without frame and with some border left', async () => { const imgSrc = await Jimp.read( mkJGD( '323232323232', @@ -284,6 +284,40 @@ describe('Autocrop', () => { ); }); + it('image without frame and with some border left', async () => { + const imgSrc = await Jimp.read( + mkJGD( + '323232323232', + '232323232323', + '32 ◆◆ 32', + '23 ◆▦▦◆ 23', + '32 ◆▦▦▦▦◆ 32', + '23 ◆▦▦◆ 23', + '32 ◆◆ 32', + '232323232323', + '323232323232' + ) + ); + + imgSrc + .autocrop({ + tolerance: 0.005, + leaveBorder: 5 + }) + .getJGDSync() + .should.be.sameJGD( + mkJGD( + '3232323232', + '2 ◆◆ 3', + '3 ◆▦▦◆ 2', + '2 ◆▦▦▦▦◆ 3', + '3 ◆▦▦◆ 2', + '2 ◆◆ 3', + '3232323232' + ) + ); + }); + it('image with top and bottom frame and leaveBorder', async () => { const imgSrc = await Jimp.read( mkJGD( From 110e9be8f52902fd756abb0e68c3ab32789d2395 Mon Sep 17 00:00:00 2001 From: jagaSto Date: Tue, 27 Aug 2019 13:49:56 +0200 Subject: [PATCH 3/5] formatting --- packages/plugin-crop/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugin-crop/src/index.js b/packages/plugin-crop/src/index.js index fc72011d3..c15eca283 100644 --- a/packages/plugin-crop/src/index.js +++ b/packages/plugin-crop/src/index.js @@ -221,7 +221,7 @@ export default function pluginCrop(event) { southPixelsToCrop = vertical; } - // make sure that crops are > 0 + // make sure that crops are >= 0 westPixelsToCrop = westPixelsToCrop >= 0 ? westPixelsToCrop : 0; eastPixelsToCrop = eastPixelsToCrop >= 0 ? eastPixelsToCrop : 0; northPixelsToCrop = northPixelsToCrop >= 0 ? northPixelsToCrop : 0; From e686b0919a43f2d82e1282225630ded624d0311a Mon Sep 17 00:00:00 2001 From: jagaSto Date: Tue, 27 Aug 2019 13:50:16 +0200 Subject: [PATCH 4/5] fix test case for out of bounds "leaveBorder" value --- packages/plugin-crop/test/autocrop.test.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/plugin-crop/test/autocrop.test.js b/packages/plugin-crop/test/autocrop.test.js index 3b73c1dba..234042c57 100644 --- a/packages/plugin-crop/test/autocrop.test.js +++ b/packages/plugin-crop/test/autocrop.test.js @@ -284,7 +284,7 @@ describe('Autocrop', () => { ); }); - it('image without frame and with some border left', async () => { + it('should NOT crop an image given an out of bounds "leaveBorder" value ', async () => { const imgSrc = await Jimp.read( mkJGD( '323232323232', @@ -307,13 +307,15 @@ describe('Autocrop', () => { .getJGDSync() .should.be.sameJGD( mkJGD( - '3232323232', - '2 ◆◆ 3', - '3 ◆▦▦◆ 2', - '2 ◆▦▦▦▦◆ 3', - '3 ◆▦▦◆ 2', - '2 ◆◆ 3', - '3232323232' + '323232323232', + '232323232323', + '32 ◆◆ 32', + '23 ◆▦▦◆ 23', + '32 ◆▦▦▦▦◆ 32', + '23 ◆▦▦◆ 23', + '32 ◆◆ 32', + '232323232323', + '323232323232' ) ); }); From 6e38612cea1741f8edc326127990381f2a4dc470 Mon Sep 17 00:00:00 2001 From: jagaSto Date: Tue, 27 Aug 2019 13:58:00 +0200 Subject: [PATCH 5/5] align test clause naming --- packages/plugin-crop/test/autocrop.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/plugin-crop/test/autocrop.test.js b/packages/plugin-crop/test/autocrop.test.js index 234042c57..78dac64bf 100644 --- a/packages/plugin-crop/test/autocrop.test.js +++ b/packages/plugin-crop/test/autocrop.test.js @@ -284,7 +284,7 @@ describe('Autocrop', () => { ); }); - it('should NOT crop an image given an out of bounds "leaveBorder" value ', async () => { + it('image not cropped given an out of bounds "leaveBorder" value ', async () => { const imgSrc = await Jimp.read( mkJGD( '323232323232', @@ -302,7 +302,7 @@ describe('Autocrop', () => { imgSrc .autocrop({ tolerance: 0.005, - leaveBorder: 5 + leaveBorder: 100 }) .getJGDSync() .should.be.sameJGD(