如何在Node.js中检测损坏的图像文件?

问题描述 投票:2回答:1

我正在寻找一些通用库来检查下载的图像是否未损坏。

我发现使用ImageMagick的https://www.npmjs.com/package/gm

我可以使用ImageMagick的identify,它将发现完全损坏的图像(我什至无法打开的图像)。但是也有部分损坏的图像-例如jpeg文件,其中图像的一半为灰色,而这些图像并未被gm标记为损坏。

如何检查图像是否在node.js中损坏?

谢谢!

编辑:

我想检测的是未完全下载的图像。通过剪切图像的最后n个字节可以达到类似的效果。

我想可以通过编程方式检测到它,因为以冗长模式识别的命令会返回:

identify: Premature end of JPEG file broken_example.jpg' @ warning/jpeg.c/JPEGWarningHandler/352.
identify: Corrupt JPEG data: premature end of data segment broken_example.jpg' @ warning/jpeg.c/JPEGWarningHandler/352.

示例(不幸的是,imgur会截断破碎的图像,所以我不得不上传屏幕截图):

Broken image

javascript node.js image-processing png jpeg
1个回答
0
投票

使用:https://www.npmjs.com/package/is-corrupted-jpeg

var isCorrupted = require('is-corrupted-jpeg');
console.log(isCorrupted('./path/to/image/not-corrupted.jpg'));
//=> true
console.log(isCorrupted('./path/to/image/corrupted.jpg'));
//=> false
© www.soinside.com 2019 - 2024. All rights reserved.