我的开发环境是windows,所以就简单说下在windows下怎样使用
1.首先先安装imagemagick 下载地址:http://www.imagemagick.org/script/binary-releases.php#windows
2.node.js 上安装 imagemagick
npm install imagemagick --save
3.引用imagemagick类库
imageMagick = require("imagemagick")
4.设置imageMagick的路径
imageMagick.identify.path = "C:/Program Files/ImageMagick-6.8.8-Q16/identify";
imageMagick.convert.path = "C:/Program Files/ImageMagick-6.8.8-Q16/convert";
我的路径是安装
imageMagick的默认路径、如果不设置这个路径会提示错误:spawn ENOENT
注意这个路径后面必须加上文件名字(identify和convert),我开始以为就是单纯的路径没有加上总提示spawn ENOENT,比较肯爹!
5.使用imageMagick
imageMagick.resize({
srcPath: tmp_path,
dstPath: target_path,
width:256,
height:256
}, function(err, stdout, stderr){
if (err) {
console.log('error while resizing images' + stderr);
}else{
// 删除临时文件夹文件,
fs.unlink(tmp_path, function() {
if (err) throw err;
res.send(view_path);
});
}
});
注意 srcPath和dstPath的路径包括文件名,之后就可以随意使用imageMagick的函数,具体可参照imageMagick的api
地址:https://github.com/rsms/node-imagemagick
觉得有保住就留个言吧!