如何将图像类型转换为';jpg';在node.js中使用imagemagick

如何将图像类型转换为';jpg';在node.js中使用imagemagick,node.js,imagemagick,Node.js,Imagemagick,我正在node.js应用程序中使用imagemagick。我已按如下方式调整图像的大小: im.resize({ format: 'jpg', srcPath: imagePath, dstPath: thumbPath, width: 220, }, function(err, stdout, stderr){ //some code } }); 我希望我的代码将传入的图像转换为jp

我正在node.js应用程序中使用imagemagick。我已按如下方式调整图像的大小:

im.resize({
        format: 'jpg',
        srcPath: imagePath,
        dstPath: thumbPath,
        width: 220,
        }, function(err, stdout, stderr){
        //some code
       }
});

我希望我的代码将传入的图像转换为
jpg
。如何实现这一点?

您需要
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:-'], 

对于此解决方案,您需要使用。这个软件包运行在imagemagick下,需要imagemagick(非常重要)

在代码中包括:

var easyimg = require('easyimage');
首先,您需要获得原始图像的路径:

tmp_path = 'path/to/image.svg';
现在更改“tmp_路径”的扩展:

因此,tmp_路径是原始路径,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。

.convert()可以工作。显然不需要writeFileSync()(尽管我当然想知道它的确切用法)。谢谢你的帮助。
tmp_extless = tmp_path.replace('.svg','.png'); //be sure of include "."
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
      }
    }
);