I am trying to fetch the image from a remote url, resize it and save it to my computer locally. For this, i am using got library which returns a buffer. i take that buffer and resize the image and then store it in a file using sharp js. Here is my code:
module.exports = async function resize() {
const url = 'https://images.unsplash.com/photo-1636838123974-c94c6904b97a?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=687&q=80';
const readStream = await got(url).buffer();
const resizedBuffer = await sharp(readStream).resize(200, 200);
const storedValue = await resizedBuffer.toFile('output');
console.log("resized buffer", resizedBuffer);
console.log("-----------------------------------------------------------")
console.log(storedValue);
return "ok";
}
The console.log(storedValue)
gives:
{
format: 'jpeg',
width: 200,
height: 200,
channels: 3,
premultiplied: false,
size: 5432
}
However, the output image is not shown as a jpeg by my windows machine.
It shows like this:
The filename output is the output from the code above. hacker is a random unused jpg image. specifying output.jpg to the file does give me a jpg image but the input can be any format jpg, png, gif so, i need to have the same extension as the media.