From b0bd2bfdfd68c11625c54868c0f44fbf06af57aa Mon Sep 17 00:00:00 2001 From: Redsandro Date: Tue, 17 Dec 2013 21:54:36 +0100 Subject: [PATCH 1/2] Preserve transparency when converting SVG Added a background option that defaults to 'none' --- imagemagick.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/imagemagick.js b/imagemagick.js index b846c0c..f5366b7 100644 --- a/imagemagick.js +++ b/imagemagick.js @@ -1,5 +1,8 @@ var childproc = require('child_process'), - EventEmitter = require('events').EventEmitter; + EventEmitter = require('events').EventEmitter, + util = require('util'); + +var debug = true; // Show command used for convert function exec2(file, args /*, options, callback */) { @@ -350,7 +353,8 @@ exports.resizeArgs = function(options) { filter: 'Lagrange', sharpening: 0.2, customArgs: [], - timeout: 0 + timeout: 0, + background: 'none' } // check options @@ -371,7 +375,12 @@ exports.resizeArgs = function(options) { throw new Error('both width and height can not be 0 (zero)'); // build args - var args = [opt.srcPath]; + var args = []; + if (opt.background) { // Needs to be added _before_ input file + args.push('-background'); + args.push(opt.background); + } + args.push(opt.srcPath); if (opt.sharpening > 0) { args = args.concat([ '-set', 'option:filter:blur', String(1.0-opt.sharpening)]); @@ -410,6 +419,9 @@ exports.resizeArgs = function(options) { if (Array.isArray(opt.customArgs) && opt.customArgs.length) args = args.concat(opt.customArgs); args.push(opt.dstPath); + + if (debug) + util.log('convert ' + args.join(' ')); return {opt:opt, args:args}; } From 2d18d9d7db7a1d46d88259bae5a9de3f8cee9761 Mon Sep 17 00:00:00 2001 From: Redsandro Date: Fri, 20 Dec 2013 01:16:26 +0100 Subject: [PATCH 2/2] Added option extent to allow resizing plus extending the canvas to fit the resolution When resizing a 128x256 image to 64x64, you normally get a 32x64 image. Using `extent: true` you will get a 64x64 image while preserving aspect, filling the rest with `background` (`none` by default). --- imagemagick.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/imagemagick.js b/imagemagick.js index f5366b7..ecbc0ee 100644 --- a/imagemagick.js +++ b/imagemagick.js @@ -354,7 +354,8 @@ exports.resizeArgs = function(options) { sharpening: 0.2, customArgs: [], timeout: 0, - background: 'none' + background: 'none', + extent: false } // check options @@ -398,6 +399,12 @@ exports.resizeArgs = function(options) { else if (opt.width === 0) args.push('x'+String(opt.height)); else args.push(String(opt.width)+'x'+String(opt.height)); } + if (opt.extent && opt.width && opt.height) { + args.push('-gravity'); + args.push('center'); + args.push('-extent'); + args.push(String(opt.width)+'x'+String(opt.height)); + } opt.format = opt.format.toLowerCase(); var isJPEG = (opt.format === 'jpg' || opt.format === 'jpeg'); if (isJPEG && opt.progressive) {