A high-performance, production-ready Go API for image processing with resize, crop, and advanced processing capabilities.
disintegration/imaginggodotenvA high-performance, production-ready Go API designed for efficient image processing. It offers functionalities such as resizing, cropping, and batch processing, leveraging external services like Redis for caching and Supabase for cloud storage to ensure scalability and speed.
.env.example) for asynchronous job queueing (though not fully implemented in current handlers).Clone the repository:
git clone https://github.com/phambaophuc/image-resize.git
cd image-resize
Set up environment variables:
Copy the example environment file and configure your credentials:
cp .env.example .env
Edit the .env file with your Supabase URL, key, bucket, and any other necessary configurations.
Build the Docker image:
docker compose build
Run the application with Docker Compose:
This command will start the API, Redis, and potentially other services defined in docker-compose.yaml.
docker compose up -d
Alternatively, to run directly without Docker (after setting up dependencies and environment variables):
Download Go dependencies:
go mod download
Run the application:
go run server/main.go
The API will be available at http://localhost:8080.
This API provides endpoints for various image manipulation tasks. All requests should be made to the base URL http://localhost:8080/api/v1.
GET /health: Checks the health status of the API and its dependencies (Supabase, Redis).POST /images/resize: Resizes a single image.POST /images/batch/resize: Resizes multiple images concurrently.POST /images/process: Performs advanced image processing (resize, crop, watermark) based on a JSON payload.Resize an image to a specific width and height.
Request:
POST /api/v1/images/resize
Content-Type: multipart/form-data
Parameters:
- image: Image file (required)
- width: Target width in pixels (required)
- height: Target height in pixels (required)
- quality: JPEG quality 1-100 (optional, default: 75)
- format: Output format jpeg|png|webp (optional)
Using curl:
curl -X POST http://localhost:8080/api/v1/images/resize \
-F "image=@./path/to/your/photo.jpg" \
-F "width=800" \
-F "height=600" \
-F "quality=90" \
-F "format=jpeg"
Perform multiple operations (resize, crop, watermark) using a JSON payload.
Request:
POST /api/v1/images/process
Content-Type: multipart/form-data
Parameters:
- image: Image file (required)
- payload: JSON string defining processing steps (e.g., resize, crop, watermark)
Using curl:
curl -X POST http://localhost:8080/api/v1/images/process \
-F "image=@./path/to/your/photo.jpg" \
-F 'payload={"resize": {"width": 800, "height": 600, "quality": 90}, "crop": {"x": 0, "y": 0, "width": 400, "height": 300}, "watermark": {"text": "© Your Company", "position": "bottom-right", "opacity": 0.7}}'
Process multiple images concurrently. The width, height, quality, and format parameters apply to all images in the batch.
Request:
POST /api/v1/images/batch/resize
Content-Type: multipart/form-data
Parameters:
- images: Multiple image files (required)
- width: Target width in pixels (required)
- height: Target height in pixels (required)
- quality: JPEG quality 1-100 (optional, default: 75)
- format: Output format jpeg|png|webp (optional)
Using curl:
curl -X POST http://localhost:8080/api/v1/images/batch/resize \
-F "images=@./path/to/image1.jpg" \
-F "images=@./path/to/image2.png" \
-F "width=400" \
-F "height=300"
For a complete setup including dependencies like Redis:
docker compose up -d --build
This command will build the necessary images and start the containers in detached mode.
If you only want to build and run the application container without other services:
# Build the image
docker build -t image-resize-api .
# Run the container
docker run -p 8080:8080 image-resize-api
Configuration is managed via environment variables, with defaults provided in .env.example.
| Variable | Description | Default |
|---|---|---|
PORT |
Server port | 8080 |
READ_TIMEOUT |
Server read timeout | 10s |
WRITE_TIMEOUT |
Server write timeout | 10s |
SUPABASE_URL |
Supabase project URL | - |
SUPABASE_KEY |
Supabase project anonymous or service role key | - |
SUPABASE_BUCKET |
Supabase storage bucket name | - |
REDIS_ADDR |
Redis server address | localhost:6379 |
REDIS_PASSWORD |
Redis password (if required) | `` |
REDIS_DB |
Redis database number | 0 |
RABBITMQ_URL |
RabbitMQ connection URL | amqp://guest:guest@localhost:5672/ |
MAX_FILE_SIZE |
Maximum allowed upload file size in bytes | 10485760 (10MB) |
UPLOAD_PATH |
Local path for uploads (if not using cloud) | ./uploads |
CACHE_DURATION |
Duration for caching results (e.g., 24h) |
24h |
GIN_MODE |
Gin framework mode (release or debug) |
release |
This project is licensed under the MIT License - see the LICENSE file for details.
Crafted with ❤️ by phambaophuc
⭐ Star this repo if it helped you build something awesome!