1. 程式人生 > >Nginx-Lua-FastDFS-GraphicsMagick動態圖片縮圖

Nginx-Lua-FastDFS-GraphicsMagick動態圖片縮圖

Nginx-Lua-FastDFS-GraphicsMagick動態圖片縮圖

公司使用FastDFS來當作圖片伺服器,客戶端通過Nginx請求圖片。某些情況下,客戶端對請求的圖片有尺寸要求, 
 http://10.100.1.145/group1/M00/00/18/CmQBkVsSjbiARQKhAAAHLpECb3c407.jpg_400x400.jpg 
這時我們可以使用GraphicsMagick工具動態的修改圖片以滿足客戶端的需求。 
這裡使用lua指令碼,呼叫GraphicsMagick的gm命令動態處理圖片。

FastDFS+Nginx的安裝配置步驟請見:https://blog.csdn.net/danielzhou888/article/details/80563355

安裝包來源請自行百度,謝謝。

整體思路:

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

user  root;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    lua_package_path "/usr/local/nginx/conf/lua/?.lua;;";

server {
    listen       80;
    server_name localhost;

    location /group1/M00 {
        root /home/fastdfs/fastdfs_storage_data/data;

    if ($uri ~* ^/group1/M00(.+\.(jpg|jpeg|gif|png))_(\d+)x(\d+)\.(jpg|jpeg|gif|png)) {       
                add_header X-Powered-By 'Lua GraphicsMagick';
                add_header file-path $request_filename;
                lua_code_cache on;
                set $request_filepath /home/fastdfs/fastdfs_storage_data/data$1;
                set $width $3;
                set $height $4;
                set $ext $5;
    }

        if (!-f $request_filename) {
            content_by_lua_file conf/lua/ImageResizer.lua;
    }
    }
}

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
lua配置請見ImageResizer.lua
if(ngx.var.height ~= '' and ngx.var.width ~= '') then
    height = tonumber(ngx.var.height);
    width = tonumber(ngx.var.width);
    if(height > 1000) then
        height = 1000
    end
    if(width > 1000) then
        height = 1000
    end
    local command = "/usr/local/GraphicsMagick/bin/gm convert " .. ngx.var.request_filepath .. " -resize " .. width .. "x" .. 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);
else
    ngx.exit(ngx.HTTP_NOT_FOUND);
end