一铸磅、安裝
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;
}