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

标签 node.js imagemagick

我在我的 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/

相关文章:

node.js - 卡夫卡错误: Request for offset X but we only have log segments in the range Y to Z

php - 从ajax调用输出图像的最快方法

从图像创建数组

node.js - DynamoDB 扫描,ExpressionAttributeValues 意外键 NodeJS 中的错误

javascript - 使用 JSON Web token 和 node.js/express 保护 URL 的正确方法

node.js - 客户端未收到多播消息

ruby - RMagick 在使用 RVG 创建新图像时忽略不透明度

image-processing - 如何使用 ImageMagick 或 RMagick 将图像转换为 1bit/px 二进制位图?

ruby-on-rails - Minimagick 错误 : ImageMagick/GraphicsMagick is not installed

Node.js PassThrough 流没有正确关闭?