Sharp: PNG / JPEG: Input file contains unsupported image format

Created on 3 Jul 2019  路  2Comments  路  Source: lovell/sharp

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

question

Most helpful comment

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)... );

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings