Skip to content

Commit

Permalink
Use gm when working with animated gifs, except when eventually conver…
Browse files Browse the repository at this point in the history
…ting to another format.
  • Loading branch information
papandreou committed Dec 1, 2015
1 parent 017f74c commit 36c37bf
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/getFilterInfosAndTargetContentTypeFromQueryString.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,15 @@ module.exports = function getFilterInfosAndTargetContentTypeFromQueryString(quer
currentEngineName = undefined;
}

queryString.split('&').forEach(function (keyValuePair) {
var keyValuePairs = queryString.split('&');

if (sourceMetadata.contentType === 'image/gif' && !keyValuePairs.some(function (keyValuePair) {
return keyValuePair === 'png' || keyValuePair === 'webp' || keyValuePair === 'jpeg';
})) {
currentEngineName = 'gm';
}

keyValuePairs.forEach(function (keyValuePair) {
var matchKeyValuePair = keyValuePair.match(/^([^=]+)(?:=(.*))?/);
if (matchKeyValuePair) {
var operationName = decodeURIComponent(matchKeyValuePair[1]),
Expand Down
4 changes: 4 additions & 0 deletions lib/processImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ module.exports = function (options) {
if (targetContentType) {
res.setHeader('Content-Type', targetContentType);
}
if (options.debug) {
// Only used by the test suite to assert that the right engine is used to process gifs:
res.setHeader('X-Express-Processimage', filterInfosAndTargetFormat.operationNames.join(','));
}
res.removeHeader('Content-Length');
var oldETag = res.getHeader('ETag'),
newETag;
Expand Down
57 changes: 57 additions & 0 deletions test/processImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,5 +417,62 @@ describe('express-processimage', function () {
errorPassedToNext: /bad extract area/
});
});

it('should convert an animated gif to png', function () {
return expect('GET /animated.gif?png', 'to yield response', {
body: expect.it('to have metadata satisfying', {
format: 'PNG',
size: {
width: 23,
height: 20
}
})
});
});

it('should resize an animated gif using gm', function () {
config.debug = true;
return expect('GET /animated.gif?resize=40', 'to yield response', {
headers: {
'X-Express-Processimage': 'gm'
},
body: expect.it('to have metadata satisfying', {
format: 'GIF',
size: {
width: 40
}
})
});
});

it('should resize a non-animated gif using gm', function () {
config.debug = true;
return expect('GET /bulb.gif?resize=200,200', 'to yield response', {
headers: {
'X-Express-Processimage': 'gm'
},
body: expect.it('to have metadata satisfying', {
format: 'GIF',
size: {
width: 200
}
})
});
});

it('should use sharp when a gif is converted to png', function () {
config.debug = true;
return expect('GET /animated.gif?resize=40&png', 'to yield response', {
headers: {
'X-Express-Processimage': 'sharp'
},
body: expect.it('to have metadata satisfying', {
format: 'PNG',
size: {
width: 40
}
})
});
});
});
});
Binary file added testdata/animated.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/bulb.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 36c37bf

Please sign in to comment.