How to Convert Images to GIF Format in Node.js

Cloudmersive
2 min readNov 20, 2023

--

Gif format isn’t just great for animations — it’s also a good option for loading small images (logos, thumbnails, etc.) on websites because of its color limits.

Using the below ready-to-run code, we can easily incorporate an API into our Node.js applications which can convert dozens of common image formats into GIF format. By authorizing our request with a free-tier API key, we’ll be able to make up to 800 conversions per month with no additional commitments.

To install the SDK, we can either run this command:

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

Or add this snippet to our package.json:

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

After that, we can use the below code to call the function and make our conversion (remembering to include our API key in the appropriate line):

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.convertToGif(imageFile, callback);

And we’re all set. No more code required!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet