[英]How to convert image type to 'jpg' using imagemagick in node.js
我在node.js應用程序中使用imagemagick。 我調整了圖像的大小,如下所示:
im.resize({
format: 'jpg',
srcPath: imagePath,
dstPath: thumbPath,
width: 220,
}, function(err, stdout, stderr){
//some code
}
});
我希望我的代碼將傳入的圖像轉換為jpg
。 我該如何實現?
對於此解決方案,您需要使用easyimage軟件包 。 該程序包在以下運行,並且需要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。
您需要writeFileSync
並通過匿名函數通過管道傳遞數據。
im.resize({
format: 'jpg',
srcPath: imagePath,
dstPath: thumbPath,
width: 220,
}, function(err, stdout, stderr){
fs.writeFileSync('[filename].jpg', stdout,'binary'); //write the file here
}
});
我相信您還需要調用convert方法。
im.convert(['originalfilename.originalextension', 'jpg:-'],
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.