Excellent library!!!
Two quick questions.
1) Is there a way to savea Base64 image in a string variable as a resized image?
2) are there available option(s) to read an image file (.jpg) and overridingit by saving a resized version of it in the same directory path?
Thank you!
Buffer#toString and pass base64:const resizedImageBuf = await require('sharp')(pathToMyImage)
.resize(32, 32)
.toBuffer();
console.log(`data:image/png;base64,${resizedImageBuf.toString('base64')}`);
// data:image/png;base64,iVBORw0KGgoAAAANS...
(you need to match the Content-Type with the type of the resulting image, of course)
Thank you to you both for helping me on this!
One more quick question in relation to the question number 1 above.
Let assume this time I have a Base64 image in a string variable and have already trimmed off the Content-Type (data:image/png). How can I save the content in this variable to a file with the .resize(32, 32) function mentioned above?
Thank you!
@sonnyk22, that sounds like a job for fs.writeFile. Something like:
const writeFileAsync = require('util').promisify(require('fs').writeFile);
const resizedImageBuf = await require('sharp')(pathToMyImage)
.resize(32, 32)
.toBuffer();
await writeFileAsync('outputFile.txt', resizedImageBuf.toString('base64'), 'utf-8');
fantastic! Another great solution to one of my issue. Thank you again!
Most helpful comment
Buffer#toStringand passbase64:(you need to match the
Content-Typewith the type of the resulting image, of course)