Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I directly process buffer data using Sharp in NodeJs

I get buffer data to my program from external and I want to process buffer data and send it as a buffer also. So I don't want to convert buffer into an image. How can I do this?

I try this way but it not work.

const process = await sharp(incoming_buffer_data).grayscale();

fs.writeFileSync('test.jpg', process); // I am using this for testing. Allways I am getting worng image format as an error

like image 760
Shan Avatar asked Jul 26 '26 09:07

Shan


1 Answers

Assuming incoming_buffer_data is indeed a buffer and has a supported image format.

You can either get the output as buffer, and send it to fs.writeFileSync() like you tried to do

const buffer = await sharp(incoming_buffer_data).grayscale().toBuffer();
fs.writeFileSync('test.jpg', buffer);

Or you can write it to a file directly

await sharp(incoming_buffer_data).grayscale().toFile('test.jpg');
like image 112
thammada.ts Avatar answered Jul 29 '26 18:07

thammada.ts



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!