Resize Image using Java Cloud SDK
Our personal and professional lives are filled with raster images. While using mobile phones, scanners, imaging devices and various other cameras, we produce a huge amount of image data. Depending on the compression algorithm, each device creates images in different dimensions and sizes. When you need to store images (JPG, PNG, GIF, TIFF etc.) in uniform dimensions, you must resize them. Many photo‑resizer applications exist, but for bulk online resizing a programmatic solution is often the best choice. In this article we discuss the steps to develop a picture resizer using the Java Cloud SDK.
Image Resize API
To resize an image we use the Aspose.Imaging Cloud SDK for Java. Its REST‑based architecture lets you access the API from any platform and build a photo resizer. In addition to resizing images online, the SDK lets you create, edit, and transform files into a wide range of supported formats. To get started, add the SDK reference to your Maven project by updating pom.xml:
<repositories>
<repository>
<id>aspose-cloud</id>
<name>artifact.aspose-cloud-releases</name>
<url>https://artifact.aspose.cloud/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-imaging-cloud</artifactId>
<version>22.4</version>
</dependency>
</dependencies>
If you do not yet have an account on the Aspose Cloud Dashboard, create a free account with a valid email address. Then log in, locate or create a Client ID and Client Secret on the Cloud Dashboard; you’ll need these credentials for authentication in the examples below.
Resize Image in Java
This section shows how to resize a PNG image. The API loads the image from the local drive, which you pass directly in the request body. After the resize operation, you can save the updated image to Cloud Storage by specifying the outPath parameter, or omit it to receive a streamed image response.
- Create an ImagingApi instance using your client credentials
- Read the PNG file from a local folder with a File object
- Load the file into a byte[] using readAllBytes(…)
- Define the new dimensions with Integer values
- Build a CreateResizedImageRequest, specifying the output file name
- Call createResizedImage(…) to perform the resize
Resize GIF using cURL Commands
During a resize operation you can also change the output format. In this example we resize a GIF and save the result as a JPG using cURL. First, generate a JWT access token (client‑credentials flow):
curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bb959721-5780-4be6-be35-ff5c3a6aa4a2&client_secret=4d84d5f6584160cbd91dba1fe145db14" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"
After obtaining the token, call the ResizeImage API to resize the GIF and save it as JPG on the local drive (the format parameter is optional):
curl -v -X GET "https://api.aspose.cloud/v3.0/imaging/winter.gif/resize?newWidth=200&newHeight=200&format=JPG" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>" \
-o "resultant.jpg"
Resize GIF Preview
The source GIF can be downloaded from winter.gif and the resulting image from resultant.jpg.
Conclusion
We have learned an effective way to resize images online using Java code snippets and cURL commands, preserving quality throughout. Explore the API capabilities directly in your browser via the Swagger API Reference. Beyond picture‑resizer features, you can discover additional functionality in the Product Documentation. All Cloud SDKs are released under the MIT license, so you can download the full source from GitHub and customize it as needed. If you encounter any issues, feel free to seek help in the free product support forum.
Related Articles
Please visit the following links to learn more about: