From b11cb1758b6a4b206802184d2c50269ae599882a Mon Sep 17 00:00:00 2001 From: noogen Date: Thu, 25 Apr 2019 14:47:02 -0500 Subject: [PATCH] change scale feature #25 --- README.md | 11 ++----- build/src/ngx_http_image_filter_module.c | 37 +++++++++-------------- files/etc/nginx/sites-enabled/server.conf | 5 +-- 3 files changed, 21 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 60c0390..3cd749a 100644 --- a/README.md +++ b/README.md @@ -26,18 +26,13 @@ image_filter_water_pos [ top-left | top-right | center | bottom-left | bottom-ri - [x] resize support image scale (enlarge image) ```shell -# similar to "resize", but if the image is smaller than the requested size -# it try to scale the image up to image_filter_ratio_max before it -# perform the resize -image_filter scale width height; - -# optional scale max ratio, default 2 or max of 2x the original image size +# optional scale max ratio, default 1 # becareful not to set this too high or it will use too much memory. # # For example a 200KB JPEG file (1024x768) will take up 4MB of memory -# when loaded, but when resampled to twice the the size the memory +# when loaded; but when resampled to twice the the size, the memory # use jumps to 20.1MB -image_filter_ratio_max 3; +image_filter_scale_max 3; ``` # What does this solve? diff --git a/build/src/ngx_http_image_filter_module.c b/build/src/ngx_http_image_filter_module.c index f20946d..13b2bc3 100644 --- a/build/src/ngx_http_image_filter_module.c +++ b/build/src/ngx_http_image_filter_module.c @@ -20,7 +20,6 @@ #define NGX_HTTP_IMAGE_ROTATE 5 #define NGX_HTTP_IMAGE_CROP_KEEPX 6 #define NGX_HTTP_IMAGE_CROP_KEEPY 7 -#define NGX_HTTP_IMAGE_SCALE 8 // experimental image resize #define NGX_HTTP_IMAGE_START 0 @@ -57,7 +56,7 @@ typedef struct { ngx_uint_t sharpen; ngx_uint_t offset_x; ngx_uint_t offset_y; - ngx_uint_t ratio_max; + ngx_uint_t scale_max; ngx_flag_t transparency; ngx_flag_t interlace; @@ -91,7 +90,7 @@ typedef struct { ngx_uint_t offset_x; ngx_uint_t offset_y; ngx_uint_t angle; - ngx_uint_t ratio_max; + ngx_uint_t scale_max; ngx_uint_t phase; ngx_uint_t type; @@ -210,11 +209,11 @@ static ngx_command_t ngx_http_image_filter_commands[] = { 0, NULL }, - { ngx_string("image_filter_ratio_max"), + { ngx_string("image_filter_scale_max"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, ngx_conf_set_num_slot, NGX_HTTP_LOC_CONF_OFFSET, - offsetof(ngx_http_image_filter_conf_t, ratio_max), + offsetof(ngx_http_image_filter_conf_t, scale_max), NULL }, { ngx_string("image_filter_water_image"), @@ -664,8 +663,8 @@ ngx_http_image_process(ngx_http_request_t *r) return NULL; } - // it's ok since we simply center image - if (conf->filter == NGX_HTTP_IMAGE_SCALE) { + // scale would force to resize image + if (conf->scale_max > 1) { ctx->force = 1; } @@ -940,7 +939,7 @@ ngx_http_image_resize(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx) red, green, blue, t, offset_x, offset_y; u_char *out; - double ratio_max, ratio, ratio_h; + double scale_max, ratio, ratio_h; ngx_buf_t *b; ngx_uint_t resize; gdImagePtr src, dst; @@ -993,18 +992,16 @@ ngx_http_image_resize(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx) // ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "FUNC resize started \n"); // pre-resize if using scale - if (conf->filter == NGX_HTTP_IMAGE_SCALE) { - conf->filter = NGX_HTTP_IMAGE_RESIZE; - // don't allow bigger than double the size? - ratio_max = (double) conf->ratio_max; + if (conf->scale_max > 1) { + scale_max = (double) conf->scale_max; ratio = ((double) ctx->max_width / (double) sx); ratio_h = ((double) ctx->max_height / (double) sy); if (ratio_h > ratio) { ratio = ratio_h; } - if (ratio > ratio_max) { - ratio = ratio_max; + if (ratio > scale_max) { + ratio = scale_max; } //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) conf->buffer_size = NGX_CONF_UNSET_SIZE; conf->offset_x = NGX_CONF_UNSET_UINT; conf->offset_y = NGX_CONF_UNSET_UINT; - conf->ratio_max = NGX_CONF_UNSET_UINT; + conf->scale_max = NGX_CONF_UNSET_UINT; return conf; } @@ -1672,9 +1669,9 @@ ngx_http_image_filter_merge_conf(ngx_conf_t *cf, void *parent, void *child) } } - if (conf->ratio_max == NGX_CONF_UNSET_UINT) { + if (conf->scale_max == NGX_CONF_UNSET_UINT) { /* 2 is the default max ratio */ - ngx_conf_merge_uint_value(conf->ratio_max, prev->ratio_max, 2); + ngx_conf_merge_uint_value(conf->scale_max, prev->scale_max, 1); } if (conf->output == NULL) { @@ -1720,8 +1717,7 @@ ngx_http_image_filter(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) if (ngx_strcmp(value[i].data, "rotate") == 0) { if (imcf->filter != NGX_HTTP_IMAGE_RESIZE - && imcf->filter != NGX_HTTP_IMAGE_CROP - && imcf->filter != NGX_HTTP_IMAGE_SCALE) + && imcf->filter != NGX_HTTP_IMAGE_CROP) { imcf->filter = NGX_HTTP_IMAGE_ROTATE; } @@ -1768,9 +1764,6 @@ ngx_http_image_filter(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) } else if (ngx_strcmp(value[i].data, "crop") == 0) { imcf->filter = NGX_HTTP_IMAGE_CROP; - } else if (ngx_strcmp(value[i].data, "scale") == 0) { - imcf->filter = NGX_HTTP_IMAGE_SCALE; - } else { goto failed; } diff --git a/files/etc/nginx/sites-enabled/server.conf b/files/etc/nginx/sites-enabled/server.conf index d9afdf4..f962fbf 100644 --- a/files/etc/nginx/sites-enabled/server.conf +++ b/files/etc/nginx/sites-enabled/server.conf @@ -241,7 +241,7 @@ server { #image_filter_water_image /app/logo.png; #image_filter_water_pos center; - # image_filter_ratio_max 3; + image_filter_scale_max 3; image_filter_sharpen $sharpen; image_filter_jpeg_quality $quality; @@ -250,7 +250,7 @@ server { image_filter rotate $rotate; # image_filter resize $width $height; - image_filter scale $width $height; + image_filter resize $width $height; } location /cmd/crop { @@ -266,6 +266,7 @@ server { #image_filter_water_image /app/logo.png; #image_filter_water_pos center; + image_filter_scale_max 3; image_filter_sharpen $sharpen; image_filter_jpeg_quality $quality;