# 1. Compress a single image and convert it to WebP:
curl -X POST "https://www.losslessimg.com/api/v1/compress" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Accept: application/json" \
-F "image=@/path/to/my_image.png" \
-F "convert_to=webp"
# 2. Batch upload and convert multiple files in one request:
curl -X POST "https://www.losslessimg.com/api/v1/compress" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Accept: application/json" \
-F "images[]=@/path/to/img1.png" \
-F "images[]=@/path/to/img2.jpg" \
-F "convert_to=webp"
// Example: Batch upload and convert multiple files to WebP
const formData = new FormData();
formData.append('convert_to', 'webp');
// Append files to the images[] batch array
filesArray.forEach((file) => {
formData.append('images[]', file);
});
fetch('https://www.losslessimg.com/api/v1/compress', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Accept': 'application/json'
},
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success && data.results) {
data.results.forEach(result => {
console.log(`File: ${result.original_name} -> Converted: ${result.converted_to}`);
console.log(`Download: ${result.download_url}`);
});
}
})
.catch(err => console.error(err));
// Example: Batch upload & convert to WebP using Guzzle
$client = new \GuzzleHttp\Client();
$response = $client->post('https://www.losslessimg.com/api/v1/compress', [
'headers' => [
'Authorization' => 'Bearer YOUR_API_KEY',
'Accept' => 'application/json',
],
'multipart' => [
[
'name' => 'convert_to',
'contents' => 'webp'
],
[
'name' => 'images[]',
'contents' => fopen('/path/to/image1.png', 'r'),
'filename' => 'image1.png'
],
[
'name' => 'images[]',
'contents' => fopen('/path/to/image2.jpg', 'r'),
'filename' => 'image2.jpg'
]
]
]);
$data = json_decode($response->getBody(), true);
if ($data['success']) {
foreach ($data['results'] as $result) {
echo "Optimized: " . $result['original_name'] . " -> " . $result['download_url'] . "\n";
}
}
# Example: Batch upload & convert to WebP
import requests
url = "https://www.losslessimg.com/api/v1/compress"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Accept": "application/json"
}
# Define files array using images[] key
files = [
('images[]', ('img1.png', open('image1.png', 'rb'), 'image/png')),
('images[]', ('img2.jpg', open('image2.jpg', 'rb'), 'image/jpeg'))
]
data = {"convert_to": "webp"}
response = requests.post(url, headers=headers, files=files, data=data)
res_json = response.json()
if res_json.get('success'):
for r in res_json['results']:
print(f"File: {r['original_name']} -> {r['download_url']}")