How to change the DPI of a photo in Node.js

The first thing we need to do is add a reference to the project in the npm packages file:

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

Then we need to require the file:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');

Last but not least, all we need to do is call convertImageImageSetDPI to set the DPI of the photo:

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.ConvertImageApi();var dpi = 56; // Number | New DPI in pixels-per-inch, for example 300 DPI or 600 DPIvar 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.convertImageImageSetDPI(dpi, inputFile, callback);

From here, the DPI of the photo will be changed. We can pass in various possible values, such as 300 DPI or 600 DPI. The rescaled photo will be returned to us.

That’s all there is to it!

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store