diff --git a/packages/plugin-crop/src/index.js b/packages/plugin-crop/src/index.js index 6c824dbfc..c15eca283 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; } + // 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); - // 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; - if (cropOnlyFrames) { // crop image if all sides should be cropped doCrop = diff --git a/packages/plugin-crop/test/autocrop.test.js b/packages/plugin-crop/test/autocrop.test.js index 66dd65b8a..78dac64bf 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', @@ -283,4 +283,74 @@ describe('Autocrop', () => { ) ); }); + + it('image not cropped given an out of bounds "leaveBorder" value ', 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: 100 + }) + .getJGDSync() + .should.be.sameJGD( + mkJGD( + '323232323232', + '232323232323', + '32 ◆◆ 32', + '23 ◆▦▦◆ 23', + '32 ◆▦▦▦▦◆ 32', + '23 ◆▦▦◆ 23', + '32 ◆◆ 32', + '232323232323', + '323232323232' + ) + ); + }); + + it('image with top and bottom frame and leaveBorder', async () => { + const imgSrc = await Jimp.read( + mkJGD( + '▥▥▥▥▥▥▥▥', + '▥▥▥▥▥▥▥▥', + '▥▥▥▥▥▥▥▥', + ' ◆◆ ', + ' ◆▦▦◆ ', + ' ◆▦▦▦▦◆ ', + ' ◆▦▦◆ ', + ' ◆◆ ', + '▥▥▥▥▥▥▥▥', + '▥▥▥▥▥▥▥▥', + '▥▥▥▥▥▥▥▥' + ) + ); + imgSrc + .autocrop({ cropSymmetric: true, cropOnlyFrames: false, leaveBorder: 2 }) + .getJGDSync() + .should.be.sameJGD( + mkJGD( + '▥▥▥▥▥▥▥▥', + '▥▥▥▥▥▥▥▥', + ' ◆◆ ', + ' ◆▦▦◆ ', + ' ◆▦▦▦▦◆ ', + ' ◆▦▦◆ ', + ' ◆◆ ', + '▥▥▥▥▥▥▥▥', + '▥▥▥▥▥▥▥▥' + ) + ); + }); });