ngx_http_image_filter_module

Resize, crop and rotate JPEG, GIF, PNG and WebP responses

dynamic moduleBSD-2-Clausesource docs

Overview

ngx_http_image_filter_module is a filter that transforms images in JPEG, GIF, PNG and WebP formats. The image_filter directive selects the operation: test the response type, report the size as JSON, rotate by 90, 180 or 270 degrees, or proportionally resize or crop to a given width and height.

The module uses the libgd library, and WebP support (1.11.6) needs libgd compiled with WebP. Images are read into a buffer of image_filter_buffer size, 1M by default, and anything larger or not an image gets a 415 error. Rotation happens after a resize but before a crop, and the rotate, quality and sharpen values may contain variables.

It is an official nginx module that upstream does not build by default, so it needs the --with-http_image_filter_module configure parameter. The n.wtf packages build it as a dynamic module shipped in libnginx-mod-http-image-filter, typically to generate thumbnails from a proxied or local image store on the fly.

Key directives

image_filteroff, test, size, rotate 90|180|270, resize width height or crop width height; default off, location context.
image_filter_bufferMaximum buffer for reading an image, default 1M; larger images return 415.
image_filter_jpeg_qualityQuality of transformed JPEG images, 1 to 100, default 75; 95 is the recommended maximum.
image_filter_webp_qualityQuality of transformed WebP images, 1 to 100, default 80 (1.11.6).
image_filter_interlaceEmit interlaced output, progressive JPEG for JPEG, default off (1.3.15).
image_filter_transparencyPreserve palette transparency in GIF and PNG, default on; PNG alpha channels are always kept.

Example

nginx.conf
location /img/ { proxy_pass http://backend; image_filter resize 150 100; image_filter rotate 90; image_filter_jpeg_quality 85; image_filter_buffer 2M; # non-image or oversized responses end up here error_page 415 = /empty; } location = /empty { empty_gif; }

Availability

Ships as a dynamic module in libnginx-mod-http-image-filter, installed with nginx-extras. The package drops a load_module config into /etc/nginx/modules-enabled/, so it loads on start.