
Features • Getting Started • API • Examples • Deployment
- 🚀 Fast - Built on Sharp, the fastest Node.js image processing library
- 📊 Efficient - Reduce image size by up to 90% without noticeable quality loss
- 🔄 Flexible - Convert between formats (JPEG, PNG, WebP, AVIF) with a single parameter
- 🌐 RESTful API - Simple HTTP API for easy integration into any system
- 📱 Responsive - Resize images for different devices with aspect ratio preservation
- 🔧 Customizable - Control compression quality, format, and dimensions
- 🔍 Visual Comparison - Interactive UI to compare original and compressed images
# Clone the repo
git clone https://github.com/lx-0/image-compression-service.git
# Install dependencies
npm install
# Start the service
npm run dev
The service will be running at http://localhost:5000 - Navigate to this URL to access the web interface.
// JavaScript fetch example
async function compressImage(file) {
const formData = new FormData();
formData.append('image', file);
formData.append('quality', 80);
formData.append('format', 'webp');
const response = await fetch('http://your-api-url/api/compress', {
method: 'POST',
body: formData,
});
return await response.json();
}
POST /api/compress
Compresses an image with optional parameters.
Content-Type: multipart/form-data
Parameter | Type | Required | Description |
---|---|---|---|
image | File | Yes | The image file to compress |
quality | Number | No | Compression quality (1-100, default: 80) |
format | String | No | Output format (jpeg, png, webp, avif) |
width | Number | No | Resize to specified width (maintaining aspect ratio) |
{
"success": true,
"data": {
"base64": "data:image/jpeg;base64,/9j/4AAQSkZJRgABA...",
"fileSize": 24680,
"mimeType": "image/jpeg"
}
}
The API returns appropriate status codes with error messages:
400
Bad Request - When no image is provided or parameters are invalid415
Unsupported Media Type - When the image format is not supported500
Internal Server Error - When compression fails unexpectedly
curl -X POST \
http://localhost:5000/api/compress \
-H 'content-type: multipart/form-data' \
-F 'image=@/path/to/your/image.jpg' \
-F 'quality=75' \
-F 'format=webp'
const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');
async function compressImage() {
const formData = new FormData();
formData.append('image', fs.createReadStream('./image.jpg'));
formData.append('quality', '80');
formData.append('format', 'webp');
try {
const response = await axios.post('http://localhost:5000/api/compress', formData, {
headers: formData.getHeaders()
});
console.log(response.data);
} catch (error) {
console.error(error);
}
}
import requests
url = 'http://localhost:5000/api/compress'
files = {'image': open('image.jpg', 'rb')}
data = {'quality': '80', 'format': 'webp'}
response = requests.post(url, files=files, data=data)
print(response.json())
# Build the image
docker build -t image-compression-service .
# Run the container
docker run -p 5000:5000 image-compression-service
Variable | Description | Default |
---|---|---|
PORT | Port for the API server | 5000 |
MAX_FILE_SIZE | Maximum file size in bytes | 10485760 (10MB) |
ALLOWED_ORIGINS | Comma-separated list of allowed CORS origins | * |
Image Size | Format | Quality | Compression Ratio | Processing Time |
---|---|---|---|---|
5 MB | JPEG | 80 | 75% | ~100ms |
10 MB | PNG | 80 | 85% | ~200ms |
15 MB | WebP | 80 | 90% | ~300ms |
Released under the MIT License. See LICENSE for details.
Contributions, issues, and feature requests are welcome! Feel free to check issues page.
Questions? Reach out to us at [email protected].
Made with ❤️ by lx-0