How to Convert an Image to PNG format with JavaScript
Looking for an easy, stress-free way to quickly convert images to PNG format on your app/website? We have a great solution: our PNG conversion API takes dozens of common image formats and converts them to PNG automatically, returning the necessary file encoding to produce a new PNG file. If you’re uploading transparent images, never fear — the transparency will be preserved during the conversion.
You can use this API for free by registering a free account on our website. Use the below JavaScript code examples to structure your API call, and then include your API key to authenticate access — it’s just that easy.
To structure your call around JavaScript’s built-in XHR feature, use the below snippet:
var data = new FormData();
data.append("imageFile", fileInput.files[0], "file");
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;xhr.addEventListener("readystatechange", function() {
if(this.readyState === 4) {
console.log(this.responseText);
}
});xhr.open("POST", "https://api.cloudmersive.com/image/convert/to/png");xhr.setRequestHeader("Apikey", "YOUR-API-KEY-HERE");xhr.send(data);
To install the jQuery library instead, run the following command:
bower install jquery
Then structure your call like so:
var form = new FormData();
form.append("imageFile", fileInput.files[0], "file");var settings = {
"url": "https://api.cloudmersive.com/image/convert/to/png",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "multipart/form-data",
"Apikey": "YOUR-API-KEY-HERE"
},
"processData": false,
"mimeType": "multipart/form-data",
"contentType": false,
"data": form
};$.ajax(settings).done(function (response) {
console.log(response);
});
And you’re all done — easy as that.