本文來源於 http://www.hopesoft.org/blog/?p=1188
一、背景說明
大多數網站基本都涉及到圖片縮略圖的處理,比如新聞配圖、電商商品圖等,特別是電商類網站,每個商品圖對應多個不同尺寸的縮略圖,用於不同的頁面。
初期訪問量少時,處理流程一般由web程序在上傳成功後,同時生成相應縮略圖。這種方式在訪問量小,單機部署時沒有問題。當訪問量逐漸加大,服務器由單臺變爲多臺時,這種方式擴展性較差。
以下有幾種方案可以解決這個問題:
1、使用七牛、又拍雲提供的雲存儲及數據處理服務,解決圖片的處理、存儲、多節點訪問速度的問題,這種方式優點是方案成熟,相應的有一定費用和開發工作,另外有一些小概率的風險,比如雲服務掛掉影響本站訪問。
2、使用第三方的圖片處理程序,比如zimg,點擊查看使用手冊,@招牌瘋子開發。zimg的性能和擴展性不錯,文檔也很完善,會繼續保持關注。
3、自己造輪子,根據自身業務,將生成縮略圖功能獨立出來,與web程序解耦。
我們採用的是第三種方案,參考了網友的基礎代碼,利用OpenResty(Nginx)+Lua+GraphicsMagick實現縮略圖功能,圖片上傳及刪除還是由web程序處理,縮略圖由單獨模塊完成。目前可實現配置路徑及縮略尺寸,無圖片時顯示默認圖片,支持多種縮放方式等,後續可基於GraphicsMagick實現更多功能
二、相關規範
1、文件夾規劃
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | img.hopesoft.org |-- avatars | `-- 001 | `-- 001.jpg |-- default | `-- notfound.jpg |-- photos | `-- 001 | `-- 001.jpg `-- thumbnail `-- photos `-- 001 |-- 001_100x100.jpg |-- 001_140x140.jpg |-- 001_250x250.jpg |-- 001_300x300.jpg |-- 001_350x350.jpg |-- 001_50x50.jpg `-- abc_50x50.jpg |
其中img.hopesoft.org爲圖片站點根目錄,avatars和photos目錄是原圖目錄,可根據目錄設置不同的縮略圖尺寸,default文件夾的notfound.jpg文件是在未找到原圖時的默認圖片,thumbnail文件夾用來存放縮略圖,可定時清理。
2、鏈接地址
原圖訪問地址:http://img.hopesoft.org/photos/001/001.jpg
縮略圖訪問地址:http://img.hopesoft.org/photos/001/001_100x100.jpg,(請勿加thumbnail,這個是因爲我們原來的原圖和縮略圖在同一個目錄,後來將縮略圖單獨放了一個文件夾)
不同目錄可設置不同的縮略圖規則,如:
原圖訪問地址:http://img.xxx.com/mall/001/001.jpg
縮略圖訪問地址:http://img.xxx.com/mall/001/001.jpg_100x100.jpg (請勿加thumbnail)
3、訪問流程
首先判斷縮略圖是否存在,如存在則直接顯示縮略圖;
如不存在則按以下流程處理:
- 判斷縮略圖鏈接與規則是否匹配,如不匹配,則404退出;如匹配跳至2
- 判斷原圖是否存在,如原圖存在則跳至5,如不存在則進入下一步;
- 判斷是否顯示默認圖片,如不顯示則404退出;如顯示則進入下一步
- 判斷是否存在默認圖片,如不存在則404退出;如存在則將默認圖片代替原始圖片,進入下一步;
- 拼接graphicsmagick命令,生成並顯示縮略圖
三、安裝OpenResty
1、關於OpenResty(http://openresty.org/cn/)
OpenResty (也稱爲 ngx_openresty)是一個全功能的 Web 應用服務器,它打包了標準的 Nginx 核心,很多的常用的第三方模塊,以及它們的大多數依賴項。
OpenResty 通過匯聚各種設計精良的 Nginx 模塊,
從而將 Nginx 有效的變成一個強大的 Web 應用服務器,
這樣, Web 開發人員可以使用 Lua 腳本語言調動 Nginx 支持的各種C以及Lua 模塊,
快速構造出足以勝任 10K+ 併發連接響應的超高性能Web 應用系統。更多
2、安裝OpenResty
注:drizzle-nginx-module模塊和nginx-http-concat模塊非必選項,各位可按需安裝
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#創建安裝文件目錄
mkdir
-p
/usr/local/src/nginx
cd
/usr/local/src/nginx
#安裝drizzle模塊(訪問mysql數據庫模塊,非必需,建議安裝)
wget
http://agentzh.org/misc/nginx/drizzle7-2011.07.21.tar.gz
tar
zxvf
drizzle7-2011.07.21.tar.gz
cd
drizzle7-2011.07.21
./configure
--without-server
make
libdrizzle-1.0
make
install-libdrizzle-1.0
cd
..
#下載openresty
wget
http://openresty.org/download/ngx_openresty-1.4.2.7.tar.gz
tar
zxvf
ngx_openresty-1.4.2.7.tar.gz
#下載nginx-http-concat(合併靜態文件請求模塊,非必需,建議安裝)
wget
https://github.com/alibaba/nginx-http-concat/archive/master.zip
unzip
master
mv
nginx-http-concat-master/
ngx_openresty-1.4.2.7/bundle/nginx-http-concat
#安裝openresty
cd
ngx_openresty-1.4.2.7
./configure
--with-luajit
--with-http_drizzle_module
--with-http_iconv_module
--with-ld-opt="-Wl,-rpath,/usr/local/lib" --with-http_stub_status_module
--with-http_ssl_module
--with-http_sub_module
--add-module=./bundle/nginx-http-concat/
gmake
gmake
install
|
3、將OpenResty(Nginx)加入自啓動
1 | vi /etc/rc.d/init.d/nginx |
文件內容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place
in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command
on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx
daemon"
NAME=nginx
DAEMON=/usr/local/openresty/nginx/sbin/$NAME
CONFIGFILE=/usr/local/openresty/nginx/conf/$NAME.conf
PIDFILE=/usr/local/openresty/nginx/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set
-e
[
-x
"$DAEMON"
]
||
exit
0
do_start()
{
$DAEMON
-c
$CONFIGFILE
||
echo
-n
"nginx already running"
}
do_stop()
{
kill
-INT
`cat
$PIDFILE`
||
echo
-n
"nginx not running"
}
do_reload()
{
kill
-HUP
`cat
$PIDFILE`
||
echo
-n
"nginx can't reload"
}
case
"$1"
in
start)
echo
-n
"Starting $DESC: $NAME"
do_start
echo
"."
;;
stop)
echo
-n
"Stopping $DESC: $NAME"
do_stop
echo
"."
;;
reload|graceful)
echo
-n
"Reloading $DESC configuration..."
do_reload
echo
"."
;;
restart)
echo
-n
"Restarting $DESC: $NAME"
do_stop
do_start
echo
"."
;;
*)
echo
"Usage: $SCRIPTNAME {start|stop|reload|restart}"
>&2
exit
3
;;
esac
exit
0
|
把nginx加入chkconfig,並設置開機啓動
1 2 | chkconfig --add nginx chkconfig nginx on |
啓動、停止、查看狀態
1
2
3
|
service
nginx
start
service
nginx
stop
service
nginx
status
|
四、安裝GraphicsMagick
1、安裝
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #創建安裝目錄 mkdir -p /usr/local/src/graphicsmagick cd /usr/local/src/graphicsmagick #下載graphicsmagick wget http://sourceforge.net/projects/graphicsmagick/files/graphicsmagick/1.3.18/GraphicsMagick-1.3.18.tar.gz/download tar zxvf GraphicsMagick-1.3.18.tar.gz #下載libjpeg wget ftp://ftp.pl.freebsd.org/vol/rzm1/GraphicsMagick/delegates/libjpeg-6b.tar.gz rpm -qa | grep libjpeg rpm -qa | grep libjpeg | xargs rpm -e --nodeps --allmatches tar zxvf libjpeg-6b.tar.gz cd libjpeg-6b ./configure make make install ln -s /usr/local/lib/libjpeg* /lib/ ln -s /usr/local/lib/libjpeg* /lib64/ cd .. #安裝libpng rpm -qa | grep libpng rpm -qa | grep libpng | xargs rpm -e --nodeps --allmatches wget ftp://ftp.pl.freebsd.org/vol/rzm1/GraphicsMagick/delegates/libpng-1.2.49.tar.gz tar zxvf libpng-1.2.49.tar.gz cd libpng-1.2.49 ./configure make make install ln -s /usr/local/lib/libpng* /lib/ ln -s /usr/local/lib/libpng* /lib64/ cd ../ #安裝freetype wget ftp://ftp.pl.freebsd.org/vol/rzm1/GraphicsMagick/delegates/freetype-2.4.10.tar.gz tar zxvf freetype-2.4.10.tar.gz cd freetype-2.4.10 ./configure make install ln -s /usr/local/lib/freetype* /lib/ ln -s /usr/local/lib/freetype* /lib64/ cd .. #安裝GraphicsMagick cd GraphicsMagick-1.3.18 ./configure --prefix=/usr/local/graphicsmagick-1.3.18 make make install #編輯profile文件 vi /etc/profile #末尾增加以下內容 export GMAGICK_HOME="/usr/local/graphicsmagick-1.3.18" export PATH="$GMAGICK_HOME/bin:$PATH" LD_LIBRARY_PATH=$GMAGICK_HOME/lib:$LD_LIBRARY_PATH export LD_LIBRARY_PATH #保存退出,運行以下命令立即生效配置 source /etc/profile |
下載libjpeg、libpng、freetype鏈接如失效可訪問:http://www.filewatcher.com/m/libjpeg-6b.tar.gz.961981-0.html
2、用法
原始圖片是input.jpg,尺寸:160×120
1)只縮小不放大
1
|
gm
convert
input.jpg
-resize
"500x500>"
output_1.jpg
|
加了>,表示只有當圖片的寬與高,大於給定的寬與高時,才進行“縮小”操作。
生成的圖片大小是:160×120,未進行操作
如果不加>,會導致圖片被比等放大。
2)等比縮圖 (缺點:產生白邊)
1 | gm convert input.jpg -thumbnail "100x100" output_1.jpg |
生成的圖片大小是:100×75
3)非等比縮圖,按給定的參數縮圖(缺點:長寬比會變化)
1
|
gm
convert
input.jpg
-thumbnail
"100x100!"
output_2.jpg
|
生成的圖片大小是:100×100
4)裁剪後保證等比縮圖 (缺點:裁剪了圖片的一部分)
1 | gm convert input.jpg -thumbnail "100x100^" -gravity center -extent 100x100 output_3.jpg |
生成的圖片大小是:100×100,還保證了比例。不過圖片經過了裁剪,剪了圖片左右兩邊才達到1:1
5)填充後保證等比縮圖 (缺點:要填充顏色,和第一種方法基本一樣)
1
|
gm
convert
input.jpg
-thumbnail
"100x100"
-background
gray
-gravity
center
-extent
100x100
output_4.jpg
|
生成的圖片大小是:100×100,還保證了比例,同時沒有對圖片進行任何裁剪,缺失的部分按指定顏色進行填充。
6)裁剪、填充相結合 (缺點:最差的方法)
1 | gm convert input.jpg -thumbnail "10000@ -background gray -gravity center -extent 100x100 output_5.jpg |
生成的圖片大小是:100×100,這次保證了大小和比例,其中的10000就是100×100的乘積,同時在填充和裁剪之間做了一個平衡。
7)位深度32 轉爲24
IE6,7,8不支持顯示“位深度32”的圖片,但IE9、火狐、谷歌瀏覽器就可以顯示。
使用GM,把“位深度32”的圖片轉換爲“位深度24”的圖片
輸入圖片zzz.jpg就是“位深度32”的圖片,輸出圖片 zzz_out.jpg就是“位深度24”的圖片
1
|
gm
convert
-resize
100x100
-colorspace
RGB
zzz.jpg
zzz_out.jpg
|
轉完後,圖片的顏色會有輕微變化。
更多請參考:http://elf8848.iteye.com/blog/382528
五、相關配置及腳本
1、Nginx配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | user www www; worker_processes 1; # 日誌級別調試時可設爲notice,生產環境請設爲error error_log /usr/local/openresty/nginx/logs/error.log notice; events { use epoll; worker_connections 51200; } http { lua_package_path '/usr/local/openresty/nginx/lua/?.lua;;'; server { listen 80; server_name img.hopesoft.org; root /home/wwwroot/img.hopesoft.org; #/thumbnail目錄下的圖片請求不經過縮略圖模塊 location ^~ /thumbnail/ { } #對類似_100x100.gif/jpg/png/jpeg進行縮略圖處理 location ~* _([0-9]+)x([0-9]+)\.(gif|jpg|png|jpeg)$ { #匹配文件名規則 root /home/wwwroot/img.hopesoft.org; #站點根目錄 set $image_root /home/wwwroot/img.hopesoft.org; #圖片目錄 set $thumbnail_root /home/wwwroot/img.hopesoft.org/thumbnail; #縮略圖存放目錄 #如果縮略圖文件存在,直接返回 set $file $thumbnail_root$uri; if (-f $file) { rewrite ^/(.*)$ /thumbnail/$1 last; } #如果縮略圖文件不存在,則應用縮略圖模塊處理 if (!-f $file) { rewrite_by_lua_file lua/thumbnail.lua; } } } include vhost/*.conf; } |
2、Lua腳本
/usr/local/openresty/nginx/lua/thumbnail.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
-- nginx thumbnail module
-- last update : 2014/8/21
-- version : 0.4.1
local
c =
require
'config'
--[[
uri :鏈接地址,如/goods/0007/541/001_328x328.jpg
ngx_img_root :圖片根目錄
ngx_thumbnail_root:縮略圖根目錄
img_width :縮略圖寬度
img_width :縮略圖高度
img_size :縮略圖寬x高
img_crop_type :縮略圖裁剪類型
cur_uri_reg_model :縮略圖uri正則規則
]]
local
uri
=
ngx.var.uri
local
ngx_img_root
=
ngx.var.image_root
local
ngx_thumbnail_root
=
ngx.var.thumbnail_root
local
img_width,img_height,img_size,img_crop_type
=
0
local
cur_uri_reg
=
c.default_uri_reg
--[[
日誌函數
log_level: 默認爲ngx.NOTICE
取值範圍:ngx.STDERR , ngx.EMERG , ngx.ALERT , ngx.CRIT , ngx.ERR ,
ngx.WARN , ngx.NOTICE , ngx.INFO , ngx.DEBUG
請配合nginx.conf中error_log的日誌級別使用
]]
function
lua_log(msg,log_level)
log_level
=
log_level
or
c.lua_log_level
if(c.enabled_log)
then
ngx.log(log_level,msg)
end
end
-- 匹配鏈接對應縮略圖規則
function
table.contains(table,element)
local
i
=
1
img_crop_type
=
0
for
_,
value
in
pairs(c.cfg)
do
local
dir
=
value['dir']
local
sizes
=
value['sizes']
local
uri_reg
=
value['uri_reg']
_,_,img_width,img_height
=
string.find(uri,''..dir..'+.*_([0-9]+)x([0-9]+)')
if(img_width
and
img_height
and
img_crop_type==0)
then
img_size
=
img_width..'x'..img_height
for
_,
value
in
pairs(sizes)
do
if(uri_reg)
then
lua_log('value[uri_reg]==='..uri_reg)
else
lua_log('value[uri_reg]===nil,dir='..dir..',cur_uri_reg='..cur_uri_reg)
end
cur_uri_reg
=
uri_reg
or
cur_uri_reg
if
(img_size
==
value)
then
img_crop_type=1
return
true
elseif
(img_size..'_'
==
value)
then
img_crop_type=2
return
true
elseif
(img_size..'!'
==
value)
then
img_crop_type=3
return
true
elseif
(img_size..'^'
==
value)
then
img_crop_type=4
return
true
elseif
(img_size..'>'
==
value)
then
img_crop_type=5
return
true
elseif
(img_size..'$'
==
value)
then
img_crop_type=6
img_size
=
img_width..'x'
return
true
end
end
end
i=i+1
end
return
false
end
-- 拼接gm命令
local
function
generate_gm_command(img_crop_type,img_original_path,img_size,img_thumbnail_path)
local
cmd
=
c.gm_path
..
' convert '
..
img_original_path
if
(img_crop_type
==
1)
then
cmd
=
cmd
..
' -thumbnail ' ..
img_size
..
' -background '
..
c.img_background_color
..
' -gravity center -extent '
..
img_size
elseif
(img_crop_type
==
2)
then
cmd
=
cmd
..
' -thumbnail ' ..
img_size
elseif
(img_crop_type
==
3)
then
cmd
=
cmd
..
' -thumbnail "' ..
img_size
..
'!" -extent '
..
img_size
elseif
(img_crop_type
==
4)
then
cmd
=
cmd
..
' -thumbnail "' ..
img_size
..
'^" -extent '
..
img_size
elseif
(img_crop_type
==
5
or
img_crop_type
==
6)
then
cmd
=
cmd
..
' -resize "' ..
img_size
..
'>"'
else
lua_log('img_crop_type
error:'..img_crop_type,ngx.ERR)
ngx.exit(404)
end
cmd
=
cmd
..
' '
..
img_thumbnail_path
return
cmd
end
lua_log("ngx_thumbnail_root======="..ngx_thumbnail_root)
if
not
table.contains(c.cfg,
uri)
then
lua_log(uri..'
is not match!',ngx.ERR)
ngx.exit(404)
else
lua_log(uri..'
is match!')
local
img_original_uri
=
string.gsub(uri,
cur_uri_reg,
'')
lua_log('img_original_uri_old==='
..
uri)
lua_log('cur_uri_reg==='
..
cur_uri_reg)
lua_log('img_original_uri_new==='
..
img_original_uri)
local
img_exist=io.open(ngx_img_root
..
img_original_uri)
if
not
img_exist
then
if
not
c.enabled_default_img
then
lua_log(img_original_uri..'
is not exist!',ngx.ERR)
ngx.exit(404)
else
img_exist=io.open(ngx_img_root
.. c.default_img_uri)
if
img_exist
then
lua_log(img_original_uri
..
' is not exist! crop image with default image')
img_original_uri
=
c.default_img_uri
else
lua_log(img_original_uri..'
is not exist!',ngx.ERR)
ngx.exit(404)
end
end
end
local
img_original_path =
ngx_img_root
..
img_original_uri
local
img_thumbnail_path
=
ngx_thumbnail_root
..
uri
local
gm_command
=
generate_gm_command(img_crop_type,img_original_path,img_size,img_thumbnail_path)
if
(gm_command)
then
lua_log('gm_command======'..gm_command)
_,_,img_thumbnail_dir,img__thumbnail_filename=string.find(img_thumbnail_path,'(.-)([^/]*)$')
os.execute('mkdir
-p '..img_thumbnail_dir)
os.execute(gm_command)
end
ngx.req.set_uri('/thumbnail'..uri)
end
|
/usr/local/openresty/nginx/lua/config.lua
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | -- nginx thumbnail module -- last update : 2014/8/21 -- version : 0.4.1 module(...,package.seeall) --[[ enabled_log: 是否打開日誌 lua_log_level: 日誌記錄級別 gm_path: graphicsmagick安裝目錄 img_background_color: 填充背景色 enabled_default_img: 是否顯示默認圖片 default_img_uri: 默認圖片鏈接 default_uri_reg: 縮略圖正則匹配模式,可自定義 _[0-9]+x[0-9] 對應:001_100x100.jpg _[0-9]+x[0-9]+[.jpg|.png|.gif]+ 對應:001.jpg_100x100.jpg ]] enabled_log = true lua_log_level = ngx.NOTICE gm_path = '/usr/local/graphicsmagick-1.3.18/bin/gm' img_background_color = 'white' enabled_default_img = true default_img_uri = '/default/notfound.jpg' default_uri_reg = '_[0-9]+x[0-9]+' --[[ 配置項,對目錄、縮略圖尺寸、裁剪類型進行配置,匹配後才進行縮略圖處理 1.sizes={'350x350'} 填充後保證等比縮圖 2.sizes={'300x300_'}等比縮圖 3.sizes={'250x250!'}非等比縮圖,按給定的參數縮圖(缺點:長寬比會變化) 4.sizes={'50x50^'}裁剪後保證等比縮圖 (缺點:裁剪了圖片的一部分) 5.sizes={'100x100>'}只縮小不放大 6.sizes={'140x140$'}限制寬度,只縮小不放大(比如網頁版圖片用於手機版時) dir="/" 對應根目錄,請放在default之前 dir="default" 對應默認圖片尺寸,當原圖不存在時,請求該尺寸會以默認圖片生成縮略圖 ]] cfg = { { dir = 'photos', sizes = {'50x50^','100x100>','140x140$','250x250!','300x300_','350x350'}, }, { dir = 'avatars', sizes = {'50x50^','80x80'}, }, { dir = 'mall', sizes = {'130x130!','228x228!','420x420!'}, uri_reg = '_[0-9]+x[0-9]+[.jpg|.png|.gif]+', }, { dir = 'default', sizes = {'50x50^','100x100>','140x140$','250x250!','300x300_','350x350','80x80'}, } } |
六、監控腳本
我們的文件上傳和刪除由web程序來完成,當web程序刪除原始文件時,需要同時刪除縮略圖,我們使用Linux的inotify來監控原始文件的變化,當有文件修改或刪除時,刪除相應的縮略圖(注:inotify需要linux的內核版本大於等於2.6.13)。
腳本文件/home/shell/monitor_thumbnail.sh內容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#!/bin/bash
basedir=/home/wwwroot/img.hopesoft.org/
hostname=img.hopesoft.org
thumbnaildir=thumbnail
excludedir=^${basedir}${thumbnaildir}
/usr/local/bin/inotifywait
--exclude
$excludedir
-mrq
--timefmt
'%d/%m/%y %H:%M'
--format
'%T %w%f %e'
--event
delete,modify ${basedir}
|
while
read date
time
file
event
do
if
[
"${file##*.}"
=
"jpg"
-o
"${file##*.}"
=
"jpeg" -o
"${file##*.}"
=
"gif"
-o
"${file##*.}"
=
"png"
];then
case
$event
in(DELETE|MODIFY)
tmpfile=${file/$hostname/$hostname\/$thumbnaildir};
filelist=${tmpfile%.*}_*.${tmpfile##*.};
for
File
in
$filelist;
do
#echo
"rm -rf "$File;
rm
-rf
$File
done
;;
esac
fi
done
|
1
2
3
4
5
|
#加入啓動
vi
/etc/rc.d/rc.local
#增加下行
nohup
/home/script/delete_thumbnail.sh
&
|
關於inotify的更多資料,請參考:http://www.1987.name/637.html
七、參考文檔
1、OpenResty官網
http://openresty.com/cn/index.html
2、HttpLuaModule模塊介紹
http://wiki.nginx.org/HttpLuaModule
3、[老王]Nginx與Lua
http://huoding.com/2012/08/31/156
4、[老王]嘗試使用GraphicsMagick的縮略圖功能
http://hi.baidu.com/thinkinginlamp/item/753d86383545d10fcfb9fe14
5、GraphicsMagick安裝及使用
http://www.cnblogs.com/javapro/archive/2013/04/28/3048393.html
6、Lua程序設計
http://book.luaer.cn/
7、靈活自定義縮略圖片尺寸大小方案分享(nginx,lua_nginx,GraphicsMagick)
http://www.iteye.com/topic/1125126
8、揭祕淘寶286億海量圖片存儲與處理架構
http://wenku.baidu.com/view/7dc77b2e7375a417866f8ff0.html
9、django-nginx-image
https://github.com/adw0rd/django-nginx-image
10、Lua中的正則表達式
http://blog.sina.com.cn/s/blog_512f462201016u3b.html
八、示例圖片(圖片來自老王博客)
原始圖片(160×120)
1、填充後保證等比縮圖(sizes={’350×350′} )
2、等比縮圖(sizes={’300x300_’})
3、非等比縮圖,按給定的參數縮圖,長寬比會變化(sizes={’250×250!’})
4、裁剪後保證等比縮圖,裁剪了圖片的一部分(sizes={’50×50^’})
5、只縮小不放大(sizes={’100×100>’})
6、限制寬度,只縮小不放大(比如網頁版圖片用於手機版時)(sizes={’140×140$’})
原圖不存在時
1、當配置顯示默認圖片,且路徑及縮略圖尺寸符合時,用默認圖片填充
2、當配置不顯示默認圖片或路徑及縮略圖尺寸不符合時,返回404
九、Github
項目地址:https://github.com/hopesoft/nginx-lua-image-module
問題反饋:https://github.com/hopesoft/nginx-lua-image-module/issues