|
1 |
| -# Image Compression Service Documentation |
| 1 | +# Image Optimizer Service Documentation |
2 | 2 |
|
3 | 3 | ## 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. |
5 | 5 |
|
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. |
12 | 12 |
|
13 | 13 | ---
|
14 | 14 |
|
15 | 15 | ## Installation
|
16 | 16 |
|
17 | 17 | ### 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. |
23 | 23 |
|
24 |
| -### Clone the repository: |
25 |
| -```bash |
26 |
| -git clone <repo_url> |
27 |
| -``` |
| 24 | +### Steps: |
28 | 25 |
|
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 | + ``` |
35 | 30 |
|
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 | + ``` |
40 | 42 |
|
41 | 43 | ---
|
42 | 44 |
|
43 | 45 | ## Usage
|
44 | 46 |
|
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. |
48 | 50 | - `-s` (default: `*`): Comma-separated list of allowed domains for downloading images. Use `*` to allow all domains.
|
49 | 51 |
|
50 | 52 | Example:
|
51 | 53 | ```bash
|
52 | 54 | ./image-compressor -o ./compressed-images -p 8080 -s example.com,another.com
|
53 | 55 | ```
|
54 | 56 |
|
55 |
| -### Endpoints |
| 57 | +### API Endpoints |
56 | 58 |
|
57 | 59 | #### 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. |
59 | 61 |
|
60 | 62 | ##### 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. |
66 | 68 |
|
67 | 69 | ##### Example:
|
68 | 70 | ```
|
69 | 71 | GET /optimize?url=https://example.com/image.jpg&output=webp&quality=80&resolution=800x600&v=1
|
70 | 72 | ```
|
71 | 73 |
|
| 74 | +##### Response: |
| 75 | +Returns the compressed image in the specified format or the original format if none is specified. |
| 76 | + |
| 77 | +--- |
| 78 | + |
72 | 79 | #### 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). |
74 | 81 |
|
75 | 82 | ##### Example:
|
76 | 83 | ```
|
77 | 84 | GET /optimize/abcd1234.jpeg
|
78 | 85 | ```
|
79 | 86 |
|
| 87 | +##### Response: |
| 88 | +Returns the requested image file from the output directory. |
| 89 | + |
| 90 | +--- |
| 91 | + |
80 | 92 | #### 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!"`. |
82 | 94 |
|
83 | 95 | ##### Example:
|
84 | 96 | ```
|
85 | 97 | GET /
|
86 | 98 | ```
|
87 | 99 |
|
| 100 | +##### Response: |
| 101 | +``` |
| 102 | +ok! |
| 103 | +``` |
| 104 | + |
88 | 105 | ---
|
89 | 106 |
|
90 | 107 | ## How It Works
|
91 | 108 |
|
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. |
94 | 111 |
|
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. |
97 | 114 |
|
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. |
100 | 117 |
|
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. |
103 | 120 |
|
104 | 121 | ---
|
105 | 122 |
|
106 | 123 | ## 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. |
109 | 127 |
|
110 | 128 | ---
|
111 | 129 |
|
112 | 130 | ## Example Workflow
|
113 | 131 |
|
114 |
| -1. **Download and Compress Image:** |
| 132 | +1. **Download and compress an image:** |
115 | 133 | ```bash
|
116 | 134 | curl "http://localhost:8080/optimize?url=https://example.com/image.jpg&output=png&resolution=800x600&quality=90&v=1"
|
117 | 135 | ```
|
118 | 136 |
|
119 |
| -2. **Retrieve Existing Compressed Image:** |
| 137 | +2. **Retrieve an existing compressed image:** |
120 | 138 | ```bash
|
121 | 139 | curl "http://localhost:8080/optimize/<md5-hash>.png"
|
122 | 140 | ```
|
123 | 141 |
|
124 | 142 | ---
|
125 | 143 |
|
126 | 144 | ## 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