简体   繁体   English

空JPEG图像(不支持DNL)节点画布

[英]Empty JPEG image (DNL not supported) node-canvas

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. 我正在尝试使用node-canvas裁剪背景图像,我遇到了一些奇怪的错误,使用以下代码。

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)" 如果我使用node-canvas repo中的crop示例代码,我会收到此错误“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. 我已经多次使用node-canvas而没有任何问题,但由于某种原因这是一个问题。 any suggestions would be great. 任何建议都会很棒。

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 ). libjpeg (可能在引擎盖下使用)不支持JPEG文件中的DNL标记( 有关说明,请参阅此文档,最后 )。 Files which contains such markers will declare to be of zero height (hence the Empty JPEG image message). 包含此类标记的文件将声明为零高度(因此为Empty JPEG image消息)。 My guess would be your input file uses such markers, triggering an error. 我的猜测是你的输入文件使用这样的标记,触发错误。

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. 至于解决方案:尝试找到一个JPEG阅读器,它将读取这些类型的文件,并可以将它们转换回libjpeg可以处理的东西。 I'm afraid I can't recommend anything as I've never run into this problem myself. 我恐怕无法推荐任何东西,因为我自己从未遇到过这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM