mirror of
https://github.com/lovell/sharp.git
synced 2026-02-04 13:46:19 +01:00
Add margin option to trim operation #4480
This commit is contained in:
committed by
Lovell Fuller
parent
d161e45e06
commit
a5e726002c
@@ -222,6 +222,9 @@ describe('Trim borders', () => {
|
||||
},
|
||||
'Invalid lineArt': {
|
||||
lineArt: 'fail'
|
||||
},
|
||||
'Invalid margin': {
|
||||
margin: -1
|
||||
}
|
||||
}).forEach(([description, parameter]) => {
|
||||
it(description, () => {
|
||||
@@ -289,4 +292,42 @@ describe('Trim borders', () => {
|
||||
assert.strictEqual(trimOffsetLeft, 0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Adds margin around content', () => {
|
||||
it('Should trim complex gradients', async () => {
|
||||
const { info } = await sharp(fixtures.inputPngGradients)
|
||||
.trim({ threshold: 50, margin: 100 })
|
||||
.toBuffer({ resolveWithObject: true });
|
||||
|
||||
const { width, height, trimOffsetTop, trimOffsetLeft } = info;
|
||||
assert.strictEqual(width, 1000);
|
||||
assert.strictEqual(height, 443);
|
||||
assert.strictEqual(trimOffsetTop, -557);
|
||||
assert.strictEqual(trimOffsetLeft, 0);
|
||||
});
|
||||
|
||||
it('Should trim simple gradients', async () => {
|
||||
const { info } = await sharp(fixtures.inputPngWithSlightGradientBorder)
|
||||
.trim({ threshold: 70, margin: 50 })
|
||||
.toBuffer({ resolveWithObject: true });
|
||||
|
||||
const { width, height, trimOffsetTop, trimOffsetLeft } = info;
|
||||
assert.strictEqual(width, 900);
|
||||
assert.strictEqual(height, 900);
|
||||
assert.strictEqual(trimOffsetTop, -50);
|
||||
assert.strictEqual(trimOffsetLeft, -50);
|
||||
});
|
||||
|
||||
it('Should not overflow image bounding box', async () => {
|
||||
const { info } = await sharp(fixtures.inputPngWithSlightGradientBorder)
|
||||
.trim({ threshold: 70, margin: 9999999 })
|
||||
.toBuffer({ resolveWithObject: true });
|
||||
|
||||
const { width, height, trimOffsetTop, trimOffsetLeft } = info;
|
||||
assert.strictEqual(width, 1000);
|
||||
assert.strictEqual(height, 1000);
|
||||
assert.strictEqual(trimOffsetTop, 0);
|
||||
assert.strictEqual(trimOffsetLeft, 0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user