Ticket #2454: image_filter_scale.patch

File image_filter_scale.patch, 3.5 KB (added by jilles-sg@…, 3 years ago)

proof of concept patch to use gdImageScale

  • src/http/modules/ngx_http_image_filter_module.c

    old new typedef struct {  
    4848
    4949    ngx_flag_t                   transparency;
    5050    ngx_flag_t                   interlace;
     51    ngx_flag_t                   use_scale_function;
    5152
    5253    ngx_http_complex_value_t    *wcv;
    5354    ngx_http_complex_value_t    *hcv;
    … … static ngx_command_t ngx_http_image_fil  
    163164      offsetof(ngx_http_image_filter_conf_t, interlace),
    164165      NULL },
    165166
     167    { ngx_string("image_filter_use_scale_function"),
     168      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
     169      ngx_conf_set_flag_slot,
     170      NGX_HTTP_LOC_CONF_OFFSET,
     171      offsetof(ngx_http_image_filter_conf_t, use_scale_function),
     172      NULL },
     173
    166174    { ngx_string("image_filter_buffer"),
    167175      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
    168176      ngx_conf_set_size_slot,
    … … ngx_http_image_resize(ngx_http_request_t  
    851859
    852860    colors = gdImageColorsTotal(src);
    853861
    854     if (colors && conf->transparency) {
     862    if (colors && !conf->use_scale_function && conf->transparency) {
    855863        transparent = gdImageGetTransparent(src);
    856864
    857865        if (transparent != -1) {
    … … ngx_http_image_resize(ngx_http_request_t  
    872880
    873881transparent:
    874882
    875     gdImageColorTransparent(src, -1);
     883    if (!conf->use_scale_function) {
     884        gdImageColorTransparent(src, -1);
     885    }
    876886
    877887    dx = sx;
    878888    dy = sy;
    … … transparent:  
    919929        }
    920930    }
    921931
    922     if (resize) {
     932    if (resize && conf->use_scale_function) {
     933        gdImageSetInterpolationMethod(src, GD_BICUBIC);
     934        dst = gdImageScale(src, dx, dy);
     935        if (dst == NULL) {
     936            gdImageDestroy(src);
     937            return NULL;
     938        }
     939        gdImageDestroy(src);
     940    } else if (resize) {
    923941        dst = ngx_http_image_new(r, dx, dy, palette);
    924942        if (dst == NULL) {
    925943            gdImageDestroy(src);
    … … transparent:  
    10231041                           "image crop: %d x %d @ %d x %d",
    10241042                           dx, dy, ox, oy);
    10251043
    1026             if (colors == 0) {
     1044            if (!conf->use_scale_function && colors == 0) {
    10271045                gdImageSaveAlpha(dst, 1);
    10281046                gdImageAlphaBlending(dst, 0);
    10291047            }
    10301048
    10311049            gdImageCopy(dst, src, 0, 0, ox, oy, dx - ox, dy - oy);
    10321050
    1033             if (colors) {
     1051            if (!conf->use_scale_function && colors) {
    10341052                gdImageTrueColorToPalette(dst, 1, 256);
    10351053            }
    10361054
    … … transparent:  
    10381056        }
    10391057    }
    10401058
    1041     if (transparent != -1 && colors) {
     1059    if (!conf->use_scale_function && transparent != -1 && colors) {
    10421060        gdImageColorTransparent(dst, gdImageColorExact(dst, red, green, blue));
    10431061    }
    10441062
    … … ngx_http_image_filter_create_conf(ngx_co  
    13011319    conf->sharpen = NGX_CONF_UNSET_UINT;
    13021320    conf->transparency = NGX_CONF_UNSET;
    13031321    conf->interlace = NGX_CONF_UNSET;
     1322    conf->use_scale_function = NGX_CONF_UNSET;
    13041323    conf->buffer_size = NGX_CONF_UNSET_SIZE;
    13051324
    13061325    return conf;
    … … ngx_http_image_filter_merge_conf(ngx_con  
    13611380
    13621381    ngx_conf_merge_value(conf->interlace, prev->interlace, 0);
    13631382
     1383    ngx_conf_merge_value(conf->use_scale_function, prev->use_scale_function, 0);
     1384
    13641385    ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size,
    13651386                              1 * 1024 * 1024);
    13661387