How to Rotate an Image using Node.js
Looking to implement photo-rotation services on your app/website? Our Image rotation API could be the service you’re looking for. It’ll allow you to specify any degree of rotation between 0.0–360.0, and it’ll accept common file formats like PNG or JPEG.
We’ve provided ready-to-run code snippets below to help you structure an API call in Node.js; alternatively, you can test this API out on our Cloudmersive API Console, or structure your call using code snippets in 12+ additional languages (with SDK installation instructions included for each).
To begin, let’s install the Node.js SDK. Run the following command:
npm install cloudmersive-image-api-client --save
Next up, add the following code block to begin the API call and authenticate your Cloudmersive API key:
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';
To get a Cloudmersive API key, just visit our website and register a free account. Now you can wrap up the API call with one last code block:
var apiInstance = new CloudmersiveImageApiClient.EditApi();var degrees = 1.2; // Number | Degrees to rotate the image; values range from 0.0 to 360.0.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.editRotate(degrees, imageFile, callback);
After your parameters are configured, you’re all set — no further code required.