I'm using express-fileupload
for reading files from the API. Now I want to process the image in the request body using Sharp
.
I don't want to first save the file at the server and process it using fs.readFileSync
.
I tried passing req.files.image.data
which is supposed to be a buffer.
const image = await sharp(Buffer.from(req.files.image.data))
.resize(500, 500)
.jpeg({ quality: 10 })
.toBuffer()
.then((outputBuffer) =>
({ data: outputBuffer, mimetype: 'image/jpeg' }))
.catch(err => {
console.log(err);
return null;
});
But it is throwing error this error: [Error: VipsJpeg: Premature end of input file]
When I tried converting the image buffer data into string as suggested in this post, converting it into buffer using Buffer.from
and then passing it, it throwing error: [Error: Input buffer contains unsupported image format]
Edit: There was a limit on image size 5mb, that's why the images greater than that were not getting completely captured in the buffer, hence, this error.
app.use(fileUpload({
limits: { fileSize: 50 * 1024 * 1024 },
}));
Some questions arise, when I see your code. Let me try to get closer to a possible solution with your provided input by asking some questions and providing hints:
req.files.image.data
is really a buffer, why do you try to generate a buffer again by using Buffer.from(req.files.image.data)
? You want to create a buffer from a buffer?.toBuffer()
. From an already existing buffer? In this case, I speak from personal experience, sharp would throw an error if trying to create a buffer from an already existing buffer.req.files.image.data
is supposed to be a buffer. Sounds maybe you are not 100% sure. I suggest to check if you really have a buffer by usingconst isItReallyBuffer = Buffer.isBuffer(req.files.image.data)
After that you can simply print it to the console: console.log(isItReallyBuffer); // true or false
req.files.image.data
one more time. Is it really a not corrupted image file? Does it really comply with the input options accepted by Sharp listed above?const image = await sharp(req.files.image.data)...
.toBuffer()
AMENDMENT
fs.readFileSync
is often used to process image files. In this case, I speak from my personal experience of many days of working with image files in node.js, I would think about using fs and better prefer the Sharp package for reading and writing image files. I don't use fs anymore. Fs is concatenating image chunks which in turn increases the probability leading to memory hogs.
You can simply open an PNG image on your desktop with WordPad or Notepad and search for IDAT chunks. Then, process the same image with fs package and you will see the difference; all of a sudden you likely have only one very huge IDAT chunk.