Skip to content

Commit 2ce4987

Browse files
committed
Customize resizing via query string
1 parent 5b924de commit 2ce4987

3 files changed

Lines changed: 64 additions & 3 deletions

File tree

.vscode/conf2doc.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
BASE=$(dirname $0)
44
README=$BASE/../README.md
5+
# PATH="/opt/homebrew/opt/grep/libexec/gnubin:$PATH"
56

67
USAGE="$(
78
grep -aPzo --color=never \

README.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,27 @@ This setup serve images from other public origin servers, as well as from Amazon
154154

155155
You can learn how to serve files from private storage in the [configurations section](#serving-files-from-private-storage).
156156

157+
158+
### Customize resizing via query string
159+
160+
You can also parse arguments from the request's query string, such as `?width=300` for the image's width or `?height=200` for the image's height, or even both of demensions, to flexibly change some parameters for resizing.
161+
162+
In this setup example, I used the `width` and `height` arguments to override the existing presets.
163+
164+
> Image with specific demensions (1200x960).<br/>
165+
> http://localhost/@nasa/esp_040663_1415.jpg?width=1200&height=960
166+
167+
> Image with `width` is set to 500px.<br/>
168+
> http://localhost/@nasa/esp_040663_1415.jpg?width=500
169+
170+
> Image with `height` is set to 500px.<br/>
171+
> http://localhost/@nasa/esp_040663_1415.jpg?height=500
172+
173+
> The image with preset `_medium` applied, but the query string will override the dimensions of the output image to 50x200px.<br/>
174+
> http://localhost/@nasa/_medium/esp_040663_1415.jpg?width=50&height=200
175+
176+
See [my configurations](#advanced-settings) to know how it works.
177+
157178
---
158179

159180

@@ -261,7 +282,7 @@ Then run the command in the [Start the server](#start-the-server) section to res
261282

262283
### Advanced settings
263284

264-
All settings for handling image URLs are written in the [`imgproxy.conf`](imgproxy.conf#L70~L216) file using [Nginx's map directives](https://Nginx.org/en/docs/http/ngx_http_map_module.html#directives).
285+
All settings for handling image URLs are written in the [`imgproxy.conf`](imgproxy.conf#L70~L234) file using [Nginx's map directives](https://Nginx.org/en/docs/http/ngx_http_map_module.html#directives).
265286

266287
I keep all configurations in very simple variables. You can also make your own version from this base.
267288

@@ -347,6 +368,7 @@ I keep all configurations in very simple variables. You can also make your own v
347368
348369
> **`$imgproxy_preset`**<br/>
349370
> Define `imgproxy` options for each preset name.
371+
> You can view more details by following [their documentation](https://docs.imgproxy.net/generating_the_url?id=processing-options).
350372
> ```nginx
351373
> map $preset_name $imgproxy_preset
352374
> {
@@ -365,6 +387,26 @@ I keep all configurations in very simple variables. You can also make your own v
365387
> ```
366388
367389
390+
> **`$imgproxy_preset_query` (overriding presets with query string)**<br/>
391+
> Override the `$imgproxy_preset` whenever `width` or `height` provided.
392+
> But beware that dynamic image sizes are able to cause a denial-of-service attack by allowing an attacker to request multiple different image resizes.
393+
> ```nginx
394+
> map "$arg_width:$arg_height" $imgproxy_preset_query
395+
> {
396+
> default '';
397+
>
398+
> # only width
399+
> ~^(?<width>[0-9]+):$ '/size:${width}:0:1:0/bg:ffffff/resizing_type:fill';
400+
>
401+
> # only height
402+
> ~^:(?<height>[0-9]+)$ '/size:0:${height}:1:0/bg:ffffff/resizing_type:fill';
403+
>
404+
> # both width and height
405+
> ~^(?<width>[0-9]+):(?<height>[0-9]+)$ '/size:${width}:${height}:1:0/bg:ffffff/resizing_type:fill';
406+
> }
407+
> ```
408+
409+
368410
> **`$imgproxy_extension`**<br/>
369411
> Detect WebP or AVIF supports from the request header `Accept`.
370412
> ```nginx
@@ -383,7 +425,7 @@ I keep all configurations in very simple variables. You can also make your own v
383425
> ```nginx
384426
> map $arg_skip $imgproxy_options
385427
> {
386-
> default '/unsafe/${imgproxy_preset}/plain/${origin_server}${origin_uri}${imgproxy_extension}';
428+
> default '/unsafe/${imgproxy_preset}${imgproxy_preset_query}/plain/${origin_server}${origin_uri}${imgproxy_extension}';
387429
> ~.+ '/unsafe/plain/${origin_server}${origin_uri}';
388430
> }
389431
> ```

imgproxy.conf

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ map $uri_omitted_origin $preset_name
147147

148148
## **`$imgproxy_preset`**
149149
## Define `imgproxy` options for each preset name.
150+
## You can view more details by following [their documentation](https://docs.imgproxy.net/generating_the_url?id=processing-options).
150151
map $preset_name $imgproxy_preset
151152
{
152153
default 'size:1600:0:0:0/preset:logo';
@@ -162,6 +163,23 @@ map $preset_name $imgproxy_preset
162163
square 'size:500:500:0:1/bg:ffffff/resizing_type:fill/preset:center_logo';
163164
}
164165

166+
## **`$imgproxy_preset_query` (overriding presets with query string)**
167+
## Override the `$imgproxy_preset` whenever `width` or `height` provided.
168+
## But beware that dynamic image sizes are able to cause a denial-of-service attack by allowing an attacker to request multiple different image resizes.
169+
map "$arg_width:$arg_height" $imgproxy_preset_query
170+
{
171+
default '';
172+
173+
# only width
174+
~^(?<width>[0-9]+):$ '/size:${width}:0:1:0/bg:ffffff/resizing_type:fill';
175+
176+
# only height
177+
~^:(?<height>[0-9]+)$ '/size:0:${height}:1:0/bg:ffffff/resizing_type:fill';
178+
179+
# both width and height
180+
~^(?<width>[0-9]+):(?<height>[0-9]+)$ '/size:${width}:${height}:1:0/bg:ffffff/resizing_type:fill';
181+
}
182+
165183
## **`$imgproxy_extension`**
166184
## Detect WebP or AVIF supports from the request header `Accept`.
167185
map $http_accept $imgproxy_extension
@@ -176,7 +194,7 @@ map $http_accept $imgproxy_extension
176194
## When URL query `?skip=1` is set, use another rule to skip `imgproxy` processing.
177195
map $arg_skip $imgproxy_options
178196
{
179-
default '/unsafe/${imgproxy_preset}/plain/${origin_server}${origin_uri}${imgproxy_extension}';
197+
default '/unsafe/${imgproxy_preset}${imgproxy_preset_query}/plain/${origin_server}${origin_uri}${imgproxy_extension}';
180198
~.+ '/unsafe/plain/${origin_server}${origin_uri}';
181199
}
182200

0 commit comments

Comments
 (0)