This service allows users to download and compress images from provided URLs. It supports different image formats such as JPEG, PNG, and WebP, with optional resizing and quality adjustments. The server listens for HTTP GET requests, processes the images, and returns the compressed image files.
- Supports JPEG, PNG, and WebP formats.
- Image resizing based on custom resolution.
- Image quality adjustment for JPEG.
- Caching based on MD5 hash to avoid redundant compression.
- Domain whitelisting to control which sources are allowed for image download.
- Go 1.18 or later.
- Required Go packages:
github.com/gorilla/mux
github.com/chai2010/webp
github.com/nfnt/resize
git clone <repo_url>
go get github.com/gorilla/mux
go get github.com/chai2010/webp
go get github.com/nfnt/resize
go build -o image-compressor
-o
(default:.
): Specifies the output directory for compressed images.-p
(default:8080
): Specifies the port on which the server listens.-s
(default:*
): Comma-separated list of allowed domains for downloading images. Use*
to allow all domains.
Example:
./image-compressor -o ./compressed-images -p 8080 -s example.com,another.com
This endpoint compresses the image from the given URL, applying optional resizing and quality adjustments. If the compressed image already exists, it is served directly without reprocessing.
- url (required): The image URL to download and compress.
- output (optional): Output image format (
jpeg
,png
,webp
). Defaults to the format of the original image. - quality (optional): JPEG quality (1-100). Only applies to JPEG format. Defaults to 75.
- resolution (optional): Desired resolution in the format
widthxheight
. Useauto
for either width or height to maintain aspect ratio. Example:800x600
,auto x 600
. - v (optional): Versioning parameter used to trigger new compression if image parameters have changed.
GET /optimize?url=https://example.com/image.jpg&output=webp&quality=80&resolution=800x600&v=1
This endpoint retrieves an existing compressed image by its filename.
GET /optimize/abcd1234.jpeg
A basic health check endpoint that returns "ok!"
.
GET /
The downloadImage
function downloads the image from the provided URL, checks its format, and decodes it into an image object. Supported formats are JPEG, PNG, and WebP.
The compressImage
function resizes the image based on the provided resolution and compresses it based on the chosen format. For JPEG, quality adjustments are possible.
The image processing parameters (URL, output format, quality, resolution, version) are concatenated into a single string and hashed using MD5 to generate a unique filename for the compressed image.
If the compressed image file already exists, the service retrieves it directly from the output directory, avoiding redundant downloads and compressions.
- If the ___domain of the image URL is not allowed, the service responds with a
403 Forbidden
error. - If the image format is unsupported or any other error occurs during image processing, the service responds with a
500 Internal Server Error
.
-
Download and Compress Image:
curl "http://localhost:8080/optimize?url=https://example.com/image.jpg&output=png&resolution=800x600&quality=90&v=1"
-
Retrieve Existing Compressed Image:
curl "http://localhost:8080/optimize/<md5-hash>.png"
This project is open source and available under the MIT License.