0

I receive an image in base64 string and I wanted to save the image in 3 different sizes. My code for saving the image in my app is as following and it works, how can I set a sepcific size for the image ?

fs.writeFile(pathImage, new Buffer(base64String, "base64"), function (err) {}

1 Answer 1

1

You can't just save an image in different sizes by writing part of the file to disk. In order to resize your image you need to first know what image format you are working with and then use an appropriate library to resize the image, usually by reducing image quality or cropping the image.

For example if you are working with a JPEG, PNG, WebP, or TIFF images, you could use https://github.com/lovell/sharp

From its example page const sharp = require('sharp'); sharp(inputBuffer) .resize(320, 240) .toFile('output.webp', (err, info) => ... );

Sign up to request clarification or add additional context in comments.

1 Comment

I will use JPG and PNG. In input buffer I create my buffer previously like this ? var inputBuffer = new Buffer(base4String). And 'output.webp' is the path where the image will be ?

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.