使用imagemagick

时间:2016-01-29 13:04:44

标签: meteor imagemagick graphicsmagick

我正在使用cfs:graphicsmagick包用于meteor,我想从base64字符串中读取图像。

Meteor.methods({
  "readImage"(imgSrc) {
    const imageMagick = gm.subClass({ imageMagick: true });

    imageMagick(imgSrc)
    .write("path/to/image.jpg", (err) => {
      if (err) console.log(err);
      else console.log("yay!")
    });
  }
});

但是当我尝试运行此代码时出现错误:

{ [Error: spawn ENAMETOOLONG] code: 'ENAMETOOLONG', errno: 'ENAMETOOLONG', syscall: 'spawn' }

我尝试通过new Buffer(string, [encoding])将字符串转换为缓冲区,但没有运气。

字符串类似于:data:image/png;base64,iVBORw0K...

有关如何使其有效的任何建议?

1 个答案:

答案 0 :(得分:2)

尝试将base64字符串不带类型定义(data:image / png; base64,)向上传递到逗号符号。 例如:iVBORw0K...

我正在使用此功能进行转换:

var fs = Npm.require('fs');
...
base64_decode: function(base64str, file) {
    var bitmap = new Buffer(base64str, 'base64');
    fs.writeFileSync(file, bitmap);
}

样本用法:

base64_decode('iVBORw0K...', '/path/to/file.png');