Skip to content

Commit 81f4517

Browse files
committed
enhance docs
1 parent 190c9d6 commit 81f4517

File tree

1 file changed

+75
-52
lines changed

1 file changed

+75
-52
lines changed

README.md

+75-52
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,150 @@
1-
# Image Compression Service Documentation
1+
# Image Optimizer Service Documentation
22

33
## Overview
4-
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.
4+
The Image Optimizer Service allows users to download and compress images from specified URLs. It supports a variety of image formats, including JPEG, PNG, and WebP, with additional features for resizing and quality adjustment. The service also caches compressed images to avoid redundant processing, improving performance.
55

6-
### Features:
7-
- Supports JPEG, PNG, and WebP formats.
8-
- Image resizing based on custom resolution.
9-
- Image quality adjustment for JPEG.
10-
- Caching based on MD5 hash to avoid redundant compression.
11-
- Domain whitelisting to control which sources are allowed for image download.
6+
### Key Features:
7+
- Supports multiple image formats: JPEG, PNG, WebP.
8+
- Allows custom resizing based on user-provided resolution.
9+
- Adjustable JPEG quality.
10+
- Caching system based on MD5 hashing to avoid duplicate processing.
11+
- Domain whitelisting to control allowed image sources.
1212

1313
---
1414

1515
## Installation
1616

1717
### Prerequisites:
18-
- Go 1.18 or later.
19-
- Required Go packages:
20-
- `github.com/gorilla/mux`
21-
- `github.com/chai2010/webp`
22-
- `github.com/nfnt/resize`
18+
- **Go version**: 1.18 or later.
19+
- Required Go libraries:
20+
- `github.com/gorilla/mux` for routing.
21+
- `github.com/chai2010/webp` for WebP image handling.
22+
- `github.com/nfnt/resize` for resizing functionality.
2323

24-
### Clone the repository:
25-
```bash
26-
git clone <repo_url>
27-
```
24+
### Steps:
2825

29-
### Install dependencies:
30-
```bash
31-
go get github.com/gorilla/mux
32-
go get github.com/chai2010/webp
33-
go get github.com/nfnt/resize
34-
```
26+
1. **Clone the repository:**
27+
```bash
28+
git clone <repo_url>
29+
```
3530

36-
### Build the service:
37-
```bash
38-
go build -o image-compressor
39-
```
31+
2. **Install dependencies:**
32+
```bash
33+
go get github.com/gorilla/mux
34+
go get github.com/chai2010/webp
35+
go get github.com/nfnt/resize
36+
```
37+
38+
3. **Build the application:**
39+
```bash
40+
go build -o image-compressor
41+
```
4042

4143
---
4244

4345
## Usage
4446

45-
### Command Line Flags
46-
- `-o` (default: `.`): Specifies the output directory for compressed images.
47-
- `-p` (default: `8080`): Specifies the port on which the server listens.
47+
### Command-Line Options:
48+
- `-o` (default: `.`): Specifies the output directory where compressed images will be saved.
49+
- `-p` (default: `8080`): Defines the port the service listens on.
4850
- `-s` (default: `*`): Comma-separated list of allowed domains for downloading images. Use `*` to allow all domains.
4951

5052
Example:
5153
```bash
5254
./image-compressor -o ./compressed-images -p 8080 -s example.com,another.com
5355
```
5456

55-
### Endpoints
57+
### API Endpoints
5658

5759
#### 1. **GET /optimize**
58-
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.
60+
Compresses an image from a provided URL, optionally resizing and adjusting the quality. If the compressed image already exists, the cached version is returned without recompression.
5961

6062
##### Query Parameters:
61-
- **url** (required): The image URL to download and compress.
62-
- **output** (optional): Output image format (`jpeg`, `png`, `webp`). Defaults to the format of the original image.
63-
- **quality** (optional): JPEG quality (1-100). Only applies to JPEG format. Defaults to 75.
64-
- **resolution** (optional): Desired resolution in the format `widthxheight`. Use `auto` for either width or height to maintain aspect ratio. Example: `800x600`, `auto x 600`.
65-
- **v** (optional): Versioning parameter used to trigger new compression if image parameters have changed.
63+
- **url** (required): The URL of the image to download and compress.
64+
- **output** (optional): The desired output format (`jpeg`, `png`, `webp`). Defaults to the original format of the image.
65+
- **quality** (optional): JPEG quality (1-100). Only applicable to JPEG images. Default is 75.
66+
- **resolution** (optional): Desired image resolution in the format `widthxheight`. Use `auto` for either width or height to maintain aspect ratio. Example: `800x600`, `auto x 600`.
67+
- **v** (optional): Versioning parameter to force new compression if image parameters change.
6668

6769
##### Example:
6870
```
6971
GET /optimize?url=https://example.com/image.jpg&output=webp&quality=80&resolution=800x600&v=1
7072
```
7173

74+
##### Response:
75+
Returns the compressed image in the specified format or the original format if none is specified.
76+
77+
---
78+
7279
#### 2. **GET /optimize/{filename}**
73-
This endpoint retrieves an existing compressed image by its filename.
80+
Fetches a previously compressed image by its filename (typically the MD5 hash of the image parameters).
7481

7582
##### Example:
7683
```
7784
GET /optimize/abcd1234.jpeg
7885
```
7986

87+
##### Response:
88+
Returns the requested image file from the output directory.
89+
90+
---
91+
8092
#### 3. **GET /**
81-
A basic health check endpoint that returns `"ok!"`.
93+
A basic health check endpoint to confirm that the server is running. Returns `"ok!"`.
8294

8395
##### Example:
8496
```
8597
GET /
8698
```
8799

100+
##### Response:
101+
```
102+
ok!
103+
```
104+
88105
---
89106

90107
## How It Works
91108

92-
### Image Download
93-
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.
109+
### 1. **Image Download**
110+
The service downloads the image from the specified URL and decodes it into an `image.Image` object. Supported formats include JPEG, PNG, and WebP.
94111

95-
### Compression
96-
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.
112+
### 2. **Image Compression**
113+
The downloaded image can be resized based on user-specified resolution parameters. For JPEGs, quality adjustment is supported. After processing, the image is saved in the desired format.
97114

98-
### MD5 Hashing
99-
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.
115+
### 3. **MD5 Hashing for Caching**
116+
To avoid redundant processing, the service generates an MD5 hash based on the image URL, desired output format, quality, resolution, and versioning parameters. This hash is used to create a unique filename for the compressed image. If the file already exists, the cached version is returned.
100117

101-
### File Caching
102-
If the compressed image file already exists, the service retrieves it directly from the output directory, avoiding redundant downloads and compressions.
118+
### 4. **Caching and Performance**
119+
If the compressed image already exists in the output directory, the service retrieves and returns it directly, bypassing the need to download and process the image again. This caching mechanism significantly improves performance and reduces redundant computation.
103120

104121
---
105122

106123
## Error Handling
107-
- If the domain of the image URL is not allowed, the service responds with a `403 Forbidden` error.
108-
- If the image format is unsupported or any other error occurs during image processing, the service responds with a `500 Internal Server Error`.
124+
125+
- **403 Forbidden:** Returned if the domain of the image URL is not on the allowed list (`-s` parameter).
126+
- **500 Internal Server Error:** Occurs if an unsupported image format is provided or an error occurs during the image processing stage.
109127

110128
---
111129

112130
## Example Workflow
113131

114-
1. **Download and Compress Image:**
132+
1. **Download and compress an image:**
115133
```bash
116134
curl "http://localhost:8080/optimize?url=https://example.com/image.jpg&output=png&resolution=800x600&quality=90&v=1"
117135
```
118136

119-
2. **Retrieve Existing Compressed Image:**
137+
2. **Retrieve an existing compressed image:**
120138
```bash
121139
curl "http://localhost:8080/optimize/<md5-hash>.png"
122140
```
123141

124142
---
125143

126144
## License
127-
This project is open source and available under the [MIT License](LICENSE).
145+
This project is licensed under the [MIT License](LICENSE).
146+
147+
---
148+
149+
### Contributions
150+
Feel free to contribute to this project by submitting issues or pull requests on the official repository.

0 commit comments

Comments
 (0)