通过Express API发送PNG图片

时间:2018-11-20 15:23:22

标签: node.js express png

我有点困惑为什么我需要将代表PNG图像的二进制数据编码为base64字符串,以便可以通过REST API发送/显示它?

这是我的有效代码:

import png from 'fast-png';

api.get('/img', (req, res) => {
    ...
    let pngImage = png.encode({
            data: pixelData,
            width: columns,
            height: rows,
            alpha: false,
            components: samplesPerPixel
        });

    let base64 = Buffer.from(pngImage, 'binary').toString('base64');
    result = new Buffer(base64, 'base64');

    res.contentType('image/png');
    res.send(result);
}

无效的代码:

import png from 'fast-png';

api.get('/img', (req, res) => {
    ...
    let pngImage = png.encode({
            data: pixelData,
            width: columns,
            height: rows,
            alpha: false,
            components: samplesPerPixel
        });

    res.contentType('image/png');
    res.send(pngImage.buffer, 'binary');
}

如果有人可以解释为什么在这种情况下不起作用?

谢谢

0 个答案:

没有答案