ngx_http_image_filter_module 模組

範例設定
指令
     image_filter
     image_filter_buffer
     image_filter_interlace
     image_filter_jpeg_quality
     image_filter_sharpen
     image_filter_transparency
     image_filter_webp_quality

ngx_http_image_filter_module 模組 (0.7.54+) 是一個可以轉換 JPEG、GIF、PNG 和 WebP 格式影像的篩選器。

此模組預設不會建置,必須使用 --with-http_image_filter_module 組態參數啟用。

此模組使用 libgd 函式庫。建議使用該函式庫的最新可用版本。

WebP 格式支援是在 1.11.6 版本中加入的。若要轉換此格式的影像,必須使用 WebP 支援編譯 libgd 函式庫。

範例設定

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

location = /empty {
    empty_gif;
}

指令

語法 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;
預設
image_filter off;
內容 location

設定要對影像執行的轉換類型

off
關閉周圍 location 的模組處理。
test
確保回應是 JPEG、GIF、PNG 或 WebP 格式的影像。否則,會傳回 415 (不支援的媒體類型) 錯誤。
size
以 JSON 格式輸出有關影像的資訊,例如:
{ "img" : { "width": 100, "height": 100, "type": "gif" } }
發生錯誤時,輸出如下:
{}
rotate 90|180|270
將影像以逆時針方向旋轉指定的度數。參數值可以包含變數。此模式可以單獨使用,也可以與 resizecrop 轉換一起使用。
resize width height
按比例將影像縮小到指定的大小。若要僅縮小一個維度,則另一個維度可以指定為「-」。如果發生錯誤,伺服器會傳回代碼 415 (不支援的媒體類型)。參數值可以包含變數。當與 rotate 參數一起使用時,旋轉會在縮小之後發生。
crop width height
按比例將影像縮小到較大的邊長大小,並將另一邊多餘的邊緣裁切掉。若要僅縮小一個維度,則另一個維度可以指定為「-」。如果發生錯誤,伺服器會傳回代碼 415 (不支援的媒體類型)。參數值可以包含變數。當與 rotate 參數一起使用時,旋轉會在縮小之前發生。

語法 image_filter_buffer size;
預設
image_filter_buffer 1M;
內容 httpserverlocation

設定用於讀取影像的緩衝區最大大小。當超出大小時,伺服器會傳回錯誤 415 (不支援的媒體類型)。

語法 image_filter_interlace on | off;
預設
image_filter_interlace off;
內容 httpserverlocation

此指令是在 1.3.15 版本中加入的。

如果啟用,最終影像將會是交錯的。對於 JPEG,最終影像將會是「漸進式 JPEG」格式。

語法 image_filter_jpeg_quality quality;
預設
image_filter_jpeg_quality 75;
內容 httpserverlocation

設定轉換後的 JPEG 影像所需的 quality。可接受的值範圍為 1 到 100。較小的值通常表示較低的影像品質和較少的傳輸資料。建議的最大值為 95。參數值可以包含變數。

語法 image_filter_sharpen percent;
預設
image_filter_sharpen 0;
內容 httpserverlocation

增加最終影像的銳利度。銳利度百分比可以超過 100。零值會停用銳利化。參數值可以包含變數。

語法 image_filter_transparency on|off;
預設
image_filter_transparency on;
內容 httpserverlocation

定義在轉換 GIF 影像或具有調色盤指定的顏色的 PNG 影像時是否應保留透明度。失去透明度會產生品質更好的影像。PNG 中的 Alpha 通道透明度始終會被保留。

語法 image_filter_webp_quality quality;
預設
image_filter_webp_quality 80;
內容 httpserverlocation

此指令是在 1.11.6 版本中加入的。

設定轉換後的 WebP 影像所需的 quality。可接受的值範圍為 1 到 100。較小的值通常表示較低的影像品質和較少的傳輸資料。參數值可以包含變數。