How to Convert Images to PNG and JPG in Nodejs

Cloudmersive
2 min readMay 13, 2024

--

There are dozens of unique image formats out there. If we don’t implement a standard image conversion solution in our Nodejs applications, we’ll end up writing a bunch of new code every time we want to convert unique image types to PNG and JPG.

Conveniently, using the ready-to-run code examples provided below, we can call a pair of free APIs that specialize in converting dozens of unique image formats (including PDF documents) to PNG and JPG respectively.

We can use the same NPM command to install the client SDK for both APIs, and we can also authorize our API calls with a single free Cloudmersive API key (note — this allows a limit of 800 API calls per month with zero commitments).

To install the client SDK, we can run the below command:

npm install cloudmersive-image-api-client --save

Or we can add the Node client to our package.json instead:

  "dependencies": {
"cloudmersive-image-api-client": "^1.3.4"
}

Now we can call two separate functions to convert our images to PNG or JPG respectively. We can use the below code to convert our images to PNG:

var CloudmersiveImageApiClient = require('cloudmersive-image-api-client');
var defaultClient = CloudmersiveImageApiClient.ApiClient.instance;

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



var apiInstance = new CloudmersiveImageApiClient.ConvertApi();

var imageFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.


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

And we can use the below code to convert our images to PNG:

var CloudmersiveImageApiClient = require('cloudmersive-image-api-client');
var defaultClient = CloudmersiveImageApiClient.ApiClient.instance;

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



var apiInstance = new CloudmersiveImageApiClient.ConvertApi();

var quality = 56; // Number | Set the JPEG quality level; lowest quality is 1 (highest compression), highest quality (lowest compression) is 100; recommended value is 75

var imageFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.


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

Both solutions will automatically detect the input file type to make a high-fidelity image format conversion. We can paste our API key where indicated in the var Apikey snippet to authorize our API calls.

No more code required! Now we’re covered when we need to convert images to PNG or JPG in our Nodejs application.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet