Skip to content

Commit

Permalink
change scale feature #25
Browse files Browse the repository at this point in the history
  • Loading branch information
noogen committed Apr 25, 2019
1 parent f0466d6 commit b11cb17
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 32 deletions.
11 changes: 3 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
37 changes: 15 additions & 22 deletions build/src/ngx_http_image_filter_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions files/etc/nginx/sites-enabled/server.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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;
Expand Down

0 comments on commit b11cb17

Please sign in to comment.