nodejs中JIMP中的文件签名无效 [英] Invalid file signature in JIMP in nodejs

查看:777
本文介绍了nodejs中JIMP中的文件签名无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JIMP将我的图像转换为灰度并降低其质量。但是对于2%的情况,它会破坏我的图像并在控制台中抛出错误 -
错误:文件签名无效
at Parser._parseSignature(C:\ Users \ Akshay \Desktop\darwin\\\
ode_modules\pngjs
\lib \ parser.js:50:18)
如果有问题的代码如下:

I am using JIMP to convert my image to greyscale and to reduce its quality.But for 2% of the cases it is corrupting my image and throwing an error in the console- "Error: Invalid file signature at Parser._parseSignature (C:\Users\Akshay\Desktop\darwin\node_modules\pngjs \lib\parser.js:50:18)" Below if the problematic code:

 var ext=path.extname(dest);
      if(ext!='.jpg'){
       dest=replaceExt(dest, '.jpg');
      }
      console.log(path.extname(dest));
      var file = fs.createWriteStream(dest);
      ////console.log(url)
      if(url.indexOf('https')!=-1){
        //console.log("https")
        var request = https.get(url, function(response) {
        response.pipe(file);
        file.on('finish', function() {
          Jimp.read(dest).then(function (lennaa) {
            lennaa.resize(256, 256)            // resize
              .quality(90)                 // set JPEG quality
              .greyscale()                 // set greyscale
              .write(dest); // save
          }).catch(function (err) {
            console.error(err);
          });
          file.close(cb);  // close() is async, call cb after close completes.
        });
      }).on('error', function(err) { // Handle errors
        fs.unlink(dest); // Delete the file async. (But we don't check the result)
        if (cb) cb(err.message);
      });
      }


推荐答案

我遇到了同样的问题似乎问题在于,尽管我们付出了最大的努力,之前的文件仍未完成写作。

I faced the same and it seems that the issue is that the previous file is still not finished writing, despite our best effort.

所以我以一种愚蠢,可耻的方式做了这件事,并增加了一半 - 读取失败时的第二次延迟。

So I did it in a dumb, shameful way and added a half-second delay in case the read fails.

let image;
try {
  image = await Jimp.read(filepath);
} catch (error) {
  debug(`Error reading ${filepath}. But we'll try again!`);

  // Wait half second, try again. What an ugly hack. It might as well work.
  let temp = await new Promise(resolve => setTimeout(resolve, 600));
  try {
    image = await Jimp.read(filepath);
    debug('Success reading file on second attempt!');
  } catch (error2) {
    // If totally failed, at least exit gracefully:
    this.session.send('The bot is a little busy now. Please try again.');
    this.session.endDialog();
  }
}

这篇关于nodejs中JIMP中的文件签名无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆