In this guide, we will show you how to use Java to convert JPG, PNG, JPEG, and GIF image formats to WebP. The process is simple and straightforward, and can be easily integrated into your existing Java application. Whether you need to convert a single image or batch process multiple images, our guide makes it easy to convert JPG to WebP, PNG to WebP, JPEG to WebP, and GIF to WebP with ease.
We know that JPEG images are highly compressible, quite portable and compatible with almost all devices. Also, the high-resolution JPEG images are vibrant and colorful. Similarly, the compression in PNG is lossless and its good for detailed, high-contrast images. However, the WebP format is developed by Google and is a popular modern image format providing superior lossless and lossy compression for images on the web. Its generally much smaller (25%-34%) than JPG, PNG etc and hence websites load faster. Now let’s explore the details on How to convert JPG to WebP in Java.
- JPG to WebP REST API
- Convert JPG to WebP in Java
- PNG to WebP in Java
- GIF to WebP using cURL Commands
JPG to WebP REST API
Aspose.Imaging Cloud SDK for Java is one of our finest solutions offering reliable image processing and image conversion capabilities. Provide image processing capabilities (edit and transform) in your Java application using simple code lines. The API is capable of processing raster images, Photoshop files, Metafiles and WebP format. Now as per the scope of this article, we are going to utilize it to convert jpg to webp in Java application. Now the first step is to add API reference in our 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>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>
Once the REST API reference has been added in Java project, please obtain your client credentials from Cloud Dashboard. Else, you need to first register a free account while using a valid email address.
Convert JPG to WebP in Java
In this section, we are going to discuss all the details related to JPG to WebP conversion using Java code snippet.
- Firstly, create an object of ImagingApi using personalized credentials
- Secondly, load the JPG image using readAllBytes(…) method and pass returned value to byte[] array
- Thirdly, create an instance of UploadFileRequest by providing JPEG image as argument and upload it to cloud storage using uploadFile(…) method
- Now create an object of ConvertImageRequest which takes JPEG image name and ‘webp’ format as arguments
- Call the method convertImage(…) to perform JPG to WebP conversion. The output is then returned as response stream
- Finally, save the resultant WebP to local drive using FileOutputStream object
PNG to WebP in Java
Let’s discuss the details on how we can perform PNG to WebP conversion without using Cloud storage. The source image is loaded from local drive and after conversion, the resultant file is also stored on local hard drive.
- First, create an object of ImagingApi using personalized credentials
- Secondly, load the PNG image using readAllBytes(…) method and pass to byte[] array
- Thirdly, create an object of CreateConvertedImageRequest which takes PNG image name and webp format as arguments.
- Now call the method createConvertedImage(…) to perform PNG to WebP conversion. The output is returned as response stream
- Finally, save the resultant WenP to local drive using FileOutputStream object
Note:- In order to save the resultant WebP on cloud storage, pass WebP path details while creating an object of CreateConvertedImageRequest. In this approach, the last step to store the output stream to local drive will be skipped.
GIF to WebP using cURL Commands
Other than Java code, the REST API can also be accessed via cURL commands. So in this section, we are going to perform GIF to WebP conversion using the cURL commands. Now the pre-requisite is to generate a JWT access token (based on client credentials) using 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 has been generated, please execute the following command to perform GIF to WebP conversion and save the output to local drive
curl -X -v GET "https://api.aspose.cloud/v3.0/imaging/UpdateGIFSampleImage.gif/convert?format=webp" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT>" \
-o "output.webp"
Conclusion
The article has provided all the necessary details on converting JPG to WebP, PNG to WebP and GIF to WebP using Java. At the same time, we have also learned the steps to transform GIF to WebP using cURL commands. Please be aware that we highly encourage you to explore the Product Documentation which is an amazing source of information. It will enlighten you on other exciting features currently being offered by REST API.
Furthermore, you may also consider testing the API features through SwaggerUI (within a web browser) and in case you need to download and modify the source code, it can be accessed from GitHub (its published under MIT license). Lastly, if you encounter any issues while using the API, please do not hesitate in contacting us for a quick resolution via free product support forum.
Related Articles
Please visit the following links to learn more about: