如何使用imagemagick在图像周围添加更多空白区域

时间:2016-09-30 12:31:53

标签: node.js imagemagick node-imagemagick

如何使用https://github.com/rsms/node-imagemagick在图片周围添加更多空白区域? 示例原始图像为width:300, height:100,我想生成新图像为width: 600, height: 200原始图像在中心保留原始大小,添加周围的空白区域成为新图像..

我尝试下面的代码,但这使得原始图像变成600 * 200 ......如何解决?

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)

1 个答案:

答案 0 :(得分:3)

我真的不会说node,但它可能与此类似:

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