在 node.js 中使用 GM 裁剪成带圆圈的图像
Use GM in node.js to crop to a circled image
我已经使用 express 4 创建了一个上传图片功能,我想在此过程中为上传的图片创建几个不同的尺寸和形状。
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);
});
}
现在,我希望将这张 35x35 的图像裁剪成透明背景的圆圈。像这样:
我发现了这个类似的问题:Rounded corner using gm in nodejs。但答案使用命令行 ImageMagick,我想使用
gm 方法和能力。有人知道怎么解决吗?
过了一段时间,我决定迁移到伟大的node-easyimage。它给了我更大的灵活性,并允许我利用成功和错误响应的回调来重现我的命令行。
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 插件确实已弃用
我已经使用 express 4 创建了一个上传图片功能,我想在此过程中为上传的图片创建几个不同的尺寸和形状。
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);
});
}
现在,我希望将这张 35x35 的图像裁剪成透明背景的圆圈。像这样:
我发现了这个类似的问题:Rounded corner using gm in nodejs。但答案使用命令行 ImageMagick,我想使用 gm 方法和能力。有人知道怎么解决吗?
过了一段时间,我决定迁移到伟大的node-easyimage。它给了我更大的灵活性,并允许我利用成功和错误响应的回调来重现我的命令行。
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 插件确实已弃用