ZettaCircl
ZettaCircl

Reputation: 875

NodeJS and Sharp, BMP to PNG Error: Input file contains unsupported image format

I am using sharp server-side to prepare pictures to be served in a webapp. The current objective is to load a picture (BMP format), to load it in nodejs with sharp, to convert it in PNG, to resize it (scale down) and save it back to disk. The code is the following :

  if(resize_pictures){

      (...)

      console.log('Reducing image size ... ');
      fs.readdirSync(input_folder).forEach(file => {
            tmp_input_path = path.join(input_folder, file)
            tmp_output_path = path.join(tmp_folder_reduced, file)

            //Resize
            sharp(tmp_input_path)
                .png() // Convert to png
                .resize(target_width,null)
                .flatten()
                .toFile(tmp_output_path,
                function(err){
                    if(err){
                    console.log("Error at reducing size / converting picture : ")
                    console.log(err)
                    console.log(tmp_input_path);
                    console.log(tmp_output_path);
                    return;
                    }
                })
    })
    console.log('Image reduction completed.');

enter image description here

I'm getting this error :

Reducing image size ... 
Image reduction completed.
Error at reducing size / converting picture : 
[Error: Input file contains unsupported image format]
/home/user/<folder>/16c93ac9f297376b1b44eeeecff141b1f59a239d.bmp
/home/user/<folder>/TMP/16c93ac9f297376b1b44eeeecff141b1f59a239d.bmp

Output folder stays empty.

I don't really get why : the paths are correct, and so can be accessed. Pictures are stored on disk, paths are directly computed server-side (so no encoding problems, as I could have seen somewhere else concerning this problem).

Would someone have an idea or a solution ?

Upvotes: 3

Views: 10942

Answers (1)

ZettaCircl
ZettaCircl

Reputation: 875

It seems that sharp can't handle BMP pictures. (See : https://github.com/lovell/sharp/issues/1255 )

So I switch to Jimp (See : https://www.npmjs.com/package/jimp) :

  console.log('Reducing image size ... ');
  fs.readdirSync(input_folder).forEach(file => {
        let tmp_input_path = path.join(input_folder, file)
        let tmp_file = file.substr(0, file.lastIndexOf(".")) + ".png";
        let tmp_output_path = path.join(tmp_folder_reduced, tmp_file)

        if(fs.existsSync(tmp_input_path)){
            console.log("File exist ! ")
        }

        //Resize
        Jimp.read(tmp_input_path)
            .then(image => {
                image
                .resize(target_width, Jimp.AUTO)
                .write(tmp_output_path)
            })
            .catch(err => {
                console.log("Error at reducing size / converting picture : ")
                console.log(err)
                console.log(tmp_input_path);
                console.log(tmp_output_path);
            });

Upvotes: 2

Related Questions