3d2e03be82
* check if failed when combine image * add status code when combine failed
36 lines
954 B
Lua
36 lines
954 B
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())
|
|
|
|
local data = req_body["data"]
|
|
local combined = file_dir .. "/" .. uuid.generate_v4() .. ".jpeg";
|
|
local len = table.getn(data)
|
|
local origin = {}
|
|
for i = 1, len do
|
|
origin[i] = file_dir .. "/" .. data[i] .. " "
|
|
end
|
|
-- linux
|
|
local magick = "/usr/local/ImageMagick/bin/magick"
|
|
local tmp = io.open(magick,"r")
|
|
if tmp == nil then
|
|
-- mac os
|
|
magick = "/usr/local/bin/magick"
|
|
else
|
|
io.close(tmp)
|
|
end
|
|
|
|
local result, _, code = os.execute(magick .. " convert -append " .. table.concat(origin, " ") .. " " .. combined)
|
|
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 |