Files
resty_functions/scripts/combine.lua
T

45 lines
1.3 KiB
Lua
Raw Normal View History

-- 指定模块引用目录,否则无法加载同目录下的其他文件
2022-02-08 13:57:15 +08:00
--package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua';
2022-02-08 14:14:57 +08:00
package.path = package.path .. ';<SCRIPT_PATH>/?.lua';
2022-02-08 13:57:15 +08:00
local uuid = require "resty.jit-uuid"
local cjson = require "cjson"
2021-09-06 18:56:59 +08:00
uuid.seed()
local file_dir = ngx.var.tmp_file_dir
ngx.req.read_body()
local req_body = cjson.decode(ngx.req.get_body_data())
2021-09-24 20:18:39 +08:00
ngx.log(ngx.INFO, 'REQ_BODY -> ', ngx.req.get_body_data())
2022-02-11 22:16:59 +08:00
local data = req_body["payload"]["images"]
2022-03-08 21:59:25 +08:00
2022-03-09 01:57:15 +08:00
local combined_uuid = uuid.generate_v4()
2022-08-22 23:59:02 +08:00
local combined = file_dir .. "/" .. combined_uuid .. ".png";
2022-03-08 21:59:25 +08:00
2021-09-06 18:56:59 +08:00
local len = table.getn(data)
2021-09-24 20:18:39 +08:00
if len == 0 then
ngx.status = 500
2021-11-20 03:37:12 +08:00
ngx.log(ngx.ERR, "No images to combine!")
return ;
2021-09-24 20:18:39 +08:00
end
2021-09-06 18:56:59 +08:00
local origin = {}
for i = 1, len do
ngx.log(ngx.INFO, "COMBINE FILE ->", data[i])
2022-08-22 23:59:02 +08:00
local cmd = convert .. " -resize 1280x " .. data[i] .. " " .. data[i] .. ".png"
ngx.log(ngx.INFO, 'CMD RESIZE -> ', cmd)
origin[i] = file_dir .. "/" .. data[i] .. ".png "
2021-09-06 18:56:59 +08:00
end
2021-09-12 11:01:41 +08:00
-- 从 nginx 变量取
2021-09-12 11:29:30 +08:00
local convert = ngx.var.convert
2021-09-12 11:01:41 +08:00
if not convert then
ngx.status = 500
ngx.log(ngx.ERR, "ImageMagick NOT FOUND.")
return
2021-09-06 18:56:59 +08:00
end
2021-09-07 21:54:52 +08:00
local cmd = convert .. " -append " .. table.concat(origin, " ") .. " " .. combined
2022-08-22 23:59:02 +08:00
ngx.log(ngx.INFO, 'CMD APPEND -> ', cmd)
2022-03-08 21:59:25 +08:00
io.popen(cmd)
ngx.say(combined_uuid)