How to Directly Validate 100+ Image File Formats using Node.js

Cloudmersive
2 min readJan 10, 2024

--

In a previous article, we demonstrated a free API solution that can simultaneously scan image files (any other file types) for viruses & validate image file contents to prevent insecure image rendering/buffering. In this article, we’ll look at a boiled-down version of that solution which focuses exclusively on image format validation.

Using the below code, we can take advantage of a free API designed to validate more than 100 unique image file formats. This service ensures image files rigorously conform to their particular format standards, and it also points out if relevant images (such as images in PDF format, for example) have password protection engaged. Invalid and/or heavily encrypted image files can sometimes indicate a threat, and it’s best that we don’t allow our applications to process such files.

To structure our API call, we can begin by installing the SDK. Let’s either run the following command:

npm install cloudmersive-convert-api-client --save

Or add the following snippet to our package.json instead:

"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}

Next, let’s get ready to authenticate our API call by grabbing a free-tier API key. These allow a limit of 800 API calls per month with no commitments (once we reach our request limit, our total will reset the following month).

With our API key in hand, we can now copy the below code into our file and begin validating our image files:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';



var apiInstance = new CloudmersiveConvertApiClient.ValidateDocumentApi();

var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.


var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.validateDocumentImageValidation(inputFile, callback);

That’s all there is to it — now we can quickly validate image files before they reach our image processing applications using minimal code.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet