How to Convert an Image to PNG format using Node.js
Whether you’re looking to implement an image format conversion service on your website, or you’re simply converting file types for your own purposes, we have an API for you: our PNG conversion API will automatically convert dozens of input file types to PNG format, and it’ll preserve image transparency where relevant. To see a full list of compatible file types for this conversion, visit our Cloudmersive API Console page. To structure an API call in Node.js, refer to the demonstration below which includes ready-to-run code snippets & documentation for your convenience.
To get started, let’s install the Node.js SDK by running the following command:
npm install cloudmersive-image-api-client --save
Next, we can start building our API call. We’ll first include the API client and the API key authentication snippet:
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';
You can get a Cloudmersive API key by registering a free account on our website.
To complete your API call, add the remaining code block:
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);
After that, you’re all done — there’s no further code required.