Skip to content

Commit a901c3e

Browse files
committed
fix issue where it always enlarge when scale_max is used with no other size value
1 parent 0f2e4ec commit a901c3e

File tree

2 files changed

+58
-57
lines changed

2 files changed

+58
-57
lines changed

build/src/ngx_http_image_filter_module.c

Lines changed: 56 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -936,10 +936,10 @@ ngx_http_image_resize(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx)
936936
{
937937
int sx, sy, dx, dy, ox, oy, ax, ay, size,
938938
colors, palette, transparent, sharpen,
939-
red, green, blue, t,
939+
red, green, blue, t, scale_max,
940940
offset_x, offset_y;
941941
u_char *out;
942-
double scale_max, ratio, ratio_h;
942+
double ratio, ratio_h;
943943
ngx_buf_t *b;
944944
ngx_uint_t resize;
945945
gdImagePtr src, dst;
@@ -952,10 +952,11 @@ ngx_http_image_resize(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx)
952952
return NULL;
953953
}
954954

955-
sx = gdImageSX(src);
956-
sy = gdImageSY(src);
957-
958-
conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module);
955+
sx = gdImageSX(src);
956+
sy = gdImageSY(src);
957+
conf = ngx_http_get_module_loc_conf(r, ngx_http_image_filter_module);
958+
scale_max = (int) conf->scale_max;
959+
ratio = 1;
959960

960961
if (!ctx->force
961962
&& ctx->angle == 0
@@ -989,61 +990,61 @@ ngx_http_image_resize(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx)
989990

990991
transparent:
991992

992-
// ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "FUNC resize started \n");
993+
if ((int)ctx->max_width > 0) {
994+
ratio = ((double) ctx->max_width / (double) sx);
995+
}
993996

994-
// pre-resize if using scale
995-
if (conf->scale_max > 1) {
996-
scale_max = (double) conf->scale_max;
997-
ratio = ((double) ctx->max_width / (double) sx);
998-
ratio_h = ((double) ctx->max_height / (double) sy);
999-
if (ratio_h > ratio) {
1000-
ratio = ratio_h;
1001-
}
997+
if ((int)ctx->max_height > 0) {
998+
ratio_h = ((double) ctx->max_height / (double) sy);
999+
if (ratio_h > ratio) {
1000+
ratio = ratio_h;
1001+
}
1002+
}
10021003

1003-
if (ratio > scale_max) {
1004-
ratio = scale_max;
1004+
// pre-resize if using scale and required a larger image
1005+
if (scale_max > 1) {
1006+
if (ratio > 1) {
1007+
// ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "scale max = %d, %d \n", scale_max, scale_max);
1008+
// ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "scale ratio = %d, %d \n", ratio, ratio);
1009+
1010+
if (ratio > (double) scale_max) {
1011+
ratio = (double) scale_max;
10051012
}
10061013

1007-
//ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "scale ratio = %d, %d \n",
1008-
// ratio, ratio);
1014+
/*
1015+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "width max = %d, %d \n", ctx->max_width, ctx->max_width);
1016+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "width img = %d, %d \n", sx, sx);
1017+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "height max = %d, %d \n", ctx->max_height, ctx->max_height);
1018+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "height img = %d, %d \n", sy, sy);
1019+
*/
1020+
1021+
// ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "scale %d, %d \n", ratio, ratio);
1022+
dst = ngx_http_image_new(r, sx * ratio, sy * ratio, palette);
10091023

1010-
//ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "scale mw = %d, %d \n",
1011-
// ctx->max_width, ctx->max_width);
1012-
1013-
//ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "scale mw = %d, %d \n",
1014-
// sx, sx);
1015-
1016-
// if source is smaller, enlarge it
1017-
// resize to smaller can be handled later
1018-
if (ratio > 1) {
1019-
// ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "scale %d, %d \n", ratio, ratio);
1020-
dst = ngx_http_image_new(r, sx * ratio, sy * ratio, palette);
1021-
1022-
if (dst == NULL) {
1023-
gdImageDestroy(src);
1024-
return NULL;
1025-
}
1026-
1027-
if (transparent == -1) {
1028-
gdImageSaveAlpha(src, 1);
1029-
gdImageColorTransparent(src, -1);
1030-
1031-
if(colors == 0)
1032-
{
1033-
gdImageAlphaBlending(dst, 0);
1034-
gdImageSaveAlpha(dst, 1);
1035-
} else {
1036-
gdImageTrueColorToPalette(dst, 1, 256);
1037-
}
1038-
}
1039-
1040-
my_resize(src, dst);
1041-
// set the new original
1042-
gdImageDestroy(src);
1043-
src = dst;
1044-
sx = gdImageSX(src);
1045-
sy = gdImageSY(src);
1024+
if (dst == NULL) {
1025+
gdImageDestroy(src);
1026+
return NULL;
10461027
}
1028+
1029+
if (transparent == -1) {
1030+
gdImageSaveAlpha(src, 1);
1031+
gdImageColorTransparent(src, -1);
1032+
1033+
if(colors == 0) {
1034+
gdImageAlphaBlending(dst, 0);
1035+
gdImageSaveAlpha(dst, 1);
1036+
} else {
1037+
gdImageTrueColorToPalette(dst, 1, 256);
1038+
}
1039+
}
1040+
1041+
my_resize(src, dst);
1042+
// set the new original
1043+
gdImageDestroy(src);
1044+
src = dst;
1045+
sx = gdImageSX(src);
1046+
sy = gdImageSY(src);
1047+
}
10471048
}
10481049

10491050
gdImageColorTransparent(src, -1);

build/src/ubuntu.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
export NGINX_BUILD_DIR=/usr/src/nginx/nginx-${NGINX_VERSION}
44
cd /tmp
55

6-
apt-get update && apt-get upgrade -y --no-install-recommends --no-install-suggests
7-
apt-get install -y --no-install-recommends --no-install-suggests curl unzip apt-transport-https \
6+
apt-get update
7+
apt-get install -y --no-install-recommends --no-install-suggests curl apt-transport-https \
88
apt-utils software-properties-common build-essential ca-certificates libssl-dev \
99
zlib1g-dev dpkg-dev libpcre3 libpcre3-dev libgd-dev gpg-agent
1010

0 commit comments

Comments
 (0)