Skip to content

Commit b11cb17

Browse files
committed
change scale feature #25
1 parent f0466d6 commit b11cb17

File tree

3 files changed

+21
-32
lines changed

3 files changed

+21
-32
lines changed

README.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,13 @@ image_filter_water_pos [ top-left | top-right | center | bottom-left | bottom-ri
2626
2727
- [x] resize support image scale (enlarge image)
2828
```shell
29-
# similar to "resize", but if the image is smaller than the requested size
30-
# it try to scale the image up to image_filter_ratio_max before it
31-
# perform the resize
32-
image_filter scale width height;
33-
34-
# optional scale max ratio, default 2 or max of 2x the original image size
29+
# optional scale max ratio, default 1
3530
# becareful not to set this too high or it will use too much memory.
3631
#
3732
# For example a 200KB JPEG file (1024x768) will take up 4MB of memory
38-
# when loaded, but when resampled to twice the the size the memory
33+
# when loaded; but when resampled to twice the the size, the memory
3934
# use jumps to 20.1MB
40-
image_filter_ratio_max 3;
35+
image_filter_scale_max 3;
4136
```
4237

4338
# What does this solve?

build/src/ngx_http_image_filter_module.c

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#define NGX_HTTP_IMAGE_ROTATE 5
2121
#define NGX_HTTP_IMAGE_CROP_KEEPX 6
2222
#define NGX_HTTP_IMAGE_CROP_KEEPY 7
23-
#define NGX_HTTP_IMAGE_SCALE 8 // experimental image resize
2423

2524

2625
#define NGX_HTTP_IMAGE_START 0
@@ -57,7 +56,7 @@ typedef struct {
5756
ngx_uint_t sharpen;
5857
ngx_uint_t offset_x;
5958
ngx_uint_t offset_y;
60-
ngx_uint_t ratio_max;
59+
ngx_uint_t scale_max;
6160

6261
ngx_flag_t transparency;
6362
ngx_flag_t interlace;
@@ -91,7 +90,7 @@ typedef struct {
9190
ngx_uint_t offset_x;
9291
ngx_uint_t offset_y;
9392
ngx_uint_t angle;
94-
ngx_uint_t ratio_max;
93+
ngx_uint_t scale_max;
9594

9695
ngx_uint_t phase;
9796
ngx_uint_t type;
@@ -210,11 +209,11 @@ static ngx_command_t ngx_http_image_filter_commands[] = {
210209
0,
211210
NULL },
212211

213-
{ ngx_string("image_filter_ratio_max"),
212+
{ ngx_string("image_filter_scale_max"),
214213
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
215214
ngx_conf_set_num_slot,
216215
NGX_HTTP_LOC_CONF_OFFSET,
217-
offsetof(ngx_http_image_filter_conf_t, ratio_max),
216+
offsetof(ngx_http_image_filter_conf_t, scale_max),
218217
NULL },
219218

220219
{ ngx_string("image_filter_water_image"),
@@ -664,8 +663,8 @@ ngx_http_image_process(ngx_http_request_t *r)
664663
return NULL;
665664
}
666665

667-
// it's ok since we simply center image
668-
if (conf->filter == NGX_HTTP_IMAGE_SCALE) {
666+
// scale would force to resize image
667+
if (conf->scale_max > 1) {
669668
ctx->force = 1;
670669
}
671670

@@ -940,7 +939,7 @@ ngx_http_image_resize(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx)
940939
red, green, blue, t,
941940
offset_x, offset_y;
942941
u_char *out;
943-
double ratio_max, ratio, ratio_h;
942+
double scale_max, ratio, ratio_h;
944943
ngx_buf_t *b;
945944
ngx_uint_t resize;
946945
gdImagePtr src, dst;
@@ -993,18 +992,16 @@ ngx_http_image_resize(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx)
993992
// ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "FUNC resize started \n");
994993

995994
// pre-resize if using scale
996-
if (conf->filter == NGX_HTTP_IMAGE_SCALE) {
997-
conf->filter = NGX_HTTP_IMAGE_RESIZE;
998-
// don't allow bigger than double the size?
999-
ratio_max = (double) conf->ratio_max;
995+
if (conf->scale_max > 1) {
996+
scale_max = (double) conf->scale_max;
1000997
ratio = ((double) ctx->max_width / (double) sx);
1001998
ratio_h = ((double) ctx->max_height / (double) sy);
1002999
if (ratio_h > ratio) {
10031000
ratio = ratio_h;
10041001
}
10051002

1006-
if (ratio > ratio_max) {
1007-
ratio = ratio_max;
1003+
if (ratio > scale_max) {
1004+
ratio = scale_max;
10081005
}
10091006

10101007
//ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "scale ratio = %d, %d \n",
@@ -1587,7 +1584,7 @@ ngx_http_image_filter_create_conf(ngx_conf_t *cf)
15871584
conf->buffer_size = NGX_CONF_UNSET_SIZE;
15881585
conf->offset_x = NGX_CONF_UNSET_UINT;
15891586
conf->offset_y = NGX_CONF_UNSET_UINT;
1590-
conf->ratio_max = NGX_CONF_UNSET_UINT;
1587+
conf->scale_max = NGX_CONF_UNSET_UINT;
15911588

15921589
return conf;
15931590
}
@@ -1672,9 +1669,9 @@ ngx_http_image_filter_merge_conf(ngx_conf_t *cf, void *parent, void *child)
16721669
}
16731670
}
16741671

1675-
if (conf->ratio_max == NGX_CONF_UNSET_UINT) {
1672+
if (conf->scale_max == NGX_CONF_UNSET_UINT) {
16761673
/* 2 is the default max ratio */
1677-
ngx_conf_merge_uint_value(conf->ratio_max, prev->ratio_max, 2);
1674+
ngx_conf_merge_uint_value(conf->scale_max, prev->scale_max, 1);
16781675
}
16791676

16801677
if (conf->output == NULL) {
@@ -1720,8 +1717,7 @@ ngx_http_image_filter(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
17201717

17211718
if (ngx_strcmp(value[i].data, "rotate") == 0) {
17221719
if (imcf->filter != NGX_HTTP_IMAGE_RESIZE
1723-
&& imcf->filter != NGX_HTTP_IMAGE_CROP
1724-
&& imcf->filter != NGX_HTTP_IMAGE_SCALE)
1720+
&& imcf->filter != NGX_HTTP_IMAGE_CROP)
17251721
{
17261722
imcf->filter = NGX_HTTP_IMAGE_ROTATE;
17271723
}
@@ -1768,9 +1764,6 @@ ngx_http_image_filter(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
17681764
} else if (ngx_strcmp(value[i].data, "crop") == 0) {
17691765
imcf->filter = NGX_HTTP_IMAGE_CROP;
17701766

1771-
} else if (ngx_strcmp(value[i].data, "scale") == 0) {
1772-
imcf->filter = NGX_HTTP_IMAGE_SCALE;
1773-
17741767
} else {
17751768
goto failed;
17761769
}

files/etc/nginx/sites-enabled/server.conf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ server {
241241

242242
#image_filter_water_image /app/logo.png;
243243
#image_filter_water_pos center;
244-
# image_filter_ratio_max 3;
244+
image_filter_scale_max 3;
245245

246246
image_filter_sharpen $sharpen;
247247
image_filter_jpeg_quality $quality;
@@ -250,7 +250,7 @@ server {
250250
image_filter rotate $rotate;
251251

252252
# image_filter resize $width $height;
253-
image_filter scale $width $height;
253+
image_filter resize $width $height;
254254
}
255255

256256
location /cmd/crop {
@@ -266,6 +266,7 @@ server {
266266

267267
#image_filter_water_image /app/logo.png;
268268
#image_filter_water_pos center;
269+
image_filter_scale_max 3;
269270

270271
image_filter_sharpen $sharpen;
271272
image_filter_jpeg_quality $quality;

0 commit comments

Comments
 (0)