空JPEG图像(不支持DNL)节点画布 [英] Empty JPEG image (DNL not supported) node-canvas

查看:87
本文介绍了空JPEG图像(不支持DNL)节点画布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用节点画布来裁剪背景图像,并且使用以下代码遇到了一些奇怪的错误。

I am trying to use node-canvas to crop a background image and i am running into some a weird error with the following code.

错误为 [错误:写入输出流时出错]。如果我使用节点画布存储库中的裁剪示例代码,则会收到此错误空JPEG图像(不支持DNL)

The error is "[Error: error while writing to output stream]". If i use the crop example code from the node-canvas repo i get this error "Empty JPEG image (DNL not supported)"

/* */
var cnv = new Canvas(w,h),
    ctx = cnv.getContext('2d'),
    original  = '/img/pages/'+page+'/background.jpg';


ctx.imageSmoothingEnabled = true;



fs.readFile('public/'+original, function( err,image){
  if (err) {
    res.status(404)
        .send('Not found');
  }
  else {
    var 
    img     = new Image;
    img.src = image;


    ctx.drawImage(img, 
      Math.abs( (w-img.width)/2),0,
      w,h,
      0,0,
      w,h
    );


    cnv.toBuffer(function(err, buf){

      console.log(err);

      if( err){
        res.status(500)
            .send('Error generating image buffer');
      }
      else {
        fs.writeFile('public'+resampled, buf, function(err){

          console.log(err);
          console.log('Resized and saved in %dms', new Date - start);

          returnResampledFile( res,page,w,h);

        });
      }
    });
  }
});

我已经多次使用节点画布,没有问题,但是由于某种原因,这是一个问题。任何建议都会很棒。

I've used node-canvas several times with no issues but for some reason this is a problem. any suggestions would be great.

推荐答案

libjpeg (可能已使用在幕后)不支持JPEG文件中的DNL标记(请参阅本文末尾的说明)。包含此类标记的文件将声明高度为零(因此,空JPEG图像消息)。我的猜测是您的输入文件使用了此类标记,从而引发错误。

libjpeg (which is probably used under the hood) doesn't support DNL markers in JPEG files (see this document, towards the end, for an explanation). Files which contains such markers will declare to be of zero height (hence the Empty JPEG image message). My guess would be your input file uses such markers, triggering an error.

至于解决方案:尝试找到一个可以读取这些类型的文件并可以转换的JPEG阅读器。他们回到libjpeg可以处理的东西。恐怕我什么都不推荐,因为我自己从来没有遇到过这个问题。

As for solutions: try and find a JPEG reader that will read these types of files and can convert them back to something that libjpeg can handle. I'm afraid I can't recommend anything as I've never run into this problem myself.

这篇关于空JPEG图像(不支持DNL)节点画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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