Skip to content

Commit f0466d6

Browse files
committed
account for height when scale
1 parent 6c4aef4 commit f0466d6

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

build/src/ngx_http_image_filter_module.c

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ ngx_http_image_resize(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx)
940940
red, green, blue, t,
941941
offset_x, offset_y;
942942
u_char *out;
943-
double ratio_max, ratio;
943+
double ratio_max, ratio, ratio_h;
944944
ngx_buf_t *b;
945945
ngx_uint_t resize;
946946
gdImagePtr src, dst;
@@ -992,55 +992,63 @@ ngx_http_image_resize(ngx_http_request_t *r, ngx_http_image_filter_ctx_t *ctx)
992992

993993
// ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "FUNC resize started \n");
994994

995-
// pre-resize if using method2
995+
// pre-resize if using scale
996996
if (conf->filter == NGX_HTTP_IMAGE_SCALE) {
997997
conf->filter = NGX_HTTP_IMAGE_RESIZE;
998998
// don't allow bigger than double the size?
999999
ratio_max = (double) conf->ratio_max;
10001000
ratio = ((double) ctx->max_width / (double) sx);
1001+
ratio_h = ((double) ctx->max_height / (double) sy);
1002+
if (ratio_h > ratio) {
1003+
ratio = ratio_h;
1004+
}
1005+
10011006
if (ratio > ratio_max) {
10021007
ratio = ratio_max;
10031008
}
10041009

1005-
//ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "resize2 ratio = %d, %d \n",
1010+
//ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "scale ratio = %d, %d \n",
10061011
// ratio, ratio);
10071012

1008-
//ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "resize2 mw = %d, %d \n",
1013+
//ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "scale mw = %d, %d \n",
10091014
// ctx->max_width, ctx->max_width);
10101015

1011-
//ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "resize2 mw = %d, %d \n",
1016+
//ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "scale mw = %d, %d \n",
10121017
// sx, sx);
10131018

10141019
// if source is smaller, enlarge it
10151020
// resize to smaller can be handled later
10161021
if (ratio > 1) {
1017-
// ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "FUNC resize2 here %d, %d \n", ratio, ratio);
1018-
1019-
dst = gdImageCreateTrueColor(sx * ratio, sy * ratio);
1022+
// ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "scale %d, %d \n", ratio, ratio);
1023+
dst = ngx_http_image_new(r, sx * ratio, sy * ratio, palette);
1024+
1025+
if (dst == NULL) {
1026+
gdImageDestroy(src);
1027+
return NULL;
1028+
}
10201029

10211030
if (transparent == -1) {
10221031
gdImageSaveAlpha(src, 1);
10231032
gdImageColorTransparent(src, -1);
10241033

10251034
if(colors == 0)
10261035
{
1027-
gdImageAlphaBlending(dst, 0);
1028-
gdImageSaveAlpha(dst, 1);
1036+
gdImageAlphaBlending(dst, 0);
1037+
gdImageSaveAlpha(dst, 1);
10291038
} else {
1030-
gdImageTrueColorToPalette(dst, 1, 256);
1039+
gdImageTrueColorToPalette(dst, 1, 256);
10311040
}
10321041
}
10331042

10341043
my_resize(src, dst);
10351044
// set the new original
10361045
gdImageDestroy(src);
10371046
src = dst;
1047+
sx = gdImageSX(src);
1048+
sy = gdImageSY(src);
10381049
}
10391050
}
10401051

1041-
sx = gdImageSX(src);
1042-
sy = gdImageSY(src);
1043-
10441052
gdImageColorTransparent(src, -1);
10451053

10461054
dx = sx;

0 commit comments

Comments
 (0)