|
轉(zhuǎn)載:http:///archives/112/
-
網(wǎng)站需求變更,需要更多不同尺寸的縮略圖
-
有些圖片的縮略圖很少使用到,但還是存在了硬盤上,造成空間浪費(fèi)
Nginx搭配Lua模塊,如果訪問的圖片不存在,則調(diào)用GraphicsMagick的命令行實時生成指定尺寸的圖片。
-集成了Lua模塊的Nginx項目OpenResty
-GraphicsMagick的安裝和使用
-具體使用方法
原始圖片地址:
/images/f47aa98b47b4b7bd.jpg
自定義圖片尺寸:
/images/f47aa98b47b4b7bd_40x40.jpg
配置文件中可以寫成這樣
location ~ '/images/([0-9a-z]+)_([0-9]+)x([0-9]+).jpg$' {
root /home/images;
set $image_root = '/home/images';
set $fileName = ngx.arg[1];
set $width = ngx.arg[2];
set $height = ngx.arg[3];
set $origin = $image_root/$fileName.jpg
set $file = $image_root/$fileName_$widthx$height.jpg
if (!-f $file) {
rewrite_by_lua '
local command = "gm convert "..ngx.var.origin.." -thumbnail "..ngx.var.width.."x"
..ngx.var.height.." "..ngx.var.file;
os.execute(command);
';
}
這樣就能簡單的生成圖片指定尺寸的縮略圖了。
摘自:http://blog.csdn.NET/vboy1010/article/details/7868645
安裝lua模塊
1、Luajit2.0(推薦)或者 Lua5.1(Lua5.2暫不支持)
-
wget http:///download/LuaJIT-2.0.0-beta9.tar.gz
-
tar zxvf LuaJIT-2.0.0-beta9.tar.gz
-
cd LuaJIT-2.0.0-beta9
-
make
-
sudo make install PREFIX=/usr/local/luajit
Note: to avoid overwriting a previous version,
the beta test releases only install the LuaJIT executable under the versioned name (i.e. luajit-2.0.0-beta10).
You probably want to create a symlink for convenience, with a command like this:
sudo ln -sf luajit-2.0.0-beta9 /usr/local/bin/luajit (加上這句命令)
下面需要配置一下 luajit 或 lua 的環(huán)境變量(Nginx編譯時需要):
-
-- luajit --
-
# tell nginx's build system where to find LuaJIT:
-
export LUAJIT_LIB=/path/to/luajit/lib
-
export LUAJIT_INC=/path/to/luajit/include/luajit-2.0
-
-
-- lua --
-
# or tell where to find Lua if using Lua instead:
-
export LUA_LIB=/path/to/lua/lib
-
export LUA_INC=/path/to/lua/include
我的測試環(huán)境里,配置如下:
-
export LUAJIT_LIB=/usr/local/luajit/lib
-
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0
|