Files
resty_functions/combine.lua
T
2021-11-18 20:02:01 +08:00

48 lines
1.2 KiB
Lua

local uuid = require "resty.jit-uuid"
local cjson = require "cjson"
uuid.seed()
local file_dir = ngx.var.tmp_file_dir
ngx.req.read_body()
local req_body = cjson.decode(ngx.req.get_body_data())
ngx.log(ngx.INFO, 'REQ_BODY -> ', ngx.req.get_body_data())
local data = req_body["data"]["images"]
local combined = file_dir .. "/" .. uuid.generate_v4() .. ".jpeg";
local len = table.getn(data)
if len == 0 then
ngx.status = 500
ngx.say("No images to combine!")
return;
end
local origin = {}
for i = 1, len do
ngx.log(ngx.INFO, "COMBINE FILE ->", data[i])
origin[i] = file_dir .. "/" .. data[i] .. " "
end
-- 从 nginx 变量取
local magick = ngx.var.magick
local convert = ngx.var.convert
if not magick then
if not convert then
ngx.status = 500
ngx.say("ImageMagick NOT FOUND.")
return
end
magick = ""
end
local cmd = magick .. " convert -append " .. table.concat(origin, " ") .. " " .. combined
ngx.log(ngx.INFO, 'CMD -> ', cmd)
local result, _, code = os.execute(cmd)
if result and code == 0 then
local file = io.open(combined)
local content = file:read("*all")
file:close()
os.remove(combined)
ngx.say(content)
else
ngx.status = 500
ngx.say("IMAGE COMBINE *FAILED*")
end