Skip to content

Commit

Permalink
Support &withoutEnlargement with gm, be more resilient to unsupported…
Browse files Browse the repository at this point in the history
… operations popping up.
  • Loading branch information
papandreou committed Mar 14, 2016
1 parent 72559f2 commit 174a5f8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/getFilterInfosAndTargetContentTypeFromQueryString.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,20 @@ module.exports = function getFilterInfosAndTargetContentTypeFromQueryString(quer
}
var resize;
var crop;
var withoutEnlargement;
for (var i = 0 ; i < gmOperationsForThisInstance.length ; i += 1) {
var gmOperation = gmOperationsForThisInstance[i];
if (gmOperation.name === 'resize') {
resize = gmOperation;
} else if (gmOperation.name === 'crop') {
crop = gmOperation;
} else if (gmOperation.name === 'withoutEnlargement') {
withoutEnlargement = gmOperation;
}
}
if (withoutEnlargement && resize) {
resize.args[1] += '>';
}
if (resize && crop) {
gmOperationsForThisInstance.push({
name: 'extent',
Expand Down Expand Up @@ -213,12 +219,17 @@ module.exports = function getFilterInfosAndTargetContentTypeFromQueryString(quer
gmOperation.name = 'interlace';
gmOperation.args = [ 'line' ];
}
if (!gmInstance[gmOperation.name]) {
// There are many, many more that could be supported:
if (gmOperation.name === 'webp' || gmOperation.name === 'png' || gmOperation.name === 'jpeg' || gmOperation.name === 'gif') {
gmOperation = _.extend({}, gmOperation);
gmOperation.args.unshift(gmOperation.name);
gmOperation.name = 'setFormat';
}
return gmInstance[gmOperation.name].apply(gmInstance, gmOperation.args);
if (typeof gmInstance[gmOperation.name] === 'function') {
return gmInstance[gmOperation.name].apply(gmInstance, gmOperation.args);
} else {
return gmInstance;
}
}, gmInstance).stream(function (err, stdout, stderr) {
if (err) {
hasEnded = true;
Expand Down
14 changes: 14 additions & 0 deletions test/processImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,20 @@ describe('express-processimage', function () {
});
});

it('should support the withoutEnlargement modfier', function () {
return expect('GET /animated.gif?resize=40,35&withoutEnlargement', 'to yield response', {
headers: {
'X-Express-Processimage': gifsicleAvailable ? 'gifsicle' : 'gm'
},
body: expect.it('to have metadata satisfying', {
format: 'GIF',
// gifsicle does not enlarge to fill the bounding box
// https://github.com/kohler/gifsicle/issues/13#issuecomment-196321546
size: { width: 23, height: 20 }
})
});
});

it('should resize a non-animated gif', function () {
return expect('GET /bulb.gif?resize=200,200', 'to yield response', {
headers: {
Expand Down

0 comments on commit 174a5f8

Please sign in to comment.