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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With