I'm trying to downsample an image in node. I have that image stored as a base64 encoded string (ie: "data:image/png;base64,iVBOR" etc.). I'm using Sharp npm package. The documentation appears to delineate that sharp can take either the filepath to an image or an "inputBuffer." I did some googling and assume the Buffer class is what they are referring to. Trying the code below among others continuously has resulted in me receiving the following error: "Input buffer contains unsupported image format." What could my issue be, and if you're not sure could you please recommend me a different npm package with more clear documentation?
我正在尝试对节点中的图像进行下采样。我将该图像存储为base64编码的字符串(即:“data:image / png; base64,iVBOR”等)。我正在使用Sharp npm包。该文档似乎描述了锐利可以将文件路径带到图像或“inputBuffer”。我做了一些谷歌搜索,并假设Buffer类是他们所指的。连续尝试下面的代码导致我收到以下错误:“输入缓冲区包含不受支持的图像格式。”我的问题可能是什么,如果你不确定,请你推荐一个不同的npm软件包和更清晰的文档?
const downsizeProfileImgForTweet = (user, cb) => {
let imgBuffer = Buffer.from(user.profileImg, 'base64');
sharp(imgBuffer)
.resize(52, 52)
.toBuffer()
.then(data => {
console.log("success");
user.profileImg = data;
cb()
} )
.catch( err => console.log(`downisze issue ${err}`) );
}
I looked all over the internet and did a bunch of guess and checks, so forgive me for the noob question. Thanks in advance for any help you can offer!
我浏览了整个互联网,做了一堆猜测和检查,请原谅我的noob问题。预先感谢您提供的任何帮助!
2 个解决方案
#1
1
You need to remove the "data:image/png;base64" part.
您需要删除“data:image / png; base64”部分。
So here you are:
所以你在这里:
const uri = user.profileImg.split(';base64,').pop()
const downsizeProfileImgForTweet = (user, cb) => {
let imgBuffer = Buffer.from(uri, 'base64');
sharp(imgBuffer)
.resize(52, 52)
.toBuffer()
.then(data => {
console.log("success");
user.profileImg = data;
cb()
} )
.catch( err => console.log(`downisze issue ${err}`) );
}
Not sure if it will work with the ".toBuffer() method, though it works like this for me (to write the file):
不确定它是否适用于“.toBuffer()方法,虽然它对我来说是这样的(写入文件):
sharp(imgBuffer)
.resize(1920, null)
.toFile(`${config.images_path}/${picture.filename}`)
.then(data => {
console.log('normal: ', data)
})
.catch(err => console.log(`downisze issue ${err}`))
I was struggling with this not so long ago…
不久前我正在努力解决这个问题......
#2
0
Try this?
const buf = new Buffer(img.replace(/^data:image/\w+;base64,/, ""),'base64')
const buf = new Buffer(img.replace(/ ^ data:image / \ w +; base64,/,“”),'base64')
#1
1
You need to remove the "data:image/png;base64" part.
您需要删除“data:image / png; base64”部分。
So here you are:
所以你在这里:
const uri = user.profileImg.split(';base64,').pop()
const downsizeProfileImgForTweet = (user, cb) => {
let imgBuffer = Buffer.from(uri, 'base64');
sharp(imgBuffer)
.resize(52, 52)
.toBuffer()
.then(data => {
console.log("success");
user.profileImg = data;
cb()
} )
.catch( err => console.log(`downisze issue ${err}`) );
}
Not sure if it will work with the ".toBuffer() method, though it works like this for me (to write the file):
不确定它是否适用于“.toBuffer()方法,虽然它对我来说是这样的(写入文件):
sharp(imgBuffer)
.resize(1920, null)
.toFile(`${config.images_path}/${picture.filename}`)
.then(data => {
console.log('normal: ', data)
})
.catch(err => console.log(`downisze issue ${err}`))
I was struggling with this not so long ago…
不久前我正在努力解决这个问题......
#2
0
Try this?
const buf = new Buffer(img.replace(/^data:image/\w+;base64,/, ""),'base64')
const buf = new Buffer(img.replace(/ ^ data:image / \ w +; base64,/,“”),'base64')