如何在node.js中使用imagemagick将图像types转换为“jpg”
我在我的node.js应用程序中使用imagemagick。 我已经调整了图像的大小,如下所示:
im.resize({ format: 'jpg', srcPath: imagePath, dstPath: thumbPath, width: 220, }, function(err, stdout, stderr){ //some code } });
我想我的代码将传入的图像转换为jpg
。 我怎样才能做到这一点?
对于这个解决scheme,你需要使用easyimage包 。 这个包下运行,需要imagemagick(非常重要)。
$npm install easyimage --save // saved in your package.json
包括你的代码:
var easyimg = require('easyimage');
首先你需要得到原始图像的path:
tmp_path = 'path/to/image.svg';
现在你改变“tmp_path”的扩展名:
tmp_extless = tmp_path.replace('.svg','.png'); //be sure of include "."
所以,tmp_path是原始path,tmp_extless是我们未来映像的path。
现在,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 } } );
现在,您的新映像位于以下path中:tmp_extless。 用这种方法你不需要writeFile或readFile。
您需要writeFileSync
并通过匿名函数pipe理您的数据。
im.resize({ format: 'jpg', srcPath: imagePath, dstPath: thumbPath, width: 220, }, function(err, stdout, stderr){ fs.writeFileSync('[filename].jpg', stdout,'binary'); //write the file here } });
我相信你也需要调用转换方法。
im.convert(['originalfilename.originalextension', 'jpg:-'],