FastDFS 之图片裁剪lua脚本

1.nginx配置

#
server {
        listen 8801;
        server_name 10.1.8.54;

        location ~ /group[1-3]/M00 {
                #root /data/fastdfs/data;
                alias /data/fastdfs/data/data;

                # fastdfs生成缩略图
                set $image_root "/data/fastdfs/data/data";
                if ($uri ~ "/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/(.*)") {
                  set $image_dir "$image_root/$3/$4/";
                  set $image_name "$5";
                  set $file "$image_dir$image_name";
                }

                if (!-f $file) {
                  # 关闭lua代码缓存,方便调试lua脚本
                  #lua_code_cache off;
                  content_by_lua_file "/usr/local/nginx/conf/lua/fastdfs.lua";
                }
                ngx_fastdfs_module;
        }

        # 根目录下返回403
        location = / {
                return 403;
        }

        include extra/lua.conf;
        include extra/status.conf;
        # log file
        access_log  logs/img_access.log access;
}

2.fastdfs-lua脚本

-- 写入文件
local function writefile(filename, info)
    local wfile=io.open(filename, "w") --写入文件(w覆盖)
    assert(wfile)  --打开时验证是否出错		
    wfile:write(info)  --写入传入的内容
    wfile:close()  --调用结束后记得关闭
end

-- 检测路径是否目录
local function is_dir(sPath)
    if type(sPath) ~= "string" then return false end

    local response = os.execute( "cd " .. sPath )
    if response == 0 then
        return true
    end
    return false
end

-- 检测文件是否存在
local file_exists = function(name)
    local f=io.open(name,"r")
    if f~=nil then io.close(f) return true else return false end
end

-- 反向查找路径
function last_find(str, k)
	local ts = string.reverse(str);
	local _, i = string.find(ts, k);
    return string.len(ts) - i + 1;
end

local area = nil
local originalUri = ngx.var.uri;
local originalFile = ngx.var.file;
local index = last_find(ngx.var.uri, "([0-9]+)x([0-9]+)");  
if index then 
    originalUri = string.sub(ngx.var.uri, 0, index-2);  
    area = string.sub(ngx.var.uri, index);  
    index = string.find(area, "([.])");  
    area = string.sub(area, 0, index-1);  

    local index = last_find(originalFile, "([0-9]+)x([0-9]+)");  
    originalFile = string.sub(originalFile, 0, index-2)
end

-- check original file
if not file_exists(originalFile) then
    local fileid = string.sub(originalUri, 2);
    -- main
    local fastdfs = require('restyfastdfs')
    local fdfs = fastdfs:new()
    fdfs:set_tracker("10.1.8.43", 22122)
    fdfs:set_tracker("10.1.8.44", 22122)
    fdfs:set_timeout(1000)
    fdfs:set_tracker_keepalive(0, 100)
    fdfs:set_storage_keepalive(0, 100)
    local data = fdfs:do_download(fileid)
    if data then
       -- check image dir
        if not is_dir(ngx.var.image_dir) then
            os.execute("mkdir -p " .. ngx.var.image_dir)
        end
        writefile(originalFile, data)
    end
end

-- 创建缩略图
-- local image_sizes = {"50x50", "100x100", "400x400", "640x320","600x600","240x150","300x300","90x90","130x130","150x150","180x180","230x230","270x270"};
local image_sizes = {"50x50","100x100", "150x150", "200x200", "300x300", "400x400","600x600","800x800"};
function table.contains(table, element)  
    for _, value in pairs(table) do  
        if value == element then
            return true  
        end  
    end  
    return false  
end 

if table.contains(image_sizes, area) then  
    local bg;
    if string.lower(string.sub(ngx.var.file,-3))=="jpg" then
        bg=" -background white ";
    else
        bg=" -background transparent ";
    end;
    local command = "/usr/local/GraphicsMagick/bin/gm convert -quality 90 " .. originalFile  .. " -thumbnail " .. area .. bg .. " -gravity center -extent " .. area .. " " .. ngx.var.file;  
    os.execute(command);  
end;

if file_exists(ngx.var.file) then
    --ngx.req.set_uri(ngx.var.uri, true);  
    ngx.exec(ngx.var.uri)
else
    ngx.exit(404)
end

 

posted @ 2018-07-18 08:50  sunmmi  阅读(526)  评论(0)    收藏  举报