Base 64图像尺寸增加得超过实际尺寸

时间:2019-05-20 05:24:32

标签: javascript amazon-s3 base64

我正在获取有关图像的Base 64字符串,获取数据后,我将字符串替换为base 64,我的代码就是这个

const buf = Buffer.from(base64String.replace(/^data:image\/\w+;base64,/, ''),
'base64');
const imagedata = {
    Key: `${customname}.${imageextension}`,
        Body: buf,
        ContentEncoding: 'base64',
    ContentType: 'image/jpeg',
};

此后,我将图像上传到AWS S3,我注意到,当我上传图像时,其实际大小为400 kb,但是在AWS S3上上传后,相同的图像大小约为2MB。

我知道base 64将增加30%到40%的大小,但就我而言,它太重了, 有人建议我如何解决这个问题,

我在使用Nodejs,非常感谢您的帮助。

问题已更新 图像扩展完全取决于用户。

问题已更新。 我创建了一个函数,该函数以kb为单位返回基数64的大小。

const calculateImageSize = (base64String) =>{
    let padding, inBytes, base64StringLength;
    if (base64String.endsWith("==")) padding = 2;
    else if (base64String.endsWith("=")) padding = 1;
    else padding = 0;

    base64StringLength = base64String.length;
    console.log(base64StringLength)
    inBytes = (base64StringLength / 4) * 3 - padding;
    console.log(inBytes);
    this.kbytes = inBytes / 1000;
    console.log(inBytes / 1000 + "KB");
    return this.kbytes;
}

之后,我上传了图像(1.43kb),在Nodejs上将字符串传递给函数以获取kb大小。

calculateImageSize(req.body.image)return 41.3 kb

意味着我必须在上载时压缩大小。 任何帮助再次表示感谢 cr

0 个答案:

没有答案
相关问题