Our personal to official life is encompassed with raster images. While using mobile phones, scanners, imaging devices and various other cameras used in variety of devices, we produce a huge amount of data in terms of images. Now depending upon the compression algorithm, every devices produces the images in difference dimensions as well as sizes. But in case we are have a requirement to store the images (JPG, PNG, GIF, TIFF etc) in unified dimensions, so we need to resize images. We do have a plethora of photo resizer applications available in market but in case we need to resize image online in bulk, a programmatic solution is always a viable approach. Therefore, in this article, we are going to discuss the details/steps on how to develop picture resizer using Java Cloud SDK.
Image Resize API
In order to resize Image, we are going to use Aspose.Imaging Cloud SDK for Java. Owing to its REST based architecture, we can access the API on any platform and develop photo resizer. Apart from feature to resize image online, it also enables you to create, edit and transform image files to variety of supported file formats. Now in order to get started, we need to add its reference in java project by including following information in pom.xml (maven build type project).
<repositories> <repository> <id>aspose-cloud</id> <name>artifact.aspose-cloud-releases</name> <url>http://artifact.aspose.cloud/repo</url> </repository> </repositories> <dependencies> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-imaging-cloud</artifactId> <version>22.4</version> </dependency> </dependencies>
In case you do not have an account over Aspose Cloud Dashboard, please create a free account through valid email address. Now login using newly created account and lookup/create Client ID and Client Secret at Cloud Dashboard. These details are required for authentication purposes in the following sections.
Resize Image in Java
This section explains the steps on how to resize the PNG image. The API expects to load the image from local drive which we need to directly pass the image in the request body. After resize png operation, we can save the updated image on the Cloud Storage by specifying the outPath parameter value. However, if we do not specify the value, the response contains a streamed image.
- Create an instance of ImagingApi based on personalized client credentials
- Read PNG file form local folder using File object
- Create byte instance and read all bytes from file using readAllBytes(…) method
- Specify new image dimensions using Integer objects
- Now create an instance of CreateResizedImageRequest where we specify the name for resultant image
- Finally resize image using createResizedImage(…) method
Resize GIF using cURL Commands
During image resize operation, we can also specify the updated format for image. So in this section, we are going to resize GIF and save the output in JPG format using cURL commands. So as a pre-requisite, we need to first generate a JWT access token (based on client credentials) while executing the following command.
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"
Once the JWT token has been generated, please call ResizeImage API to resize image and save the output in JPG format on local drive (the resultant file 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"
We have leared an amazing approach to resize image online using Java code snippet. It has also enlightened us on details on how to resize image without losing quality using cURL commands. Please note that you can also explore the API capabilties within web browser while using the swagger API Reference. Now other than Picture Resizer capabilities, you can explore and learn about other exciting features of API from Product Documentation. Nevertheless, all our Cloud SDKs are published under MIT license, so you may consider downloading the complete source code from GitHub (which you can modify as per your requirements). Lastly, if you encoutner any issues while using the API, you may consider approaching us for a quick resolution via free product support forum.
Please visit the following links to learn more about: