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);
	if(not i) then return false end
    return string.len(ts) - i + 1;
end



function table.contains(table, element)  
    for _, value in pairs(table) do  
        if value == element then
            return true  
        end  
    end  
    return false  
end 

local nginx_image_dir=ngx.var.image_dir;
local nginx_uri=ngx.var.uri;
local nginx_file=ngx.var.file;
-- local nginx_uri="test.png_5_2.png";
-- local nginx_file=nginx_uri;

local area = nil
local originalUri = nginx_uri;
local originalFile = nginx_file;
local index = last_find(nginx_uri, "([0-9]+)x([0-9]+)");  
if index then 
    originalUri = string.sub(nginx_uri, 0, index-2);  
    area = string.sub(nginx_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)
else 
	index = last_find(nginx_uri, "([0-9]+)_([0-9]+)");  
	
	if index then 
		originalUri = string.sub(nginx_uri, 0, index-2);  
		area = string.sub(nginx_uri, index);  
		index = string.find(area, "([.])");  
		area = string.sub(area, 0, index-1);  

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

end


if not file_exists(originalFile) then
    local fileid = string.sub(originalUri, 2);
    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(nginx_image_dir) then
            os.execute("mkdir -p " .. nginx_image_dir)
        end
        writefile(originalFile, data)
    end
end

if not file_exists(originalFile) then
	ngx.exit(404)
end


local image_sizes = {"50x50","100x100", "150x150", "200x200", "300x300", "400x400","600x600","800x800"};


if table.contains(image_sizes, area) then  
    local bg;
    if string.lower(string.sub(nginx_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 .. " " .. nginx_file;  
    os.execute(command);  
end;

local image_crops = {"5_0","5_1","5_2","5_3","5_4"};
if table.contains(image_crops, area) then  
		
	local sindex= string.find(area,"_");
	local now=string.sub(area,sindex+1);
	local split=string.sub(area,1,sindex-1);
	local filepath=originalFile;
	now=math.ceil(now);
	split=math.ceil(split);
	local infoexe=gm_exe.." identify "..filepath;
	local handle = io.popen(infoexe);
	local result = handle:read("*a")
	handle:close()
	local height=string.match(result,"[0-9]+x([0-9]+)");
	local width=string.match(result,"([0-9]+)x[0-9]+");
	if (height and now<split and now>=0 and split >0) then
		height=math.ceil(height/split);
		local offsettop=math.ceil(height*now);
		local command=gm_exe.." convert "..filepath.." -crop "..width.."x"..height.."+0+"..offsettop.." "..nginx_file;
		-- print(command);
		os.execute(command);
	end;
	
end;


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

 

 

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