Skip to content

⚡ High-performance image compression API built with Sharp. Optimize your images with a simple REST API that delivers up to 90% size reduction while preserving quality.

License

Notifications You must be signed in to change notification settings

lx-0/image-compression-service

Repository files navigation

🖼️ Image Compression Microservice

Image Compression Microservice

FeaturesGetting StartedAPIExamplesDeployment

Stars Node Platform License

⚡ High-performance image compression API with minimal configuration ⚡

✨ Features

  • 🚀 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

🚀 Getting Started

Quick Start (15 seconds)

# 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.

Using in your application

// 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();
}

🛠️ API Reference

POST /api/compress

Compresses an image with optional parameters.

Request

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)

Response

{
  "success": true,
  "data": {
    "base64": "data:image/jpeg;base64,/9j/4AAQSkZJRgABA...",
    "fileSize": 24680,
    "mimeType": "image/jpeg"
  }
}

Error Handling

The API returns appropriate status codes with error messages:

  • 400 Bad Request - When no image is provided or parameters are invalid
  • 415 Unsupported Media Type - When the image format is not supported
  • 500 Internal Server Error - When compression fails unexpectedly

📋 Examples

cURL

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'

Node.js

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);
  }
}

Python

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())

⚙️ Deployment

Docker

# Build the image
docker build -t image-compression-service .

# Run the container
docker run -p 5000:5000 image-compression-service

Environment Variables

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 *

🔄 Benchmark

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

📝 License

Released under the MIT License. See LICENSE for details.

🤝 Contributing

Contributions, issues, and feature requests are welcome! Feel free to check issues page.

📧 Contact

Questions? Reach out to us at [email protected].


Made with ❤️ by lx-0

About

⚡ High-performance image compression API built with Sharp. Optimize your images with a simple REST API that delivers up to 90% size reduction while preserving quality.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages