【问题标题】:Read image from base64 string in Meteor using imagemagick使用 imagemagick 从 Meteor 中的 base64 字符串读取图像
【发布时间】:2016-01-29 13:04:44
【问题描述】:

我正在为流星使用 cfs:graphicsmagick 包,我想从 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...

关于如何使它工作的任何建议?

【问题讨论】:

    标签: meteor imagemagick graphicsmagick


    【解决方案1】:

    尝试传递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');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 2010-10-31
      • 2012-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多