使用nginx動(dòng)態(tài)轉(zhuǎn)換圖片大小生成縮略圖
Nginx的ngx_http_image_filter_module 模塊(nginx版本為0.7.54+) 可用來(lái)動(dòng)態(tài)轉(zhuǎn)換JPEG, GIF, PNG, 和WebP格式的圖片大小。
該模塊默認(rèn)沒(méi)有構(gòu)建,需要通過(guò) --with-http_image_filter_module 配置參數(shù)啟用。
如果圖片訪問(wèn)量不大,可以使用該模塊。
該模塊使用了libgd庫(kù)。 推薦使用該庫(kù)的最新版本。
以下為在已經(jīng)安裝了nginx的情況下添加該模塊的步驟。
1 安裝依賴
yum -y install gd-devel yum -y install libxslt-devel yum -y install perl-devel perl-ExtUtils-Embed
2 查看原nginx參數(shù)
nginx -V
輸出結(jié)果如下:
nginx version: nginx/1.12.2
configure arguments:
查看是否有--with-http_image_filter_module,如果有則說(shuō)明已經(jīng)安裝過(guò),否則繼續(xù)安裝。
3 編譯
重新編譯時(shí)需要下載和原來(lái)相同版本的nginx源碼,解壓。
添加--with-http_image_filter_module=dynamic及原來(lái)的參數(shù)(不需要執(zhí)行make install命令重新安裝)
./configure --with-http_image_filter_module=dynamic make
4 備份原nginx
cp -r /usr/local/nginx /usr/local/nginxbak
5 覆蓋nginx
cp objs/nginx /usr/local/nginx/sbin/ mkdir /usr/local/nginx/module cp objs/ngx_http_image_filter_module.so /usr/local/nginx/module/ngx_http_image_filter_module.so
6 修改配置文件
vim /usr/local/nginx/conf/nginx.conf
load_module /usr/local/nginx/module/ngx_http_image_filter_module.so; location ~* /images/(.+)_(\d+)x(\d+)\.(jpg|jpeg|gif|png)$ { set $w $2; set $h $3; image_filter resize $w $h; image_filter_buffer 30M; #設(shè)置圖片緩沖區(qū)的最大大小,大小超過(guò)設(shè)定值,服務(wù)器將返回錯(cuò)誤415 image_filter_interlace on; image_filter_jpeg_quality 80; try_files /$1.$4 /notfound.jpg; expires 1d; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; alias /home/publisherupload/ ; #root 能夠讓訪問(wèn) /images/ 路徑時(shí),訪問(wèn)到 /User/XX/Desktop/images,如果將 root 替換為 alias,則訪問(wèn)的是 /User/XX/Desktop/;其中 /User/XX/Desktop/ 是本機(jī)中的絕對(duì)路徑。 } location /images { alias /home/publisherupload/ ; #root 能夠讓訪問(wèn) /images/ 路徑時(shí),訪問(wèn)到 /User/XX/Desktop/images,如果將 root 替換為 alias,則訪問(wèn)的是 /User/XX/Desktop/;其中 /User/XX/Desktop/ 是本機(jī)中的絕對(duì)路徑。 autoindex on; #打開(kāi)目錄瀏覽功能 }
7 重載nginx配置
/usr/local/nginx/sbin/nginx -s
8 測(cè)試
最終的圖片訪問(wèn)地址為在原圖片地址后面加入_寬度X高度參數(shù)
http://110.87.103.58:12345/images/2021-01/202101250944272_500x400.png
參考
https://cloud.tencent.com/developer/article/1644466
到此這篇關(guān)于使用nginx動(dòng)態(tài)轉(zhuǎn)換圖片大小生成縮略圖的文章就介紹到這了,更多相關(guān)nginx動(dòng)態(tài)轉(zhuǎn)換圖片大小內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。