use*_*888 1 imagemagick node.js 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)
Run Code Online (Sandbox Code Playgroud)
我真的不会说话node,但它可能会类似于这样:
im.convert(['input.jpg', '-gravity', 'center', '-background','white', '-extent', '600x200','result.jpg'],
function(err, stdout){
if (err) throw err;
console.log('stdout:', stdout);
});
Run Code Online (Sandbox Code Playgroud)