Sharp Input file contains unsupported image format when calling .toBuffer()
Asked Answered
C

0

6

When calling the .toBuffer() method on a sharp object, I get the following error:

Input buffer contains unsupported image format

I'm fetching an image from S3, and I am certain its retrieved as a Buffer which is then passed to sharp.

Moreover, when I console.log the base64 of the buffer and check what's behind, I do get the original image and the filetype is jpeg, so I can't seem to figure out why it would say that the Input buffer contains unsupported image format.

const imageBuffer = fileFromS3.Body
console.log(imageBuffer)
console.log(imageBuffer.toString('base64'))

let result = await sharp(imageBuffer)
  .toColourspace('b-w')
  .sharpen()
  .toBuffer()

When I check the sharp object before running toBuffer() I get this data in the object:

let result = await sharp(imageBuffer)
  .toColourspace('b-w')
  .sharpen()

console.log(result)

Prints:

...
input: {
      failOnError: true,
      limitInputPixels: 268402689,
      sequentialRead: false,
      buffer: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff db 00 43 00 03 02 02 02 02 02 03 02 02 02 03 03 03 03 04 06 04 04 04 04 04 08 06 06 05 ... 96981 more bytes>
    }
Cremator answered 16/11, 2021 at 22:50 Comment(1)
Same issue here. It's not happening to all my s3 images, but I have a few of these issues every day. I didn't find a solution for it yet.Bewley

© 2022 - 2024 — McMap. All rights reserved.