0

I'm trying to get to grips with the basics of the sharp javascript library. But I'm failing dismally in my attempts to get it to read a base 64 string. My actual goal is to get this to work as part of a web service, but I've found that I can't get it to read base64 even locally, so for simplicity I'm trying to do that.

Here's the minimal program I've been working on:

const sharp = require('sharp');

const inputBuffer = Buffer.from(
    'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAABhGlDQ1BJQ0MgcHJvZmlsZQAAKJF9' +
    'kT1Iw0AcxV9TtaIVB4OIOGSoThZEizhqFYpQIdQKrTqYj35Bk4YkxcVRcC04+LFYdXBx1tXBVRAE'+
    'P0DcBSdFFynxf0mhRYwHx/14d+9x9w7g6mVFszomAE23zVQiLmSyq0LoFV3oxSB4xCTFMuZEMQnf'+
    '8XWPAFvvoizL/9yfo0/NWQoQEIhnFcO0iTeIpzdtg/E+Ma8UJZX4nHjcpAsSPzJd9viNccFljmXy'+
    'Zjo1T8wTC4U2lttYKZoacYw4omo65XMZj1XGW4y1clVp3pO9MJzTV5aZTnMECSxiCSIEyKiihDJs'+
    'RGnVSbGQov24j3/Y9YvkkslVgkKOBVSgQXL9YH/wu1srPzXpJYXjQOeL43yMAqFdoFFznO9jx2mc'+
    'AMFn4Epv+St1YOaT9FpLixwB/dvAxXVLk/eAyx1g6MmQTMmVgjS5fB54P6NvygIDt0DPmtdbcx+n'+
    'D0CaukreAAeHwFiBstd93t3d3tu/Z5r9/QCL53Kx9rcTpAAAAAlwSFlzAAAuIwAALiMBeKU/dgAA'+
    'AAd0SU1FB+kGCxYVLLgYx34AAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAA'+
    'R0lEQVQY03WQOQ7AQAwCB5T/f5kUObRkHRoXHEYoQMIIydfhB76tu0ICDt5wqaTJQ7eDpY2Hh0tT'+
    'l2/r4S/XChc3h4+zSMAJo6AbF2z7fuQAAAAASUVORK5CYII=')
const outputBuffer = sharp(inputBuffer)
    .resize(100, 100) // You can adjust the dimensions as needed
    .toFile('output.jpeg', (err, info) => {
console.log('err is ' + err + ' info is ' + info)})

I hardcoded the image data (obviously), but I believe that's the format needed. I got the base64 text from the commandline "base64" tool from this image:

Red X image

I'm entirely ready to believe that I have something wrong with the base64 encoding (or the preamble to it), but the error reported is Input buffer contains unsupported image format info is undefined. I think it wants me to provide "info" in some way to tell it what kind of image it is, but I'm at a loss. I'm finding the documentation very opaque (indeed, I'm wondering if I'm even looking at the right docs, since they seem to suggest using a constructor for Buffer which shows up as deprecated--that's why I went with Buffer.from, which doesn't seem to cause problems).

Following the suggestion of @JPS, I removed the "data:image/png;base64," parts, but this made no difference in the error message. The docs claim this tool can handle png, and I've tried with a jpg too, with the same results.

Can anyone point me in a good direction?

2
  • 1
    read a base 64 string., 'data:image/png;base64,iVBORw0.... - that's not a Base64 string but a Data URL which contains a Base64 string (starting after the ,). Remove the DataURL part data:image/png;base64, . Commented Jun 12 at 10:49
  • Thanks @jps, I've struggled to work out what's needed here and the variations of this concept; for URIs, image tags, and whatever else I havent come across yet! Unfortunately, this doesn't change the error message, it still asserts that it's an unsupported image format. Sharp docs claim it handles PNG and JPG, and I've tried both, but with the same result. Commented Jun 12 at 14:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.