Hi,
I'm trying to do a bot in Telegram that can rotate images with Sharp.
The bot download the image from the Telegram's server (With jpg or png, i tried with both) and when goes to rotate catch the error "[Error: Input file contains unsupported image format]".
I think that is the compression of the image (If i save the image manually, from Telegram in .jpg, and set the path of this image, it create a new image rotated), but i don't know if i need to change the compression or if it is a problem of Sharp.
bot.getFile(msg.reply_to_message.photo[msg.reply_to_message.photo.length - 1].file_id).then((imagen) => {
let pathIm = imagen.file_path;
var path = __dirname + '\\ImageRotate\\' + tokenGenerator(10) + '.jpg'
var newPath = __dirname + '\\ImageRotate\\' + tokenGenerator(10) + '.jpg'
var dest = fs.createWriteStream(path);
var newDest = fs.createWriteStream(newPath);
const request = http.get("https://api.telegram.org/file/bot" + config.token + "/" + pathIm, (response) => {
response.pipe(dest);
sharp(path)
.rotate(90, "#ffffff")
.toFile(newPath)
.then(info => {
console.log(info)
})
.catch(err => {
console.log(err)
})
});
Thanks
Hi, this code does not wait for the contents of dest to be written before starting the image processing pipeline, so will attempt to open an empty or partial file. You'll need to listen for the close event on dest before creating the sharp instance.
dest.on('close', () => sharp(path)... );
Yep, that was my bad. Thanks.
Most helpful comment
Hi, this code does not wait for the contents of
destto be written before starting the image processing pipeline, so will attempt to open an empty or partial file. You'll need to listen for thecloseevent ondestbefore creating the sharp instance.