user1775888 user1775888 - 2 months ago 17
Node.js Question

How to use imagemagick add more white space around the image

How to use https://github.com/rsms/node-imagemagick add more white space around the image ?
example original image is

width:300, height:100
, I want to generate new image is
width: 600, height: 200
the original image is in center stay original size, add white space around to become new image..

I tried below code but this make the original image become 600*200 ... how to solve it?

function cropGravity(srcFilePath, dstFilePath, resizeWidth, resizeHeight, quality, gravity) {
return new Promise(function (resolve, reject){
im.crop({
srcPath: srcFilePath,
dstPath: dstFilePath,
width: resizeWidth,
height: resizeHeight,
quality: quality,
gravity: gravity
}, function(error, stdout, stderr){
if (error != null) {
console.log(error)
reject(error);
} else {
resolve(dstFilePath);
}
});
});
};

...
var resizeWidth = 600;
var resizeHeight = 200;
var quality = 1;
var gravity = 'Center';
cropGravity(srcFilePath, dstFilePath, resizeWidth, resizeHeight, quality, gravity)

Answer

I really don't speak node, but it will likely be similar to this:

im.convert(['input.jpg', '-gravity', 'center', '-background','white', '-extent', '600x200','result.jpg'], 
function(err, stdout){
  if (err) throw err;
  console.log('stdout:', stdout);
});