首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >nodejs中JIMP的文件签名无效

nodejs中JIMP的文件签名无效
EN

Stack Overflow用户
提问于 2017-09-25 19:27:51
回答 1查看 1.7K关注 0票数 1

我正在使用JIMP将我的图像转换为灰度,并在2%的情况下减少它的签名,因为它损坏了我的图像,并在控制台中抛出错误- " error : Invalid file quality.But at Parser._parseSignature JIMP \lib\parser.js:50:18)“,如果出现问题的代码:

代码语言:javascript
运行
复制
 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);
      });
      }
EN

回答 1

Stack Overflow用户

发布于 2017-10-18 19:20:19

我也遇到了同样的问题,尽管我们尽了最大的努力,但问题似乎是前一个文件仍然没有完成编写。

所以我以一种愚蠢的、可耻的方式做了这件事,并增加了半秒的延迟,以防读取失败。

代码语言:javascript
运行
复制
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();
  }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46404120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档