reSmush
DevelopmentA Guide to Using the Resmush.it API
The Resmush.it API provides a way to compress and optimize images. Developers can integrate this API into their applications to reduce the burden on image processing and hosting resources. In this guide, we will be exploring the use of the Resmush.it API with JavaScript.
API Requirements
To use the Resmush.it API, you'll need a valid API key which can be obtained by signing up on the Resmush.it website.
Basic Usage
The Resmush.it API allows for two different types of requests:
- POST request: allows you to upload an image file.
- GET request: allows you to enter the URL of the image to be optimized.
Let's dive into a simple example.
POST Request
To upload an image for compression, the following steps are to be followed:
- Create a new
FormData
object and append the file entry to it.
let formData = new FormData();
formData.append('files', fileObject);
- Make an AJAX request to the endpoint with your API key added to the header and the
FormData
object attached to the payload.
const url = 'https://api.resmush.it/ws.php?';
$.ajax({
url: url,
dataType: 'json',
processData: false,
contentType: false,
headers: {
'X-API-KEY': 'YOUR_API_KEY_HERE'
},
type: 'POST',
data: formData,
success: function(response) {
console.log(response);
}
});
- The response will contain compressed image information such as image size, URL, etc.
GET Request
To optimize an image using the API by using its URL, the following command can be used.
fetch(`http://api.resmush.it/ws.php?img=${encodeURI(imageUrl)}`, {
headers: {
'Content-Type': 'application/json',
'X-API-KEY': 'YOUR_API_KEY_HERE'
},
}).then((response) => {
return response.json();
}).then((data) => {
console.log(data);
});
Conclusion
The Resmush.it API is an excellent tool for reducing image size and optimizing images without degrading quality. Integrating the API into a web-based application allows you to ensure that your images are always optimized while minimizing overall hosting costs. Get started today with these examples, and you'll be well on your way to bringing your applications to the next level!