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

Plugin crop/fix safety checks #743

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/plugin-crop/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
72 changes: 71 additions & 1 deletion packages/plugin-crop/test/autocrop.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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(
'▥▥▥▥▥▥▥▥',
'▥▥▥▥▥▥▥▥',
' ◆◆ ',
' ◆▦▦◆ ',
' ◆▦▦▦▦◆ ',
' ◆▦▦◆ ',
' ◆◆ ',
'▥▥▥▥▥▥▥▥',
'▥▥▥▥▥▥▥▥'
)
);
});
});