gpt4 book ai didi

javascript - 在Nodejs中将base64 png转换为jpeg图像

转载 作者:太空宇宙 更新时间:2023-11-03 21:55:32 25 4
gpt4 key购买 nike

我在Nodejs中使用FabricJS模块。我试图将一个 Canvas 导出为 jpeg 但它不会(给我带来困难)。我得到的只是 base64 png 数据。

开头的数据
data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAfQAAAH0CAYAAADL1t.....

在nodejs中是否可以将此图像转换为jpeg?我用谷歌搜索了很多,但找不到解决方案

最佳答案

您可以使用png-to-jpeg模块。假设“数据”是字符串形式:

const fs = require("fs");

const pngToJpeg = require('png-to-jpeg');
const imgStr = 'data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAfQAAAH0CAYAAADL1t.....';

const buffer = new Buffer(imgStr.split(/,\s*/)[1],'base64');
pngToJpeg({quality: 90})(buffer).then(output => fs.writeFileSync("./some-file.jpeg", output));

关于javascript - 在Nodejs中将base64 png转换为jpeg图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42218785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com