Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add leaveBorder to AutoCrop #399

Merged
merged 11 commits into from
Sep 5, 2018
2 changes: 1 addition & 1 deletion packages/jimp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
3 changes: 2 additions & 1 deletion packages/jimp/jimp.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,8 @@ declare namespace Jimp {
options: {
tolerance?: number;
cropOnlyFrames?: boolean;
cropSymmetric: boolean;
cropSymmetric?: boolean;
leaveBorder?: number;
},
cb?: Jimp.ImageCallback
): this;
Expand Down
9 changes: 9 additions & 0 deletions packages/plugin-crop/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -106,6 +107,10 @@ export default function pluginCrop(event) {
if (typeof config.cropSymmetric !== 'undefined') {
({ cropSymmetric } = config);
}

if (typeof config.leaveBorder !== 'undefined') {
({ leaveBorder } = config);
}
}
}

Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand Down
36 changes: 35 additions & 1 deletion packages/plugin-crop/test/autocrop.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'▥▥ ◆◆ ▥▥▥▥',
Expand All @@ -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'
)
);
});
});