admin管理员组

文章数量:1429636

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

I'm getting this error :

Reducing image size ... 
Image reduction pleted.
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 puted server-side (so no encoding problems, as I could have seen somewhere else concerning this problem).

Would someone have an idea or a solution ?

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

I'm getting this error :

Reducing image size ... 
Image reduction pleted.
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 puted server-side (so no encoding problems, as I could have seen somewhere else concerning this problem).

Would someone have an idea or a solution ?

Share Improve this question asked May 24, 2019 at 9:21 ZettaCirclZettaCircl 8731 gold badge10 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

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

So I switch to Jimp (See : https://www.npmjs./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);
            });

本文标签: