Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resize and crop by any single dimension #11

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ image_filter watermark;

image_filter_watermark_width_from 300;
image_filter_watermark_height_from 400;

image_filter_watermark "PATH_TO_FILE";
image_filter_watermark_position center-center; # top-left|top-right|bottom-right|bottom-left|right-center|left-center|bottom-center|top-center|center-center|center-random`
```
Expand Down Expand Up @@ -77,3 +77,43 @@ Usage with resize and crop:
image_filter_watermark_position center-center;
}
```

Resize and crop can omit any dimension value

* For example, nginx locations configuration:

```
location /resize/ {
alias /path/to/background/image;

image_filter resize $arg_w $arg_h;

image_filter_watermark /path/to/watermark/image;
image_filter_watermark_position center-center;
}

location /crop/ {
alias /path/to/background/image;

image_filter crop $arg_w $arg_h;

image_filter_watermark /path/to/watermark/image;
image_filter_watermark_position center-center;
}
```

* Requests with args:

1. for proportionally reduces an image to the specified width

```
http://localhost/resize/test.png?w=200
http://localhost/resize/test.png?w=200&h=-
```

2. for proportionally reduces an image to the width size and crops by height

```
http://localhost/crop/test.png?h=250
http://localhost/crop/tets.png?w=-&h=250
```
30 changes: 14 additions & 16 deletions ngx_http_image_filter_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -621,27 +621,25 @@ ngx_http_image_process(ngx_http_request_t *r)
return ngx_http_image_resize(r, ctx);
}
}
ctx->max_width = ngx_http_image_filter_get_value(r, conf->wcv, conf->width);

if (ctx->max_width == 0) {
return NULL;
}
ctx->max_width = ngx_http_image_filter_get_value(
r,
conf->wcv,
conf->width
);

ctx->max_height = ngx_http_image_filter_get_value(r, conf->hcv,
conf->height);
if (ctx->max_height == 0) {
ctx->max_height = ngx_http_image_filter_get_value(
r,
conf->hcv,
conf->height
);

if (ctx->max_width == 0 && ctx->max_height == 0) {
return NULL;
}

if (rc == NGX_OK
&& ctx->width <= ctx->max_width
&& ctx->height <= ctx->max_height
&& ctx->angle == 0
&& !ctx->force
&& !conf->watermark.data)
{
return ngx_http_image_asis(r, ctx);
}
if(ctx->max_width == 0) ctx->max_width =ctx->width;
if(ctx->max_height == 0) ctx->max_height=ctx->height;

return ngx_http_image_resize(r, ctx);
}
Expand Down