A high-performance image optimization service built with Bun and TypeScript. This service handles image optimization through WebP conversion, width resizing, and quality control. Perfect for reducing image sizes while maintaining quality in web applications.
- 🖼️ WebP conversion for optimal compression
- 📏 Dynamic image resizing
- 🎯 Adjustable quality settings
- ⚡ High-performance Bun runtime
- 🔄 Auto-reload in development mode
- 🐳 Docker support with Redis caching
- Bun runtime (for local development)
- Node.js environment (for local development)
- Docker and Docker Compose (for containerized deployment)
cp .env.example .env
- Install Bun if not already installed:
curl -fsSL https://bun.sh/install | bash
- Install dependencies:
bun install
- Build and start the services:
docker compose up --build
Or run in detached mode:
docker compose up --build -d
- Stop the services:
docker compose down
To remove volumes when stopping:
docker compose down -v
For production:
bun start
For development in watch mode:
bun dev
Type checking:
bun type-check
The service will be available at:
http://localhost:3000
Environment variables are configured in docker-compose.yml:
- PORT=3000
- REDIS_URL=redis://redis:6379
The service exposes a single endpoint for image optimization:
Path Parameters:
src
(required): Source URL of the image to optimize or the name of the image in theimages
directory
Query Parameters:
w
(optional): Desired width in pixelsq
(optional): WebP quality (0-100, default: 75)
Example Request:
http://localhost:3000/url-encoded-image-link?w=800&q=80
http://localhost:3000/image-name?w=800&q=80
The service will:
- If the parameter
src
is a URL, fetch the image from the provided URL, if not, look for the image in theimages
directory - Check if the image is already in the Redis cache, if it is, return the cached image
- If the image is not in the cache, optimize it by resizing it to the specified width (if provided) and converting it to WebP format
- Cache the optimized image in Redis
- Return the optimized image
The service leverages:
- TypeScript for type safety
- Bun's native features:
- Fast JavaScript/TypeScript runtime
- Built-in fetch API
- File watching
- Package management
- Docker with multi-stage builds for optimized deployment
- Redis for caching optimization results
WebP format provides:
- Superior compression compared to JPEG/PNG
- Smaller file sizes with quality retention
- Support for lossy and lossless compression
- Broad browser compatibility
Contributions are welcome! Feel free to open issues and pull requests.
This project is open source and available under the MIT License.
Paul George - contact@paulgeorge.dev
Project Link: https://github.com/paulgeorge35/image-optimizer