How to Easily Resize Images in Nodejs
We can easily automate the process of resizing our images using the complementary, ready-to-run Nodejs code examples provided further down the page.
Our first solution will resize images to any height and width of our choosing, and our second solution will resize our images to a maximum height and width to preserve the original aspect ratio of the image.
Before we begin structuring our API calls, we’ll first need to get a free Cloudmersive API key to authorize our requests. This will allow us to make up to 800 API calls per month with no additional commitments.
To handle client SDK installation, we can either run the following NPM command:
npm install cloudmersive-image-api-client --save
Or we can add the Node client to our package.json:
"dependencies": {
"cloudmersive-image-api-client": "^1.3.4"
}
Now we can copy code to call the function we need. For each function, we can replace the ‘YOUR API KEY’
placeholder string with our own API key string.
To resize our images to any height and width of our choosing, let’s copy from the below code examples. We can enter custom integers for var width and var height respectively:
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.ResizeApi();
var width = 56; // Number | Width of the output image - final image will be exactly this width
var height = 56; // Number | Height of the output image - final image will be exactly this height
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.resizeResizeSimple(width, height, imageFile, callback);
To resize our images preserving the original aspect ratio, let’s copy from the following code examples. We can enter integers into the var maxWidth
and var maxHeight
variables respectively:
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.ResizeApi();
var maxWidth = 56; // Number | Maximum width of the output image - final image will be as large as possible while less than or equial to this width
var maxHeight = 56; // Number | Maximum height of the output image - final image will be as large as possible while less than or equial to this height
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.resizePost(maxWidth, maxHeight, imageFile, callback);
Each action will return the encoding for our adjusted image, and we can simply write that encoding to a new image file (In the same format as the input file).
That’s all there is to it — no more code required! Now we can easily resize our images in two distinct ways using just a few lines of code.