Comparing version 0.3.0 to 0.3.1
{ | ||
"name": "impro", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Image processing engine", | ||
@@ -5,0 +5,0 @@ "author": "Andreas Lind <andreaslindpetersen@gmail.com>", |
@@ -6,4 +6,96 @@ const requireOr = require('require-or'); | ||
const errors = require('../errors'); | ||
const _ = require('lodash'); | ||
function createGmOperations(pipeline, operations) { | ||
var gmOperations = []; | ||
var resize; | ||
var crop; | ||
var withoutEnlargement; | ||
var ignoreAspectRatio; | ||
for (const requestedOperation of operations) { | ||
const operation = Object.assign({}, requestedOperation); | ||
if (operation.name === 'resize') { | ||
resize = operation; | ||
} else if (operation.name === 'crop') { | ||
crop = operation; | ||
} else if (operation.name === 'withoutEnlargement') { | ||
withoutEnlargement = operation; | ||
continue; | ||
} else if (operation.name === 'ignoreAspectRatio') { | ||
ignoreAspectRatio = operation; | ||
continue; | ||
} | ||
if (operation.name === 'rotate' && operation.args.length === 1) { | ||
operation.args = ['transparent', operation.args[0]]; | ||
} else if (operation.name === 'resize') { | ||
const args = operation.args; | ||
if ( | ||
typeof pipeline.options.maxOutputPixels === 'number' && | ||
args[0] * args[1] > pipeline.options.maxOutputPixels | ||
) { | ||
throw new errors.OutputDimensionsExceeded( | ||
'resize: Target dimensions of ' + | ||
args[0] + | ||
'x' + | ||
args[1] + | ||
' exceed maxOutputPixels (' + | ||
pipeline.options.maxOutputPixels + | ||
')' | ||
); | ||
} | ||
} else if (operation.name === 'extract') { | ||
operation.name = 'crop'; | ||
operation.args = [ | ||
operation.args[2], | ||
operation.args[3], | ||
operation.args[0], | ||
operation.args[1] | ||
]; | ||
} else if (operation.name === 'crop') { | ||
operation.name = 'gravity'; | ||
operation.args = [ | ||
{ | ||
northwest: 'NorthWest', | ||
north: 'North', | ||
northeast: 'NorthEast', | ||
west: 'West', | ||
center: 'Center', | ||
east: 'East', | ||
southwest: 'SouthWest', | ||
south: 'South', | ||
southeast: 'SouthEast' | ||
}[String(operation.args[0]).toLowerCase()] || 'Center' | ||
]; | ||
} | ||
if (operation.name === 'progressive') { | ||
operation.name = 'interlace'; | ||
operation.args = ['line']; | ||
} | ||
// There are many, many more that could be supported: | ||
if (module.exports.outputTypes.includes(operation.name)) { | ||
operation.args.unshift(operation.name); | ||
operation.name = 'setFormat'; | ||
} | ||
gmOperations.push(operation); | ||
} | ||
if (withoutEnlargement && resize) { | ||
resize.args[2] = '>'; | ||
} | ||
if (ignoreAspectRatio && resize) { | ||
resize.args[2] = '!'; | ||
} | ||
if (resize && crop) { | ||
gmOperations.push({ | ||
name: 'extent', | ||
args: [].concat(resize.args) | ||
}); | ||
resize.args[2] = '^'; | ||
} | ||
return gmOperations; | ||
} | ||
function getMockFileNameForContentType(contentType) { | ||
@@ -24,3 +116,3 @@ if (contentType) { | ||
unavailable: !gm, | ||
operations: ['gif', 'png', 'jpeg', 'extract'].concat( | ||
operations: ['extract', 'withoutEnlargement', 'ignoreAspectRatio'].concat( | ||
Object.keys(gm.prototype).filter(function(propertyName) { | ||
@@ -78,5 +170,6 @@ return ( | ||
execute: function(pipeline, operations) { | ||
var gmOperations = createGmOperations(pipeline, operations); | ||
// For some reason the gm module doesn't expose itself as a readable/writable stream, | ||
// so we need to wrap it into one: | ||
var readStream = new Stream(); | ||
@@ -96,3 +189,3 @@ readStream.readable = true; | ||
getMockFileNameForContentType( | ||
operations[0].sourceContentType | ||
gmOperations[0].sourceContentType | ||
) | ||
@@ -103,96 +196,5 @@ ); | ||
} | ||
var resize; | ||
var crop; | ||
var withoutEnlargement; | ||
var ignoreAspectRatio; | ||
for (var i = 0; i < operations.length; i += 1) { | ||
var operation = operations[i]; | ||
if (operation.name === 'resize') { | ||
resize = operation; | ||
} else if (operation.name === 'crop') { | ||
crop = operation; | ||
} else if (operation.name === 'withoutEnlargement') { | ||
withoutEnlargement = operation; | ||
} else if (operation.name === 'ignoreAspectRatio') { | ||
ignoreAspectRatio = operation; | ||
} | ||
} | ||
if (withoutEnlargement && resize) { | ||
resize.args[1] += '>'; | ||
} | ||
if (ignoreAspectRatio && resize) { | ||
resize.args[1] += '!'; | ||
} | ||
if (resize && crop) { | ||
operations.push({ | ||
name: 'extent', | ||
args: [].concat(resize.args) | ||
}); | ||
resize.args.push('^'); | ||
} | ||
operations | ||
gmOperations | ||
.reduce(function(gmInstance, operation) { | ||
var args = operation.args; | ||
if ( | ||
operation.name === 'resize' && | ||
typeof pipeline.options.maxOutputPixels === | ||
'number' && | ||
args[0] * args[1] > pipeline.options.maxOutputPixels | ||
) { | ||
throw new errors.OutputDimensionsExceeded( | ||
'resize: Target dimensions of ' + | ||
args[0] + | ||
'x' + | ||
args[1] + | ||
' exceed maxOutputPixels (' + | ||
pipeline.options.maxOutputPixels + | ||
')' | ||
); | ||
} | ||
if ( | ||
operation.name === 'rotate' && | ||
operation.args.length === 1 | ||
) { | ||
operation = _.extend({}, operation); | ||
operation.args = ['transparent', operation.args[0]]; | ||
} | ||
if (operation.name === 'extract') { | ||
operation.name = 'crop'; | ||
operation.args = [ | ||
operation.args[2], | ||
operation.args[3], | ||
operation.args[0], | ||
operation.args[1] | ||
]; | ||
} else if (operation.name === 'crop') { | ||
operation.name = 'gravity'; | ||
operation.args = [ | ||
{ | ||
northwest: 'NorthWest', | ||
north: 'North', | ||
northeast: 'NorthEast', | ||
west: 'West', | ||
center: 'Center', | ||
east: 'East', | ||
southwest: 'SouthWest', | ||
south: 'South', | ||
southeast: 'SouthEast' | ||
}[String(operation.args[0]).toLowerCase()] || | ||
'Center' | ||
]; | ||
} | ||
if (operation.name === 'progressive') { | ||
operation.name = 'interlace'; | ||
operation.args = ['line']; | ||
} | ||
// There are many, many more that could be supported: | ||
if ( | ||
module.exports.outputTypes.includes(operation.name) | ||
) { | ||
operation = _.extend({}, operation); | ||
operation.args.unshift(operation.name); | ||
operation.name = 'setFormat'; | ||
} | ||
if (typeof gmInstance[operation.name] === 'function') { | ||
@@ -243,3 +245,5 @@ return gmInstance[operation.name].apply( | ||
pipeline._attach(readWriteStream); | ||
return gmOperations; | ||
} | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58047
1455