java centos 缩略图_使用 Nginx 的 image_filter 模块来构建动态缩略图服务器
原標(biāo)題:使用 Nginx 的 image_filter 模塊來(lái)構(gòu)建動(dòng)態(tài)縮略圖服務(wù)器
在以前我們實(shí)現(xiàn)縮略圖機(jī)制通常是在當(dāng)用戶(hù)上傳一張圖片后,后端程序會(huì)固定將圖片生成前端頁(yè)面需要的不同大小縮略圖。不管前端頁(yè)面是否有使用,后端都會(huì)先產(chǎn)生好,這樣做明顯有以下缺陷:
占用過(guò)多的磁盤(pán)空間大小
前端頁(yè)面需要更多樣格式的縮略圖時(shí),需要單獨(dú)處理。
當(dāng)出現(xiàn)第二個(gè)問(wèn)題時(shí)會(huì)比較麻煩,后端程序就需要將系統(tǒng)的全部圖片重新產(chǎn)生一次所需的縮略圖。這個(gè)過(guò)程非常耗時(shí),也比較耗費(fèi)系統(tǒng)性能。
現(xiàn)在比較流行的做法是改成透過(guò) URL 定義長(zhǎng)寬來(lái)即時(shí)生成所需的縮略圖,實(shí)現(xiàn)的方式也有多種多樣,本文將介紹使用 Nginx 的 image_filter 模塊來(lái)實(shí)現(xiàn)動(dòng)態(tài)生成縮略圖。
從 Nginx 0.7.54 以后的版本,提供了一個(gè) http_image_filter_module 的集成圖片處理模塊。該模塊可以實(shí)現(xiàn)實(shí)時(shí)縮放圖片、旋轉(zhuǎn)圖片、驗(yàn)證圖片有效性以及獲取圖片寬高以及圖片類(lèi)型信息等功能。
安裝 image_filter 模塊
http_image_filter_module 模塊在默認(rèn)情況下是沒(méi)有包含在官方預(yù)編譯包中的,如果需要使用 http_image_filter_module 模塊需要重新編譯 Nginx。
查看是否已安裝 image_filter 模塊$ nginx -V
如果返回的結(jié)果中含有 --with-http_image_filter_module 字樣就表明已經(jīng)安裝了 image_filter 模塊。
編譯 Nginx 并加入 image_filter 模塊
啟用 http_image_filter_module 模塊的方法是比較簡(jiǎn)單的,由于http_image_filter_module 擴(kuò)展需要 GD 庫(kù)支持,首先需要安裝相關(guān)依賴(lài)環(huán)境:
# CentOS / RHEL$ sudo yum -y install gd-devel# Ubuntu / Debian$ sudo apt-get install libgd2-xpm-dev
其次在原來(lái)的 Nginx 編譯參數(shù)上加上 --with-http_image_filter_module 參數(shù)后重新編譯 Nginx 就行了。
$ sudo wget http://nginx.org/download/nginx-1.15.0.tar.gz$ sudo tar xzvf nginx-1.15.0.tar.gz$ cd nginx-1.15.0/$ sudo ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' --with-http_image_filter_module$ make && make install
驗(yàn)證是否安裝成功$ nginx -Vnginx version: nginx/1.15.0built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)built with OpenSSL 1.0.2g 1 Mar 2016TLS SNI support enabledconfigure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' --with-http_image_filter_module
如果返回的結(jié)果中含有 --with-http_image_filter_module 字樣就表明安裝成功了。
image_filter 模塊常用參數(shù)說(shuō)明 # 關(guān)閉模塊image_filter off;# 確保圖片是 JPEG、GIF、PNG、或者 WebP,否則返回 415 錯(cuò)誤image_filter test;# 輸出有關(guān)圖像的 json 格式,例如 { "img" : { "width": 100, "height": 100, "type": "gif" } } ,如果出錯(cuò)顯示:{}image_filter size;# 將圖像旋轉(zhuǎn)指定度數(shù),參數(shù)可以包括變量。單獨(dú)或一起與 resize crop 一起使用。image_filter rotate 90|180|270;# 按比例減少圖像到指定大小,要減少一個(gè)維度,可以將另一個(gè)維度指定為 "-" 來(lái)表示, 出錯(cuò)返回 415 錯(cuò)誤。參數(shù)值可包含變量,可以與 rotate 一起使用。image_filter resize width height;# 按比例將圖像縮小到較大的一側(cè),并在另一側(cè)產(chǎn)生無(wú)關(guān)邊緣。其它和 rotate 一樣。image_filter crop width height;# 設(shè)置讀取圖像緩沖的最大大小,超過(guò)則報(bào) 415 錯(cuò)誤。image_filter_buffer 10M;# 如果啟用,最終圖像將交錯(cuò)。對(duì)于JPEG,最終圖像將采用 "逐行JPEG" 格式。image_filter_interlace on;# 設(shè)置轉(zhuǎn)換的 JPEG 圖像的質(zhì)量。可接受的值是從 1 到 100 的范圍內(nèi)。較小的值通常意味著圖像質(zhì)量越低,以達(dá)到減少數(shù)據(jù)傳輸。推薦的最大值為 95,參數(shù)值可以包含變量。image_filter_jpeg_quality 95;# 增加最終圖像的清晰度,銳度百分比可以超過(guò) 100。零值將禁用銳化。參數(shù)值可以包含變量。image_filter_sharpen 100;# 定義在使用調(diào)色板指定的顏色轉(zhuǎn)換 GIF 圖像或 PNG 圖像時(shí)是否應(yīng)保留透明度。透明度的降低會(huì)導(dǎo)致質(zhì)量更好的圖像,PNG 中 的 alpha 通道透明度始終保留。image_filter_transparency on; 使用 image_filter 模塊動(dòng)態(tài)生成縮略圖
配置 image_filter 模塊
這里給出一個(gè)簡(jiǎn)單的例子,在你的 Nginx 配置文件中 Server 配置段增加如下內(nèi)容:
$ sudo vim nginx.conf...server {listen 80 default_server;listen [::]:80 default_server;server_name www.hi-linux.com;location ~ ^/([0-9]+)/(.*)$ {set $width $1;set $path $2;set $EXPIRE_TIME 1d;set $JPG_QUALITY 95;rewrite ^ /$path break;image_filter resize $width -;image_filter_buffer 100M;image_filter_jpeg_quality ${JPG_QUALITY};expires ${EXPIRE_TIME};}}...
重載 Nginx 配置$ sudo nginx -s reload
生成縮略圖
通過(guò)在圖片請(qǐng)求地址的文件名前加上 /縮放大小/,就可以訪(fǎng)問(wèn)到你想要的任意尺寸的縮略圖。例如:要生成縮略圖的圖片地址為:http://192.168.100.210/01.jpg,現(xiàn)在只需要將請(qǐng)求修改為:http://192.168.100.210/200/01.jpg 將會(huì)自動(dòng)返回對(duì)應(yīng)的縮略圖。
給 image_filter 模塊加入緩存機(jī)制
默認(rèn)情況下,image_filter 模塊都是實(shí)時(shí)生成縮略圖的。每一次請(qǐng)求 Nginx 都會(huì)需要處理,如果有 10000 次請(qǐng)求,就會(huì)處理 10000 次。這樣就會(huì)在高并發(fā)請(qǐng)求時(shí)給 Nginx 服務(wù)器造成壓力。
要解決這個(gè)問(wèn)題,我們可以通過(guò) Nginx 的 Proxy Cache 機(jī)制讓 Nginx 在相同請(qǐng)求的情況下只產(chǎn)生一次縮略圖,并且將縮略圖放到緩存目錄中以減少短時(shí)間內(nèi)的不同請(qǐng)求所產(chǎn)生的運(yùn)算次數(shù)。
由于 image_filter 模塊無(wú)法跟 Proxy Cache 同時(shí)處理,要實(shí)現(xiàn)這個(gè)功能必須要將請(qǐng)求拆成兩個(gè) Host 來(lái)達(dá)成。具體配置如下:
$ sudo vim nginx.conf...proxy_cache_path /data keys_zone=cache_zone:10m;server {# Internal image resizing server.server_name localhost;listen 8888;location ~ ^/([0-9]+)/(.*)$ {set $width $1;set $path $2;set $JPG_QUALITY 95;rewrite ^ /$path break;#proxy_pass ${IMAGE_HOST};image_filter resize $width -;image_filter_buffer 100M;image_filter_jpeg_quality ${JPG_QUALITY};}}server {listen 80 default_server;listen [::]:80 default_server;server_name www.hi-linux.com;location ~ ^/([0-9]+)/(.*)$ {set $width $1;set $path $2;set $EXPIRE_TIME 1d;rewrite ^ /$path break;proxy_pass http://127.0.0.1:8888/$width/$path;proxy_cache cache_zone;proxy_cache_key $uri;proxy_cache_valid 200 302 24h;proxy_cache_valid 404 1m;# expire time for browserexpires ${EXPIRE_TIME};}} 使用 Docker 容器快速構(gòu)建
如果你和我一樣比較偏愛(ài)使用 Docker,可以直接使用 nginx-image-resizer 項(xiàng)目。nginx-image-resizer 項(xiàng)目是一個(gè)用 Docker 實(shí)現(xiàn)的實(shí)時(shí)圖像調(diào)整大小和緩存的容器項(xiàng)目,nginx-image-resizer 支持 Amazon S3 和 Minio 作為存儲(chǔ)。
項(xiàng)目官方地址:http://t.cn/RBG2QWx
下面給一個(gè)使用 Minio 作為存儲(chǔ)的 docker-compose 示例:
編寫(xiě) docker-compose 配置文件$ vim docker-compose.ymlversion: '2'services:minio:image: minio/miniocontainer_name: minioports:- "9000:9000"volumes:- minio-data:/dataenvironment:MINIO_ACCESS_KEY: YOUR_MINIO_ACCESS_KEYMINIO_SECRET_KEY: YOUR_MINIO_SECRET_KEYcommand: server /dataimage-resizer:image: appleboy/nginx-image-resizercontainer_name: image-resizerports:- "80:80"environment:IMAGE_HOST: http://minio:9000NGINX_HOST: localhostvolumes:minio-data:
注:MINIO_ACCESS_KEY 和 MINIO_SECRET_KEY 分別為 Minio 的登陸帳號(hào)和密碼,請(qǐng)根據(jù)實(shí)際情況修改。
啟動(dòng)相關(guān)容器$ docker-compose up -d
生成縮略圖
生成縮略圖的方法和前面方法類(lèi)似,但由于這里使用了 Minio 做為文件存儲(chǔ)服務(wù),所以需要先在 Minio 中上傳圖片文件。在 Minio 中上傳文件的方法大致分為以下幾步:
登陸 Minio Web 管理界面, Web 管理界面訪(fǎng)問(wèn)地址默認(rèn)為:http://HostIP:9000。
創(chuàng)建 bucket,這里我們創(chuàng)建一個(gè)名為 test 的 bucket。
設(shè)置名為 test 的 bucket 的訪(fǎng)問(wèn)權(quán)限。
在名為 test 的 bucket 中上傳圖片文件。完成以上幾步后,和前面類(lèi)似直接在訪(fǎng)問(wèn)的 URL 中加入需縮放圖片比例即可生成對(duì)應(yīng)縮略圖。例如:訪(fǎng)問(wèn)http://HostIP/600/test/test1.jpg 將會(huì)自動(dòng)返回對(duì)應(yīng)的縮略圖。
其它相關(guān)說(shuō)明
利用 Nginx 的 image_filter 模塊不僅僅能生成縮略圖,你還可以通過(guò)修改配置來(lái)實(shí)現(xiàn)其它的功能,比如圖片旋轉(zhuǎn)、圖片裁剪、圖片透明化等等。關(guān)于 image_filter 模塊的更多使用方法可參考官方文檔。
https://www.google.com
http://t.cn/RBG2QWx
http://t.cn/RBGyUbz
http://t.cn/R7R9bZU
http://t.cn/RBGyxil
只要你愿意投入時(shí)間和精力來(lái)培養(yǎng)新技能,那么你就能做你應(yīng)該做的事。如果你只涉獵你能力所及的領(lǐng)域,那么你的技能就會(huì)過(guò)時(shí)。
責(zé)任編輯:
總結(jié)
以上是生活随笔為你收集整理的java centos 缩略图_使用 Nginx 的 image_filter 模块来构建动态缩略图服务器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java异常之-Caused by: j
- 下一篇: 成都Java培训班帮你分析如何通过企业面