Nginx之http_image_filter_module模塊使用

一铸磅、安裝

yum install gd-devel #安裝gd庫(kù)

檢查是否已安裝過刁赦,我是用的寶塔編譯默認(rèn)安裝的nginx1.21隔缀,默認(rèn)就已經(jīng)安裝了

nginx -V
企業(yè)微信截圖_16277194081145.png

有這個(gè)http_image_filter_module就是裝過了,直接可以用了

沒裝過需要手動(dòng)編譯安裝

./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;
    }

功能配置參數(shù)參考

image_filter off;
#關(guān)閉模塊
 
image_filter test;
#確保圖片是jpeg gif png否則返415錯(cuò)誤
 
image_filter size;
#輸出有關(guān)圖像的json格式:例如以下顯示{ "img" : { "width": 100, "height": 100, "type": "gif" } } 出錯(cuò)顯示:{}
 
image_filter rotate 90|180|270;
#旋轉(zhuǎn)指定度數(shù)的圖像潮瓶,參數(shù)能夠包括變量,單獨(dú)或一起與resize crop一起使用钙姊。
 
image_filter resize width height;
#按比例降低圖像到指定大小毯辅,公降低一個(gè)能夠還有一個(gè)用"-"來表示,出錯(cuò)415,參數(shù)值可包括變量煞额,能夠與rotate一起使用思恐,則兩個(gè)一起生效沾谜。
 
image_filter crop width height;
#按比例降低圖像比較大的側(cè)面積和還有一側(cè)多余的載翦邊緣,其他和rotate一樣胀莹。沒太理解
 
image_filter_buffer 10M;
#設(shè)置讀取圖像緩沖的最大大小基跑,超過則415錯(cuò)誤。
 
image_filter_interlace on;
#假設(shè)啟用描焰,終于的圖像將被交錯(cuò)媳否。對(duì)于JPEG,終于的圖像將在“漸進(jìn)式JPEG”格式荆秦。
 
image_filter_jpeg_quality 95;
#設(shè)置變換的JPEG圖像的期望質(zhì)量篱竭。可接受的值是從1到100的范圍內(nèi)步绸。較小的值通常意味著既降低圖像質(zhì)量掺逼,降低數(shù)據(jù)傳輸,推薦的最大值為95瓤介。參數(shù)值能夠包括變量吕喘。
 
image_filter_sharpen 100;
#添加了終于圖像的清晰度。銳度百分比能夠超過100刑桑。零值將禁用銳化氯质。參數(shù)值能夠包括變量。
 
image_filter_transparency on;
#定義是否應(yīng)該透明轉(zhuǎn)換的GIF圖像或PNG圖像與調(diào)色板中指定的顏色時(shí)漾月,能夠保留病梢。透明度的損失將導(dǎo)致更好的圖像質(zhì)量。在PNG的Alpha通道總是保留透明度梁肿。

三、幾個(gè)規(guī)則觅彰,可能實(shí)用吩蔑。
匹配全站全部的結(jié)尾圖片


        location ~* \.(jpg|gif|png)$ {
            image_filter resize 500 500;
        }

匹配某個(gè)文件夾全部圖片

        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
自己主動(dòng)將原圖縮放為300*200的尺寸

更多配置方案

nginx_image_filter 

http_image_filter_module

配置


第一種:

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

location = /empty {
    empty_gif;
}

http://nginx.org/en/docs/http/ngx_http_image_filter_module.html#image_filter_webp_quality

第二種:

//裁剪圖片,不存儲(chǔ)硬盤
訪問 http://xxx.com/fanfan_11.jpg@100w_100h_75Q_r
http://xxx.com/fanfan.jpg@150w_100h_75Q_r
http://xxx.com/img/fanfan.jpg@11w_11_h_80Q_r

    # 等比縮放圖片
    location ~ (.+)\.(jpeg|JPEG|jpg|gif|png|JPG|GIF|PNG)@(\d+)w_(\d+)h_(\d+)Q_r$ {
        set $w $3; #寬
        set $h $4; #高
        set $q $5; #圖片質(zhì)量
        image_filter resize $w $h;
        image_filter_jpeg_quality $q;
        image_filter_buffer 5M;
        try_files $1.$2 /www/wwwroot/www_xiyuehui/www/profile/notfound.jpg;
    }
    # 裁剪圖片
    location ~ (.+)\.(jpeg|JPEG|jpg|gif|png|JPG|GIF|PNG)@(\d+)w_(\d+)h_(\d+)Q_c$ {
        set $w $3; #寬
        set $h $4; #高
        set $q $5; #圖片質(zhì)量
        image_filter crop $w $h;
        image_filter_jpeg_quality $q;
        image_filter_buffer 5M;
        try_files $1.$2 /www/wwwroot/www_xiyuehui/www/profile/notfound.jpg;
    }

第三種:

//保存在磁盤

訪問 
http://xxx.com/image_filter/222.jpg@120w_120h_75Q_r
代理到
xxx.com/image-resize/image_filter/222.jpg?w=200&h=200&q=75

location ~ (.+)\.(jpeg|JPEG|jpg|gif|png|JPG|GIF|PNG)@(\d+)w_(\d+)h_(\d+)Q_([rc])$ {
    
    # 方便調(diào)試
    error_log  /usr/local/var/logs/nginx/xxx.com.imagefilter.error.log  debug;

    # 限制referer填抬,防盜鏈
    # valid_referers xxx.com;#domain modify
    # if ($invalid_referer) {return 404;}
 
    set $w $3; #寬
    set $h $4; #高
    set $q $5; #圖片質(zhì)量
    set $type $6;
    set $image_path  $1.$2; #真實(shí)圖片地址
    set $cache_path  $1_$3w_$4h_$5Q_$6.$2;  #臨時(shí)文件地址
    if ($type = 'r') {
        set $type 'image-resize';
    }
    if ($type = 'c') {
        set $type 'image-crop';
    }
    set $image_uri  /$type$image_path?w=$w&h=$h&q=$q;
    if (-f $document_root$cache_path) {
        rewrite (.+)\.(jpg|gif|png)@(\d+)w_(\d+)h_(\d+)Q_([rc])$ $1_$3w_$4h_$5Q_$6.$2;
        break;
    }
    if (!-f $document_root$cache_path) {
        # proxy_pass http://$server_name.$image_uri;
        # 必須填寫ip,填寫域名的話烛芬,nginx在啟動(dòng)的時(shí)候會(huì)檢測(cè)域名,解析DNS,啟動(dòng)后飒责,在修改域名的解析是不生效的
        # 實(shí)際上赘娄,本機(jī)填寫域名報(bào)500不生效,估計(jì)是DNS設(shè)置不對(duì)宏蛉,會(huì)在server下添加
        # resolver 127.0.0.1 valid=300s;
# 圖片端口不是80 需要配置端口
        proxy_pass http://127.0.0.1:8011$image_uri;
        break;
    }
    proxy_store $document_root$cache_path;
    proxy_store_access user:rw group:rw all:r;
    proxy_temp_path  /tmp/images;
    proxy_set_header Host $host;
    expires  10d; # 設(shè)置圖片過期時(shí)間10天
}
location ~ /image-resize(.+)\.(jpeg|JPEG|jpg|gif|png|JPG|GIF|PNG) {
    rewrite /image-resize(.+)\.(jpg|gif|png)$ $1.$2 break;
    image_filter resize $arg_w $arg_h;
    image_filter_jpeg_quality $arg_q;
    image_filter_buffer 5M;
    try_files $1.$2 /img/notfound.jpg;
}
location ~ /image-crop(.+)\.(jpeg|JPEG|jpg|gif|png|JPG|GIF|PNG) {
    rewrite /image-crop(.+)\.(jpg|gif|png)$ $1.$2 break;
    image_filter crop $arg_w $arg_h;
    image_filter_jpeg_quality $arg_q;
    image_filter_buffer 5M;
    try_files $1.$2 /img/notfound.jpg;
}

壓縮圖片存盤的完整參考

#BINDING-127.0.0.1-START
server
{
    listen 8011;
    server_name 127.0.0.1;
    root /www/wwwroot/www_xiyuehui/www/profile;

    # 壓縮圖片不存盤
    # # 等比縮放圖片
    # location ~ (.+)\.(jpeg|JPEG|jpg|gif|png|JPG|GIF|PNG)@(\d+)w_(\d+)h_(\d+)Q_r$ {
    #     set $w $3; #寬
    #     set $h $4; #高
    #     set $q $5; #圖片質(zhì)量
    #     image_filter resize $w $h;
    #     image_filter_jpeg_quality $q;
    #     image_filter_buffer 5M;
    #     try_files $1.$2 /www/wwwroot/www_xiyuehui/www/profile/notfound.jpg;
    # }
    # # 裁剪圖片
    # location ~ (.+)\.(jpeg|JPEG|jpg|gif|png|JPG|GIF|PNG)@(\d+)w_(\d+)h_(\d+)Q_c$ {
    #     set $w $3; #寬
    #     set $h $4; #高
    #     set $q $5; #圖片質(zhì)量
    #     image_filter crop $w $h;
    #     image_filter_jpeg_quality $q;
    #     image_filter_buffer 5M;
    #     try_files $1.$2 /www/wwwroot/www_xiyuehui/www/profile/notfound.jpg;
    # }
    # 壓縮圖片緩存存盤
    location ~ (.+)\.(jpeg|JPEG|jpg|gif|png|JPG|GIF|PNG)@(\d+)w_(\d+)h_(\d+)Q_([rc])$ {
      
      # 方便調(diào)試
      error_log  /www/wwwlogs/www.xiyuehui.online.imagefilter.error.log  debug;
  
      # 限制referer遣臼,防盜鏈
      # valid_referers xxx.com;#domain modify
      # if ($invalid_referer) {return 404;}
   
      set $w $3; #寬
      set $h $4; #高
      set $q $5; #圖片質(zhì)量
      set $type $6;
      set $image_path  $1.$2; #真實(shí)圖片地址
      set $cache_path  $1_$3w_$4h_$5Q_$6.$2;  #臨時(shí)文件地址
      if ($type = 'r') {
          set $type 'image-resize';
      }
      if ($type = 'c') {
          set $type 'image-crop';
      }
      set $image_uri  /$type$image_path?w=$w&h=$h&q=$q;
      if (-f $document_root$cache_path) {
          rewrite (.+)\.(jpeg|JPEG|jpg|gif|png|JPG|GIF|PNG)@(\d+)w_(\d+)h_(\d+)Q_([rc])$ $1_$3w_$4h_$5Q_$6.$2;
          break;
      }
      if (!-f $document_root$cache_path) {
          # proxy_pass http://$server_name.$image_uri;
          # 必須填寫ip,填寫域名的話,nginx在啟動(dòng)的時(shí)候會(huì)檢測(cè)域名拾并,解析DNS,啟動(dòng)后揍堰,在修改域名的解析是不生效的
          # 實(shí)際上鹏浅,本機(jī)填寫域名報(bào)500不生效,估計(jì)是DNS設(shè)置不對(duì)屏歹,會(huì)在server下添加
          # resolver 127.0.0.1 valid=300s;
  
          proxy_pass http://127.0.0.1:8011$image_uri;
          break;
      }
      proxy_store $document_root$cache_path;
      proxy_store_access user:rw group:rw all:r;
      proxy_temp_path  /tmp/images;
      proxy_set_header Host $host;
      expires  10d; # 設(shè)置圖片過期時(shí)間10天
  }
  location ~ /image-resize(.+)\.(jpeg|JPEG|jpg|gif|png|JPG|GIF|PNG) {
      rewrite /image-resize(.+)\.(jpeg|JPEG|jpg|gif|png|JPG|GIF|PNG)$ $1.$2 break;
      image_filter resize $arg_w $arg_h;
      image_filter_jpeg_quality $arg_q;
      image_filter_buffer 5M;
      try_files $1.$2 /img/notfound.jpg;
  }
  location ~ /image-crop(.+)\.(jpeg|JPEG|jpg|gif|png|JPG|GIF|PNG) {
      rewrite /image-crop(.+)\.(jpeg|JPEG|jpg|gif|png|JPG|GIF|PNG)$ $1.$2 break;
      image_filter crop $arg_w $arg_h;
      image_filter_jpeg_quality $arg_q;
      image_filter_buffer 5M;
      try_files $1.$2 /img/notfound.jpg;
  }

    # access_log /www/wwwlogs/www.xiyuehui.online.log;
    # error_log  /www/wwwlogs/www.xiyuehui.online.error.log;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末隐砸,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子蝙眶,更是在濱河造成了極大的恐慌季希,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,273評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件幽纷,死亡現(xiàn)場(chǎng)離奇詭異式塌,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)霹崎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門珊搀,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人尾菇,你說我怎么就攤上這事境析。” “怎么了派诬?”我有些...
    開封第一講書人閱讀 167,709評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵劳淆,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我默赂,道長(zhǎng)沛鸵,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,520評(píng)論 1 296
  • 正文 為了忘掉前任缆八,我火速辦了婚禮曲掰,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘奈辰。我一直安慰自己栏妖,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,515評(píng)論 6 397
  • 文/花漫 我一把揭開白布奖恰。 她就那樣靜靜地躺著吊趾,像睡著了一般。 火紅的嫁衣襯著肌膚如雪瑟啃。 梳的紋絲不亂的頭發(fā)上论泛,一...
    開封第一講書人閱讀 52,158評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音蛹屿,去河邊找鬼屁奏。 笑死,一個(gè)胖子當(dāng)著我的面吹牛蜡峰,可吹牛的內(nèi)容都是我干的了袁。 我是一名探鬼主播朗恳,決...
    沈念sama閱讀 40,755評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼载绿!你這毒婦竟也來了粥诫?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,660評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤崭庸,失蹤者是張志新(化名)和其女友劉穎怀浆,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體怕享,經(jīng)...
    沈念sama閱讀 46,203評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡执赡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,287評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了函筋。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片沙合。...
    茶點(diǎn)故事閱讀 40,427評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖跌帐,靈堂內(nèi)的尸體忽然破棺而出首懈,到底是詐尸還是另有隱情,我是刑警寧澤谨敛,帶...
    沈念sama閱讀 36,122評(píng)論 5 349
  • 正文 年R本政府宣布究履,位于F島的核電站,受9級(jí)特大地震影響脸狸,放射性物質(zhì)發(fā)生泄漏最仑。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,801評(píng)論 3 333
  • 文/蒙蒙 一炊甲、第九天 我趴在偏房一處隱蔽的房頂上張望泥彤。 院中可真熱鬧,春花似錦卿啡、人聲如沸全景。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至滞伟,卻和暖如春揭鳞,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背梆奈。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工野崇, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人亩钟。 一個(gè)月前我還...
    沈念sama閱讀 48,808評(píng)論 3 376
  • 正文 我出身青樓乓梨,卻偏偏與公主長(zhǎng)得像鳖轰,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子扶镀,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,440評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容