gpt4 book ai didi

node.js - 将数据 URI 保存为图像?

转载 作者:搜寻专家 更新时间:2023-10-31 23:01:45 25 4
gpt4 key购买 nike

在 Node 服务器上,我想将上传的 datauri 数据保存为图像。为此,我尝试解码此 png 的内容-

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAACCAIAAAFlEcHbAAAAB3RJTUUH1gMWFjk7nUWcXQAAAAlwSFlzAABOIAAATiABFn2Z3gAAAARnQU1BAACxjwv8YQUAAAAeSURBVHjaY7h79y7DhAkTGIA04/Tp0xkYGJ49ewYAgYwLV/R7bDQAAAAASUVORK5CYII=

并将其保存为 .png 扩展名。看起来还有更多。如何解码 datauri 并将其保存为文件?

最佳答案

我创建了一个与 Node.js 一起使用的库,它有助于对数据 URI 方案进行编码和解码。相信可以帮到你,查看:

https://github.com/DiegoZoracKy/image-data-uri

使用这个库,在你的情况下,代码将是:

'use strict';

const ImageDataURI = require('image-data-uri');

const dataURI = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAACCAIAAAFlEcHbAAAAB3RJTUUH1gMWFjk7nUWcXQAAAAlwSFlzAABOIAAATiABFn2Z3gAAAARnQU1BAACxjwv8YQUAAAAeSURBVHjaY7h79y7DhAkTGIA04/Tp0xkYGJ49ewYAgYwLV/R7bDQAAAAASUVORK5CYII=';
const fileName = 'decoded-image.png';

ImageDataURI.outputFile(dataURI, filePath);

关于node.js - 将数据 URI 保存为图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36585018/

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