一、安裝
#yum install gd-devel # #./configure --prefix=/usr/local/nginx \ # --with-debug \ # --with-http_stub_status_module \ # --with-http_ssl_module \ # --with-http_realip_module \ # --with-http_image_filter_module \ # --with-pcre=../pcre-8.21 \ # --add-module=../ngx_devel_kit-0.2.19 \ # --add-module=../lua-nginx-module-0.9.8 \ # --add-module=../echo-nginx-module \ # --add-module=../redis2-nginx-module \ # --add-module=../set-misc-nginx-module
二、配置
location ~* (.*\.(jpg|gif|png))!(.*)!(.*)$ {
set $width $3;
set $height $4;
rewrite "(.*\.(jpg|gif|png))(.*)$" $1;
}
location ~* /image/.*\.(jpg|gif|png)$ {
root /home/jfy/web/;
#image_filter off;
#image_filter test;
#image_filter size;
#image_filter rotate 90;
image_filter resize $width $height;
#image_filter crop 300 200;
image_filter_buffer 10M;
image_filter_interlace on;
image_filter_jpeg_quality 95;
image_filter_sharpen 100;
image_filter_transparency on;
}
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通道總是保留透明度。
三、 幾個規則,可能實用。
匹配全站全部的結尾圖片
---------------------------------------------------------
location ~* \.(jpg|gif|png)$ {
image_filter resize 500 500;
}
---------------------------------------------------------
匹配某個文件夾全部圖片
---------------------------------------------------------
location ~* /image/.*\.(jpg|gif|png)$ {
image_filter resize 500 500;
}
---------------------------------------------------------
再比方用url來指定
---------------------------------------------------------
location ~* (.*\.(jpg|gif|png))!(.*)!(.*)$ {
set $width $3;
set $height $4;
rewrite "(.*\.(jpg|gif|png))(.*)$" $1;
}
location ~* /image/.*\.(jpg|gif|png)$ {
image_filter resize $width $height;
}
---------------------------------------------------------
http://172.16.18.114/image/girl.jpg!300!200
自己主動將原圖縮放為300*200的尺寸
具體效果可參見: http://cwtea.blog.51cto.com/blog/4500217/1333142
