Empty JPEG image (DNL not supported) node-canvas

安稳与你 提交于 2020-01-02 19:59:30

问题


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.

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.


回答1:


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.

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.



来源:https://stackoverflow.com/questions/16470268/empty-jpeg-image-dnl-not-supported-node-canvas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!