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

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

我正在使用 sharp 服务器端来准备要在网络应用程序中提供的图片。 目前的objective是加载一张图片(BMP格式),在nodejs中用sharp加载,转成PNG,调整大小(缩小),存回磁盘。代码如下:

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

我收到这个错误:

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

输出文件夹保持为空。

我不太明白为什么:路径是正确的,所以可以访问。图片存储在磁盘上,路径直接在服务器端计算(因此没有编码问题,正如我在其他地方看到的有关此问题)。

有人有想法或解决方案吗?

sharp好像不能处理BMP图片。 (参见:https://github.com/lovell/sharp/issues/1255

所以我改用 Jimp(参见: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);
            });