diff --git a/packages/jimp/README.md b/packages/jimp/README.md index 75db3199f..e3fb78dd7 100644 --- a/packages/jimp/README.md +++ b/packages/jimp/README.md @@ -128,7 +128,7 @@ image.scaleToFit( w, h[, mode] ); // scale the image to the largest size that fi /* Crop */ image.autocrop([tolerance, frames]); // automatically crop same-color borders from image (if any), frames must be a Boolean -image.autocrop(options); // automatically crop same-color borders from image (if any), options may contain tolerance, cropOnlyFrames, cropSymmetric +image.autocrop(options); // automatically crop same-color borders from image (if any), options may contain tolerance, cropOnlyFrames, cropSymmetric, leaveBorder image.crop( x, y, w, h ); // crop to the given region /* Composing */ diff --git a/packages/jimp/jimp.d.ts b/packages/jimp/jimp.d.ts index f4b6416a0..f91b0441e 100644 --- a/packages/jimp/jimp.d.ts +++ b/packages/jimp/jimp.d.ts @@ -432,7 +432,8 @@ declare namespace Jimp { options: { tolerance?: number; cropOnlyFrames?: boolean; - cropSymmetric: boolean; + cropSymmetric?: boolean; + leaveBorder?: number; }, cb?: Jimp.ImageCallback ): this; diff --git a/packages/plugin-crop/src/index.js b/packages/plugin-crop/src/index.js index 885dde77d..59c95f29a 100644 --- a/packages/plugin-crop/src/index.js +++ b/packages/plugin-crop/src/index.js @@ -68,6 +68,7 @@ export default function pluginCrop(event) { const minPixelsPerSide = 1; // to avoid cropping completely the image, resulting in an invalid 0 sized image let cb; // callback + let leaveBorder = 0; // Amount of pixels in border to leave let tolerance = 0.0002; // percent of color difference tolerance (default value) let cropOnlyFrames = true; // flag to force cropping only if the image has a real "frame" // i.e. all 4 sides have some border (default value) @@ -106,6 +107,10 @@ export default function pluginCrop(event) { if (typeof config.cropSymmetric !== 'undefined') { ({ cropSymmetric } = config); } + + if (typeof config.leaveBorder !== 'undefined') { + ({ leaveBorder } = config); + } } } @@ -135,6 +140,7 @@ export default function pluginCrop(event) { if (this.constructor.colorDiff(rgba1, rgba2) > tolerance) { // this pixel is too distant from the first one: abort this side scan + northPixelsToCrop -= leaveBorder; break north; } } @@ -151,6 +157,7 @@ export default function pluginCrop(event) { if (this.constructor.colorDiff(rgba1, rgba2) > tolerance) { // this pixel is too distant from the first one: abort this side scan + eastPixelsToCrop -= leaveBorder; break east; } } @@ -171,6 +178,7 @@ export default function pluginCrop(event) { if (this.constructor.colorDiff(rgba1, rgba2) > tolerance) { // this pixel is too distant from the first one: abort this side scan + southPixelsToCrop -= leaveBorder; break south; } } @@ -191,6 +199,7 @@ export default function pluginCrop(event) { if (this.constructor.colorDiff(rgba1, rgba2) > tolerance) { // this pixel is too distant from the first one: abort this side scan + westPixelsToCrop -= leaveBorder; break west; } } diff --git a/packages/plugin-crop/test/autocrop.test.js b/packages/plugin-crop/test/autocrop.test.js index 88b558510..66dd65b8a 100644 --- a/packages/plugin-crop/test/autocrop.test.js +++ b/packages/plugin-crop/test/autocrop.test.js @@ -222,7 +222,7 @@ describe('Autocrop', () => { ); }); - it('image wihtout frame and with symmetric border configured by options', async () => { + it('image without frame and with symmetric border configured by options', async () => { const imgSrc = await Jimp.read( mkJGD( '▥▥ ◆◆ ▥▥▥▥', @@ -249,4 +249,38 @@ describe('Autocrop', () => { ) ); }); + + it('image without frame and with 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: 1 + }) + .getJGDSync() + .should.be.sameJGD( + mkJGD( + '3232323232', + '2 ◆◆ 3', + '3 ◆▦▦◆ 2', + '2 ◆▦▦▦▦◆ 3', + '3 ◆▦▦◆ 2', + '2 ◆◆ 3', + '3232323232' + ) + ); + }); });