本文主要针对nginx的ngx_http_image_filter_module 模块做简单介绍,本文具体包括如下指image_filter,image_filter_buffer,image_filter_jpeg_quality,image_filter_sharpen,image_filter_transparency
ngx_http_image_filter_module模块是一个用于在JPEG, GIF, 和PNG 图片格式之间进行转换的过滤模块(nginx的一个术语)。
这个模块不是nginx内建模块,需要通过–with-http_image_filter_module指令进行进行开启相关功能。
这个模块使用libgd库, 推荐采用最新版本的库。
配置示例如下:
location /img/ {
proxy_pass http://backend;
image_filter resize 150 100;
image_filter rotate 90;
error_page 415 = /empty;
}
location = /empty {
empty_gif;
}
Nginx原文:
The ngx_http_image_filter_module module (0.7.54+) is a filter that transforms images in JPEG, GIF, and PNG formats.
This module is not built by default, it should be enabled with the –with-http_image_filter_module configuration parameter.
This module utilizes the libgd library. It is recommended to use the latest available version of the library; it is version 2.0.35 as of this writing.
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;
}
1. image_filter
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;
|
default:
|
image_filter off;
|
context:
|
location
|
Off: 关闭一个访问类的图片变换处理功能(一般是一个nginx配置的location)
Test:确认在返回给用户的相应图片是JPEG, GIF, or PNG 格式的, 否则报415 错误(不支持的媒体文件类型)
Size: 采用json格式输出信息,例如:
{ "img" : { "width": 100, "height": 100, "type": "gif" } }
如果发生错误则输出如下
{}
rotate 90|180|270
逆时针旋转图像到设定的度数,这个值可以包括变量(配置时是个变量, 到执行具体这个条指令是, 变量需要实例化成具体的值),指令可以单独使用,也可以同resize 和crop 变换结合使用
调整高度和宽度(resize width height)
保持比例(原意是按照比例, 但是根据推测,这里的图片缩小等一定会保持原始图片的显示比例, 然后根据设定的宽度或者高度进行相关的缩放, 否则一定带来图片的比例失真,用户体验会比较差)的缩减图像到指定的尺寸。通常为了缩减等, 可以仅仅使用一个维度的设置(例如仅仅指定,宽度或者高度), 被省略的维度可以用“-”符号替代,在这种情况下, 若是发生错误,则给用户反馈一个415的错误代码(不支持的媒体类型)。这个指令的值可以是一个变量。当使用时伴随一个rotate参数,则缩放完成后进行旋转操作。
裁剪图像到指定的宽高(crop width height)
按照比例将图片调整到合适大小(最后一个维度达到设定值后停止变换, 然后裁剪掉超出的部分。 同上面指令区别是,上面是第一维度达到设定值后,停止变换, 然后将其他地方用背景或者设定的留白填充(我自己推测的, 没去测试))。通常为了缩减等操作, 可以仅仅使用一个维度的设置(例如仅仅指定,宽度或者高度), 被省略的维度可以用“-”符号替代,在这种情况下, 若是发生错误,则给用户反馈一个415的错误代码(不支持的媒体类型)。这个指令的值可以是一个变量。当使用时伴随一个rotate参数,则缩放完成后进行旋转操作。
Nginx原文:
Sets the type of transformation to perform on images:
off
turns off module processing in a surrounding location.
test
ensures that responses are images in either JPEG, GIF, or PNG format. Otherwise, the error 415 (Unsupported Media Type) is returned.
size
outputs information about images in a JSON format, e.g.:
{ "img" : { "width": 100, "height": 100, "type": "gif" } }
In case of an error, the following is output:
{}
rotate 90|180|270
rotates images counter-clockwise by the specified number of degrees. Value of the parameter can contain variables. Can be used either alone, or along with the resize and crop transformations.
resize width height
proportionally reduces an image to the specified sizes. To reduce by only one dimension, another dimension can be specified as “-”. In case of an error, the server will return code 415 (Unsupported Media Type). Values of parameters can contain variables. When used along with the rotate parameter, the rotation happens after reduction.
crop width height
proportionally reduces an image to the size of the largest side and crops extraneous edges by another side. To reduce by only one dimension, another dimension can be specified as “-”. In case of an error, the server will return code 415 (Unsupported Media Type). Values of parameters can contain variables. When used along with the rotate parameter, the rotation happens before reduction.
2. image_filter_buffer
syntax:
|
image_filter_buffer size;
|
default:
|
image_filter_buffer 1M;
|
context:
|
http, server, location
|
Nginx原文:
Sets the maximum size of the buffer used for reading images. When a size is exceeded the server will return error 415 (Unsupported Media Type).
3. image_filter_jpeg_quality
syntax:
|
image_filter_jpeg_quality quality;
|
default:
|
image_filter_jpeg_quality 75;
|
context:
|
http, server, location
|
Nginx原文:
Sets the desired quality of the transformed JPEG images. Acceptable values are in the 1..100 range. Lesser values usually imply both lower image quality and less data to transfer. The maximum recommended value is 95. Value of the parameter can contain variables.
4. image_filter_sharpen
syntax:
|
image_filter_sharpen percent;
|
default:
|
image_filter_sharpen 0;
|
context:
|
http, server, location
|
Nginx原文:
Increases sharpness of the final image. The sharpness percentage can exceed 100. The value of 0 disables sharpening. Value of the parameter can contain variables.
5. image_filter_transparency
syntax:
|
image_filter_transparency on|off;
|
default:
|
image_filter_transparency on;
|
context:
|
http, server
|
Nginx原文:
Defines whether transparency should be preserved when transforming PNG images with colors specified by a palette, or in GIF images. The loss of transparency allows to obtain images of a better quality. The alpha channel transparency in PNG is always preserved.
声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。