Skip to content

Commit

Permalink
Add support for withoutReduction operation.
Browse files Browse the repository at this point in the history
Do this both for the sharp engine as of the support for it introduced
in 0.30.0 as well as emulation glue to gm which uses resize with '<'.
  • Loading branch information
alexjeffburke committed Mar 15, 2022
1 parent 728a44c commit a48b366
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/engines/gm.js
Expand Up @@ -10,6 +10,7 @@ function createGmOperations(pipeline, operations) {
let resize;
let crop;
let withoutEnlargement;
let withoutReduction;
let ignoreAspectRatio;

for (const requestedOperation of operations) {
Expand All @@ -22,6 +23,9 @@ function createGmOperations(pipeline, operations) {
} else if (operation.name === 'withoutEnlargement') {
withoutEnlargement = operation;
continue;
} else if (operation.name === 'withoutReduction') {
withoutReduction = operation;
continue;
} else if (operation.name === 'ignoreAspectRatio') {
ignoreAspectRatio = operation;
continue;
Expand Down Expand Up @@ -92,6 +96,9 @@ function createGmOperations(pipeline, operations) {
if (withoutEnlargement && resize) {
resize.args[2] = '>';
}
if (withoutReduction && resize) {
resize.args[2] = '<';
}
if (ignoreAspectRatio && resize) {
resize.args[2] = '!';
}
Expand Down Expand Up @@ -121,6 +128,7 @@ module.exports = {
'extract',
'progressive',
'withoutEnlargement',
'withoutReduction',
'ignoreAspectRatio',
].concat(
Object.keys(gm.prototype).filter(
Expand All @@ -136,6 +144,7 @@ module.exports = {
switch (name) {
// Operations that emulate sharp's API:
case 'withoutEnlargement':
case 'withoutReduction':
case 'ignoreAspectRatio':
case 'progressive':
return args.length === 0;
Expand Down
2 changes: 2 additions & 0 deletions src/engines/sharp.js
Expand Up @@ -28,6 +28,7 @@ const optionsToOutputType = {
};
const optionsToResize = {
withoutEnlargement: () => ({ withoutEnlargement: true }),
withoutReduction: () => ({ withoutReduction: true }),
ignoreAspectRatio: () => ({ fit: 'fill' }),
};
const variationsToResize = {
Expand All @@ -53,6 +54,7 @@ module.exports = {
'flip',
'flop',
'withoutEnlargement',
'withoutReduction',
'ignoreAspectRatio',
'blur',
'sharpen',
Expand Down
49 changes: 49 additions & 0 deletions test/impro.spec.js
Expand Up @@ -949,6 +949,29 @@ describe('impro', () => {
]);
});

it('should throw on withoutReduction without resize', () =>
expect(
() => {
impro.withoutReduction().flush();
},
'to throw',
'sharp: withoutReduction() operation must follow resize'
));

it('should support withoutReduction', () => {
return expect(
'turtle.jpg',
'when piped through',
impro.resize(10, 10).withoutReduction(),
'to yield output satisfying',
'to have metadata satisfying',
{
format: 'JPEG',
size: { width: 481, height: 424 }, // unchanged
}
);
});

it('should throw on ignoreAspectRatio without resize', () =>
expect(
() => {
Expand Down Expand Up @@ -1258,6 +1281,16 @@ describe('impro', () => {
]);
});

it('should support withoutReduction', () => {
const executeSpy = sinon.spy(impro.engineByName.gm, 'execute');

impro.gm().resize(10, 10).withoutReduction().flush();

return expect(executeSpy.returnValues[0], 'to equal', [
{ name: 'resize', args: [10, 10, '<'] },
]);
});

it('should support ignoreAspectRatio', () => {
const executeSpy = sinon.spy(impro.engineByName.gm, 'execute');

Expand Down Expand Up @@ -1293,6 +1326,22 @@ describe('impro', () => {
});
});

describe('with executed conversions', () => {
it('should support withoutReduction', () => {
return expect(
'turtle.jpg',
'when piped through',
impro.gm().resize(10, 10).withoutReduction(),
'to yield output satisfying',
'to have metadata satisfying',
{
format: 'JPEG',
size: { width: 481, height: 424 }, // unchanged
}
);
});
});

describe('with a maxOutputPixels setting in place', () => {
it('should support resize (only width)', () => {
const executeSpy = sinon.spy(impro.engineByName.gm, 'execute');
Expand Down

0 comments on commit a48b366

Please sign in to comment.