Module ngx_http_image_filter_module

ngx_http_image_filter_module模块(0.7.54)是一个过滤器,可转换 JPEG,GIF,PNG 和 WebP 格式的图像。

默认情况下未构建此模块,应使用--with-http_image_filter_module配置参数启用它。

Note

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

Note

WebP 格式支持出现在 1.11.6 版中。要以这种格式转换图像,必须在 WebP 支持下编译libgd库。

Example Configuration

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

location = /empty {
    empty_gif;
}

Directives

Syntax: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

    • 关闭周围位置的模块处理。
  • 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参数一起使用时,旋转会在还原之前进行。

Syntax:image_filter_buffer size;
Default:image_filter_buffer 1M;
Context:http , server , location

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

Syntax:image_filter_interlace on | off;
Default:image_filter_interlace off;
Context:http , server , location

该指令出现在 1.3.15 版中。

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

Syntax:image_filter_jpeg_quality quality;
Default:image_filter_jpeg_quality 75;
Context:http , server , location

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

Syntax:image_filter_sharpen percent;
Default:image_filter_sharpen 0;
Context:http , server , location

增加最终图像的清晰度。锐度百分比可以超过 100.零值将禁用锐化。参数值可以包含变量。

Syntax:image_filter_transparency on|off;
Default:image_filter_transparency on;
Context:http , server , location

定义在转换由调色板指定的颜色的 GIF 图像或 PNG 图像时是否应保留透明度。透明度的损失导致图像质量更好。始终保留 PNG 中的 Alpha 通道透明度。

Syntax:image_filter_webp_quality quality;
Default:image_filter_webp_quality 80;
Context:http , server , location

该指令出现在 1.11.6 版本中。

设置所需的quality转换后的 WebP 图像。可接受的值范围是 1 到 100.较小的值通常意味着较低的图像质量和较少的要传输的数据。参数值可以包含变量。