我正在尝试使用node-canvas来裁剪背景图像,但我在以下代码中遇到了一些奇怪的错误。
错误是" error : error while to output stream“。如果我使用node-canvas代码库中的裁剪示例代码,我会得到这个错误"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);
});
}
});
}
});我已经多次使用node-canvas,没有任何问题,但由于某些原因,这是一个问题。任何建议都是很棒的。
发布于 2013-05-10 04:28:04
libjpeg (可能在幕后使用)不支持JPEG文件(see this document, towards the end, for an explanation)中的DNL标记。包含此类标记的文件将声明为零高度(因此出现Empty JPEG image消息)。我的猜测是您的输入文件使用了这些标记,从而触发了一个错误。
至于解决方案:试着找到一个JPEG阅读器,它可以读取这些类型的文件,并可以将它们转换回libjpeg可以处理的文件。恐怕我不能推荐任何东西,因为我自己从来没有遇到过这个问题。
https://stackoverflow.com/questions/16470268
复制相似问题