SVG images are prominent because they are scalable and maintain the same quality, irrespective of size or screen resolution. They can be designed at any resolution, and they can size up/down, without damaging the quality (or being pixelated). But, since the SVG images are based on points & paths, so instead of pixels, so they cannot display as many details as a raster image. So, if we convert the SVG to PNG image, these issues can be resolved.
SVG to PNG Conversion API
In this article, we are going to use Aspose.Imaging Cloud SDK for Java which is REST based API offering the capabilities to programmatically edit, manipulate and transform raster, metafiles, photoshop to variety of Supported Formats. It also supports the feature to convert SVG to PNG online. Now, in order to get started with SDK utilization, we need to add its reference in our Java project by including the 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>
Now, in order to use the API, we need our personalized client credentials. They can easily be obtained if you are already registered on Aspose Cloud Dashboard. Else, register a free account through a valid email address and get your client credentials.
Convert SVG to PNG in Java
Our Image processing API is capable of converting PNG to SVG and SVG to PNG with minimal code lines. So in this section, we are going to discuss all the details to convert SVG to PNG using Java.
- First of all, create an object of ImagingApi class where we pass the client credentials as argument
- Secondly, read the content of SVG image using readAllBytes(…) method and return it to byte[] array
- Then create an instance of UploadFileRequest while passing the SVG name and upload it to cloud storage using uploadFile(…) method
- Now create an object of ConvertImageRequest, where we pass the input SNG name and resultant format as “PNG”
- Call the convertImage(…) method to initiate the SVG to PNG conversion. The output is then returned as response stream
- Finally, save the resultant PNG to local drive using FileOutputStream object
The input SVG image used in the above example can be downloaded from File_Extension Icons.svg.
Save SVG as PNG using cURL Commands
As Aspose.Imaging Cloud is developed according to REST architecture, so it can easily be accessed via cURL commands. So in this section, we are going to save SVG as PNG using the cURL commands. Now, the first step is to generate a JWT access token (based on client credentials) using 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 is generated, please execute the following command to save SVG as PNG image.
curl -v -X GET "https://api.aspose.cloud/v3.0/imaging/input.svg/convert?format=PNG" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>" \
-o Resultant.png
Conclusion
By the end of this article, we have learned the details on how we can convert SVG to PNG using Java. At the same time, we have also explored an option of converting SVG to PNG using cURL commands. Furthermore, if you would like to get hold of SDK source code and modify it as per your requirements, it can be downloaded from GitHub (our Cloud SDKs are developed under MIT license). Please note that another option for testing the amazing features of the API is via SwaggerUI within the web browser.
Nevertheless, the Product Documentation is a wonderful repository of articles explaining other exciting features of the API. Lastly, in case you encounter any issue while using the API, you may consider approaching us via free product support forum.
Related Articles
We highly recommend visiting the following links to learn more about: