模块 ngx_http_image_filter_module

配置示例
指令
     image_filter
     image_filter_buffer
     image_filter_interlace
     image_filter_jpeg_quality
     image_filter_sharpen
     image_filter_transparency
     image_filter_webp_quality

ngx_http_image_filter_module 模块 (0.7.54+) 是一个用于转换 JPEG、GIF、PNG 和 WebP 格式图片的过滤器。

该模块默认不会构建,需要通过配置参数 --with-http_image_filter_module 来启用。

该模块利用 libgd 库。建议使用该库的最新可用版本。

WebP 格式的支持出现在 1.11.6 版本中。要转换此格式的图片,libgd 库必须编译时启用 WebP 支持。

配置示例

location /img/ {
    proxy_pass   http://backend;
    image_filter resize 150 100;
    image_filter rotate 90;
    error_page   415 = /empty;
}

location = /empty {
    empty_gif;
}

指令

语法 image_filter off;
image_filter test;
image_filter size;
image_filter rotate 90 | 180 | 270;
image_filter resize width height;
image_filter crop width height;
默认值
image_filter off;
上下文 location

设置对图片执行的转换类型

off
在当前 location 中关闭模块处理。
test
确保响应是 JPEG、GIF、PNG 或 WebP 格式的图片。否则,返回 415 (不支持的媒体类型) 错误。
size
以 JSON 格式输出图片信息,例如
{ "img" : { "width": 100, "height": 100, "type": "gif" } }
如果发生错误,输出如下
{}
rotate 90|180|270
按指定的度数逆时针旋转图片。参数值可以包含变量。此模式可单独使用,也可与 resizecrop 转换一起使用。
resize width height
按比例将图片缩小到指定尺寸。若仅按一个维度缩小,另一个维度可以指定为 “-”。如果发生错误,服务器将返回代码 415 (不支持的媒体类型)。参数值可以包含变量。与 rotate 参数一起使用时,旋转发生在缩小之后
crop width height
按比例将图片缩小到较大一边的尺寸,并裁剪掉另一边的多余边缘。若仅按一个维度缩小,另一个维度可以指定为 “-”。如果发生错误,服务器将返回代码 415 (不支持的媒体类型)。参数值可以包含变量。与 rotate 参数一起使用时,旋转发生在缩小之前

语法 image_filter_buffer size;
默认值
image_filter_buffer 1M;
上下文 http, server, location

设置用于读取图片的最大缓冲区大小。当大小超出时,服务器返回错误 415 (不支持的媒体类型)。

语法 image_filter_interlace on | off;
默认值
image_filter_interlace off;
上下文 http, server, location

此指令出现在 1.3.15 版本中。

如果启用,最终图片将采用隔行扫描。对于 JPEG,最终图片将采用“渐进式 JPEG”格式。

语法 image_filter_jpeg_quality quality;
默认值
image_filter_jpeg_quality 75;
上下文 http, server, location

设置转换后的 JPEG 图片的期望质量。可接受的值范围为 1 到 100。较小的值通常意味着较低的图片质量和更少的数据传输。最大推荐值为 95。参数值可以包含变量。

语法 image_filter_sharpen percent;
默认值
image_filter_sharpen 0;
上下文 http, server, location

增加最终图片的锐度。锐度百分比可以超过 100。零值禁用锐度。参数值可以包含变量。

语法 image_filter_transparency on|off;
默认值
image_filter_transparency on;
上下文 http, server, location

定义转换 GIF 图片或使用调色板指定颜色的 PNG 图片时是否应保留透明度。丢失透明度会得到更高质量的图片。PNG 中的 alpha 通道透明度始终保留。

语法 image_filter_webp_quality quality;
默认值
image_filter_webp_quality 80;
上下文 http, server, location

此指令出现在 1.11.6 版本中。

设置转换后的 WebP 图片的期望质量。可接受的值范围为 1 到 100。较小的值通常意味着较低的图片质量和更少的数据传输。参数值可以包含变量。