Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty JPEG image (DNL not supported) node-canvas

Tags:

node.js

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.

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.

like image 619
Jovan Alleyne Avatar asked May 09 '13 20:05

Jovan Alleyne


1 Answers

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.

like image 156
robertklep Avatar answered Sep 23 '22 23:09

robertklep