nginx处理图片模块生成缩略图

nginx docs

安装

1
2
3
4
5
6
7
8
9
10
11
12
13
tar zxvf nginx.tar.gz
cd nginx
yum update
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel gd-devel
./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-threads \
--with-http_image_filter_module
make
make install

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server {
listen 80;
server_name localhost;

location ~ ^/([0-9]+)/(.*)$ {
set $width $1;
set $path $2;
set $EXPIRE_TIME 1d;
set $JPG_QUALITY 95;
rewrite ^ /$path break;
image_filter resize $width -;
image_filter_buffer 100M;
image_filter_jpeg_quality ${JPG_QUALITY};
expires ${EXPIRE_TIME};
}
}

测试

1
http://localhost/100/example.jpg

说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
image_filter off;  
#关闭模块

image_filter test;
#确保图片是jpeg gif png否则返415错误

image_filter size;
#输出有关图像的json格式:如下显示{ "img" : { "width": 100, "height": 100, "type": "gif" } } 出错显示:{}

image_filter rotate 90|180|270;
#旋转指定度数的图像,参数可以包括变量,单独或一起与resize crop一起使用。

image_filter resize width height;
#按比例减少图像到指定大小,公减少一个可以另一个用"-"来表示,出错415,参数值可包含变量,可以与rotate一起使用,则两个一起生效。

image_filter crop width height;
#按比例减少图像比较大的侧面积和另一侧多余的载翦边缘,其它和rotate一样。没太理解

image_filter_buffer 10M;
#设置读取图像缓冲的最大大小,超过则415错误。

image_filter_interlace on;
#如果启用,最终的图像将被交错。对于JPEG,最终的图像将在“渐进式JPEG”格式。

image_filter_jpeg_quality 95;
#设置变换的JPEG图像的期望质量。可接受的值是从1到100的范围内。较小的值通常意味着既降低图像质量,减少传输数据,推荐的最大值为95。参数值可以包含变量。

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

image_filter_transparency on;
#定义是否应该透明转换的GIF图像或PNG图像与调色板中指定的颜色时,可以保留。透明度的损失将导致更好的图像质量。在PNG的Alpha通道总是保留透明度。