[patch] ngx_http_image_filter_module incorrectly calculates size

Maxim Bublis b at codemonkey.ru
Fri Apr 20 11:25:07 UTC 2012


Hi!

>
> You patch just moves processing from one code path to another in a
> case where nginx (due to use of fixed-point calculations) doesn't
> see the difference between original image aspect ratio (~ 1.773)
> and desired resulting image aspect ratio (~ 1.778).  Normally both
> code paths should result in identical images matching desired
> sizes, but in the case in question this doesn't happen due to
> rounding effects.  With your patch the problem goes away for a
> particular image, but will instead appear in other cases.

My original patch fixes problem for resizing image with crop from
360x203 to 304x171 but in the same time breaks calculations for image
with original dimensions with 203x360 and resizing it with crop to
171x304 (i.e. height of original image is greater that it's width).
Thanks for pointing to that.

>
> Something like this should be correct solution to eliminate the
> problem:
>
> --- a/src/http/modules/ngx_http_image_filter_module.c
> +++ b/src/http/modules/ngx_http_image_filter_module.c
> @@ -817,9 +817,7 @@ transparent:
>
>         resize = 0;
>
> -        if ((ngx_uint_t) (dx * 100 / dy)
> -            < ctx->max_width * 100 / ctx->max_height)
> -        {
> +        if (dx * ctx->max_height < dy * ctx->max_width) {
>             if ((ngx_uint_t) dx > ctx->max_width) {
>                 dy = dy * ctx->max_width / dx;
>                 dy = dy ? dy : 1;

I think it should be correct solution if multiplication of either dx
and ctx->max_height or dy and ctx->max_width would not result in
integer overflow in case when both dx and ctx->max_height or dy and
ctx->max_height are rather large integers.

-- 
Maxim Bublis



More information about the nginx-devel mailing list