在node.js中使用GM来裁剪一个圆圈图像

我已经用express 4创build了一个上传图像function,并且我想在这个过程中为上传的图像创build几种不同的大小和形状。

mkdirp(smallPath, function (err) { if (err) { console.error('resizeImg err='+err+']'); return; } else { gm(basePath+'/original/'+image) .resize(35, 35) .noProfile() .write(smallOutputFilePath, function (err) { if (err) console.log(err); }); } 

现在,我希望这张35×35的图像在透明的背景下被圈起来。 喜欢这个: 在这里输入图像说明

我发现了这个类似的问题: 在nodejs中使用gm的圆angular 。 但答案使用命令行ImageMagick,我想使用gm的方法和function。 有谁知道如何解决?

过了一段时间,我决定迁移到伟大的节点easyimage 。 它给了我更多的灵活性,并允许我复制我的命令行利用callback的成功和错误响应的优势。

 function resizeImageToSize(path, size, outputTempFilePath, outputFilePath) { easyimg.exec('convert '+path+' -resize ' + (size) + 'x' + (size) + '^ -gravity center -crop ' + (size) + 'x' + (size) + '+0+0 +repage '+outputTempFilePath).then( function(file) { easyimg.exec('convert '+outputTempFilePath+' \\( -size ' + (size) + 'x' + (size) + ' xc:none -fill white -draw "circle ' + (size / 2) + ',' + (size / 2) + ' ' + (size / 2) + ',0" \\) -compose copy_opacity -composite '+ outputFilePath).then( function(file) { fs.unlink(outputTempFilePath, function (err) { if (err) { console.log(err); } }); }, function (err) { console.log(err); } ); }, function (err) { console.log(err); } );} 

imagemagick插件是真的弃用