原文:http://blog.csdn.net/jiao_fuyou/article/details/37598441
一、安裝
#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