WebP is a popular modern image format providing superior lossless and lossy compression for images on the web. This image format is developed by Google and is intended as a replacement for JPEG, PNG, and GIF file formats. It supports both lossy and lossless compression, as well as animation and alpha transparency. They are also popular because a WebP file is generally much smaller than a traditional JPEG and hence websites load faster and give users a better experience. However, many conventional systems still require raster image formats for storage and processing purposes therefore, we my have a requirement to perform WebP to JPG bulk conversion. So in this article, we are going to discuss the details on how to programmatically convert WebP to JPG, WebP to PNG and WebP to GIF formats.
- WebP to JPG REST API
- Convert WebP to JPEG in Java
- WebP to PNG in Java
- WebP to GIF using cURL Commands
WebP to JPG REST API
Our REST based solution Aspose.Imaging Cloud SDK for Java is a reliable image processing and image conversion API. With the help of this API, you can implement image processing capabilities (edit and transform) in your application. It supports raster images, Photoshop files, Metafiles and other formats such as WebP format. So as per the scope of this article, we are going to use this API to convert WebP to JPEG. Now in order to get started with SDK utilization, we need to add its 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 WebP to JPEG in Java
Let’s discuss the details on how to load and convert WebP to JPG format using Java code snippet.
- Firstly, create an object of ImagingApi using personalized credentials
- Secondly, load the WebP image using readAllBytes(…) method and pass to byte[] array
- Thirdly, create an instance of UploadFileRequest by providing WebP image as argument and upload it to cloud storage using uploadFile(…) method
- Now create an object of ConvertImageRequest which takes WebP image name and JPG format as arguments
- Call the method convertImage(…) to convert WebP to JPEG format. The output is returned as response stream
- Finally, save the resultant JPG to local drive using FileOutputStream object
The sample WebP image used in above example can be downloaded from WEBPSampleImage.webp and the output from Converted.jpg.
WebP to PNG in Java
This section discusses the details on how to convert WebP to PNG format without using Cloud storage.
- First, create an object of ImagingApi using personalized credentials
- Secondly, load the WebP image using readAllBytes(…) method and pass to byte[] array
- Thirdly, create an object of CreateConvertedImageRequest which takes WebP image name and pNG format as arguments.
- Now call the method createConvertedImage(…) to convert WebP to PNG. The output is returned as response stream
- Finally, save the resultant PNG to local drive using FileOutputStream object
Note:- If we need to save the resultant PNG on cloud storage, pass PNG 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.
WebP to GIF using cURL Commands
The REST API can also be accessed via cURL commands, so in this section, we are going to perform WebP to GIF conversion using 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"
After the JWT has been generated, please execute the following command to perform WebP to GIF conversion.
curl -v -X GET "https://api.aspose.cloud/v3.0/imaging/WEBPSampleImage.webp/convert?format=GIF" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>" \
-o "Converted,gif"
Conclusion
This article has explained all the necessary details on how to convert WebP to JPEG, WebP to PNG and how to perform WebP to GIF conversion using REST API. We have learned the steps to either programmatically transform WebP to JPG or convert webp to gif using cURL commands. We highly encourage you to explore the Product Documentation which is an amazing source of information to learn about other exciting features offered by the API.
You may also consider testing API features through SwaggerUI within a web browser and in case you need to download and modify the source code of Cloud SDK, it is available on GitHub (published under MIT license). Lastly, in case you encounter any issues while using the API, you may consider approaching us for a quick resolution via free product support forum.
Related Articles
Please visit the following links to learn more about: