我在我的 node.js 应用程序中使用 imagemagick。我已将图像调整如下:
im.resize({
format: 'jpg',
srcPath: imagePath,
dstPath: thumbPath,
width: 220,
}, function(err, stdout, stderr){
//some code
}
});
我希望我的代码将传入的图像转换为 jpg
。我怎样才能做到这一点?
最佳答案
对于此解决方案,您需要使用 easyimage package .这个包在 imagemagick 下运行并且需要 imagemagick(非常重要)。
$npm install easyimage --save // saved in your package.json
在您的代码中包括:
var easyimg = require('easyimage');
首先需要获取原图路径:
tmp_path = 'path/to/image.svg';
现在您更改“tmp_path”的扩展名:
tmp_extless = tmp_path.replace('.svg','.png'); //be sure of include "."
所以,tmp_path是原始路径,tmp_extless是我们以后镜像的路径。
现在,easyimage 的魔力:
easyimg.convert({src: tmp_path, dst: tmp_extless, quality: 80},
function(err,stdout){
if(stdout){ console.log('stdout', stdout);
//here you can run a script to delete tmp_path
}
}
);
现在,您的新图像位于路径中:tmp_extless。 使用此方法,您不需要 writeFile 或 readFile。
关于node.js - 如何在 node.js 中使用 imagemagick 将图像类型转换为 'jpg',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22302144/