圖片縮略圖 水印處理的Nginx模塊:Nginx Image Module
本nginx模塊主要功能是對請求的圖片進行縮略/水印處理,支持文字水印和圖片水印。
支持自定義字體,文字大小,水印透明度,水印位置。
判斷原圖是否是否大于指定尺寸才處理。 ....等等
編譯方法
編譯前請確認您的系統已經安裝了libcurl-dev libgd2-dev libpcre-dev 依賴庫
Debian / Ubuntu 系統舉例
# 如果你沒有安裝GCC相關環境才需要執行
$ sudo apt-get install build-essential m4 autoconf automake make
$ sudo apt-get install libgd2-noxpm-dev libcurl4-openssl-dev libpcre3-dev
CentOS /RedHat / Fedora
# 請確保已經安裝了gcc automake autoconf m4
$ sudo yum install gd-devel pcre-devel libcurl-devel
FreeBSD / NetBSD / OpenBSD
# 不多說了,自己用port 把libcurl-dev libgd2-dev libpcre-dev 裝上吧
# 編譯前請確保已經安裝gcc automake autoconf m4
Windows
# 也支持的,不過要修改的代碼太多了,包括Nginx本身,用VC++來編譯
# 嫌麻煩可以用cygwin來編譯。還是不建議你這么做了,用Unix/Linux操作系統吧。
下載nginx / tengine 源代碼
然后下載本模塊代碼,并放在nginx源代碼目錄下
選Nginx還是Tengine,您自己看,兩者選其一
# 下載Tengine
$ wget http://tengine.taobao.org/download/tengine-1.4.5.tar.gz
$ tar -zxvf tengine-1.4.5.tar.gz
$ cd tengine-1.4.5
# 下載Nginx
$ wget http://nginx.org/download/nginx-1.4.0.tar.gz
$ tar -zxvf nginx-1.4.0.tar.gz
$ cd nginx-1.4.0
$ wget https://github.com/3078825/nginx-image/archive/master.zip
$ unzip master.zip
$ ./configure --add-module=./nginx-image-master
$ make
$ sudo make install
配置方法
打開 nginx.conf
vim /etc/nginx/nginx.conf
# 該路徑為默認路徑,如果不在此處,自己找一下 find / -name "nginx.conf"
在
location / {
root html;
#添加以下配置
image on;
image_output on;
}
或者指定目錄開啟
location /upload {
root html;
image on;
image_output on;
}
其他參數說明:
image on/off 是否開啟縮略圖功能,默認關閉
image_backend on/off 是否開啟鏡像服務,當開啟該功能時,請求目錄不存在的圖片(判斷原圖),將自動從鏡像服務器地址下載原圖
image_backend_server 鏡像服務器地址
image_output on/off 是否不生成圖片而直接處理后輸出 默認off
image_jpeg_quality 75 生成JPEG圖片的質量 默認值75
image_water on/off 是否開啟水印功能
image_water_type 0/1 水印類型 0:圖片水印 1:文字水印
image_water_min 300 300 圖片寬度 300 高度 300 的情況才添加水印
image_water_pos 0-9 水印位置 默認值9 0為隨機位置,1為頂端居左,2為頂端居中,3為頂端居右,4為中部居左,5為中部居中,6為中部居右,7為底端居左,8為底端居中,9為底端居右
image_water_file 水印文件(jpg/png/gif),絕對路徑或者相對路徑的水印圖片
image_water_transparent 水印透明度,默認20
image_water_text 水印文字 "Power By Vampire"
image_water_font_size 水印大小 默認 5
image_water_font 文字水印字體文件路徑
image_water_color 水印文字顏色,默認 #000000
調用說明
這里假設你的nginx 訪問地址為 http://127.0.0.1/
并在nginx網站根目錄存在一個 test.jpg 的圖片
通過訪問
http://127.0.0.1/test.jpg!c300x200.jpg 將會 生成/輸出 test.jpg 300x200 的縮略圖
其中 c 是生成圖片縮略圖的參數, 300 是生成縮略圖的 寬度 200 是生成縮略圖的 高度
一共可以生成四種不同類型的縮略圖。
支持 jpeg / png / gif (Gif生成后變成靜態圖片)
C 參數按請求寬高比例從圖片高度 10% 處開始截取圖片,然后縮放/放大到指定尺寸( 圖片縮略圖大小等于請求的寬高 )
M 參數按請求寬高比例居中截圖圖片,然后縮放/放大到指定尺寸( 圖片縮略圖大小等于請求的寬高 )
T 參數按請求寬高比例按比例縮放/放大到指定尺寸( 圖片縮略圖大小可能小于請求的寬高 )
W 參數按請求寬高比例縮放/放大到指定尺寸,空白處填充白色背景顏色( 圖片縮略圖大小等于請求的寬高 )
調用舉例
http://127.0.0.1/test.jpg!c300x300.jpg
http://127.0.0.1/test.jpg!t300x300.jpg
http://127.0.0.1/test.jpg!m300x300.jpg
http://127.0.0.1/test.jpg!w300x300.jpg
http://127.0.0.1/test.c300x300.jpg
http://127.0.0.1/test.t300x300.jpg
http://127.0.0.1/test.m300x300.jpg
http://127.0.0.1/test.w300x300.jpg