Skip to content

根据请求url中图片的尺寸,Nginx利用lua脚本调用GraphicsMagick工具对图片进行处理,返回给客户端。

Notifications You must be signed in to change notification settings

ahaii/Nginx-Lua-FastDFS-GraphicsMagick

Repository files navigation

Nginx-Lua-FastDFS-GraphicsMagick

我们通常使用FastDFS来当作图片服务器,客户端通过Nginx请求图片。某些情况下,客户端对请求的图片有尺寸要求,
http://ip/group1/M00/00/00/wKhnBFiH_2KAOAH-AA1rIuRd3Es633.jpg_500x600.jpg
这时我们可以使用GraphicsMagick工具动态的修改图片以满足客户端的需求。
这里使用lua脚本,调用GraphicsMagick的gm命令动态处理图片。

整体思路:

1、首先服务器需要有lua环境
2、Nginx扩展支持lua,可调用lua脚本
3、lua脚本中定义gm命令及参数

安装配置:

1、安装lua环境
tar zxvf LuaJIT-2.0.2.tar.gz
cd LuaJIT-2.0.2
make && make install

2、配置lua环境变量
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.0

3、安装GraphicsMagick
tar zxvf GraphicsMagick-1.3.21.tar.gz
cd GraphicsMagick
./configure --prefix=/data/local/GraphicsMagick --enable-shared
make && make install

查看GraphicsMagick 支持的文件类型:
/data/local/GraphicsMagick/bin/gm -version

Feature Support:
  Native Thread Safe       yes
  Large Files (> 32 bit)   yes
  Large Memory (> 32 bit)  yes
  BZIP                     yes
  DPS                      no
  FlashPix                 no
  FreeType                 no
  Ghostscript (Library)    no
  JBIG                     no
  JPEG-2000                no
  JPEG                     yes
  Little CMS               no
  Loadable Modules         no
  OpenMP                   yes (201107)
  PNG                      yes
  TIFF                     no
  TRIO                     no
  UMEM                     no
  WebP                     no
  WMF                      no
  X11                      no
  XML                      no
  ZLIB                     yes

Host type: x86_64-unknown-linux-gnu

若PNG、JPEG等不支持,可以在编译GraphicsMagick的时候,使用 --with-png=yes

4、准备lua模块
ngx_devel_kit-0.2.18.tar.gz
v0.8.6.tar.gz(lua-nginx-module-0.8.6

5、安装Nginx
tar zxvf nginx-1.4.2.tar.gz
cd nginx-1.4.2
./configure —prefix=/usr/local/nginx —add-module=lua-nginx-module-0.8.6 —add-module=ngx_devel_kit-0.2.18
make && make install

测试nginx扩展lua是否正常:

location /test {
    default_type text/html;
    content_by_lua '
        ngx.say("hello world")
        ngx.log(ngx.ERR,"err err")
    ';
}

若返回hello world即说明正常

6、启动Nginx
若出错,如/usr/local/nginx/sbin/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
请按如下操作:
find / -name libluajit-5.1.so.2

ln -s /usr/local/lib/libluajit-5.1.so.2 /usr/lib64/libluajit-5.1.so.2

7、lua脚本ImageResizer.lua

local command = "/data/local/GraphicsMagick/bin/gm convert " .. ngx.var.request_filepath .. " -resize " .. ngx.var.width .. "x" .. ngx.var.height .. " +profile \"*\" " .. ngx.var.request_filepath .. "_" .. ngx.var.width .. "x" .. ngx.var.height .. "." ..                           
ngx.var.ext;os.execute(command);
ngx.exec(ngx.var.request_uri);

Nginx配置请见nginx.conf

About

根据请求url中图片的尺寸,Nginx利用lua脚本调用GraphicsMagick工具对图片进行处理,返回给客户端。

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages