-- 指定模块引用目录,否则无法加载同目录下的其他文件 --package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua'; package.path = package.path .. ';/?.lua'; -- Requires local uuid = require("lib.my_uuid") local cjson = require "cjson" local pl = require "pl.pretty" local http = require "resty.http" local verify = require('lib.jwt_verify') local get_wx_media = require("lib.wx_download") -- Verify token local httpc = http:new() local authHeader = ngx.req.get_headers().Authorization local sudoerToken = ngx.req.get_headers().HtySudoerToken verify(httpc, authHeader, sudoerToken) --uuid.seed() -- Get ngx var local file_dir = ngx.var.tmp_file_dir -- Read request body 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 images = req_body["payload"]["images"] local image_origin = req_body["payload"]["image_origin"] -- Judge payload image origin if image_origin == "WX" then ngx.log(ngx.INFO, "IMAGE ORIGIN -> ", image_origin) elseif image_origin == "LOCAL" then ngx.log(ngx.INFO, "IMAGE ORIGIN -> ", image_origin) else image_origin = "LOCAL" ngx.log(ngx.INFO, "IMAGE ORIGIN default to LOCAL") end -- Judge payload images para length local len = table.getn(images) if len == 0 then ngx.status = 500 ngx.log(ngx.ERR, "No images to combine!") return ; end -- If combine images from WX local wx_download_images = {} if image_origin == "WX" then for i = 1, len do ngx.log(ngx.INFO, "DOWNLOAD WX URL -> ", images[i]) wx_download_images[i] = get_wx_media(httpc, images[i], cjson, pl); end end -- Prepare combined file name and path local combined_uuid = uuid.uuid() local output_filename = combined_uuid .. ".jpeg"; local output_filepath = file_dir .. "/" .. output_filename local origin = {} -- 从 nginx 变量取 local convert = ngx.var.convert if not convert then ngx.status = 500 ngx.log(ngx.ERR, "ImageMagick NOT FOUND.") return end for i = 1, len do local incoming if image_origin == "WX" then ngx.log(ngx.INFO, "DOWNLOAD WX FILE -> ", wx_download_images[i]) incoming = wx_download_images[i] elseif image_origin == "LOCAL" then ngx.log(ngx.INFO, "LOCAL FILE -> ", images[i]) incoming = file_dir .. "/" .. images[i] else ngx.status = 400 ngx.log(ngx.ERR, "COMBINE REQUEST PAYLOAD IMAGES ORIGIN NOT FOUND.") return end origin[i] = incoming .. ".png " local cmd = convert .. " -resize 642x1389 -strip -quality 75% " .. incoming .. " " .. origin[i] ngx.log(ngx.INFO, 'CMD RESIZE -> ', cmd) local handle = io.popen(cmd) local result = handle:read("*a") ngx.log(ngx.INFO, 'CMD RESIZE -> RESULT ', pl.dump(result)) handle:close() end local cmd = convert .. " -append " .. table.concat(origin, " ") .. " " .. output_filepath ngx.log(ngx.INFO, 'CMD APPEND -> ', cmd) io.popen(cmd) ngx.say(output_filename)