add combine image logic (#14)
* add combine image logic * remove unnecessary log
This commit is contained in:
+32
-5
@@ -1,5 +1,32 @@
|
||||
---
|
||||
--- Generated by EmmyLua(https://github.com/EmmyLua)
|
||||
--- Created by moicen.
|
||||
--- DateTime: 2021/9/4 12:28
|
||||
---
|
||||
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
|
||||
-- in linux
|
||||
local magick = "/usr/local/ImageMagick/bin/magick"
|
||||
local tmp = io.open(magick,"r")
|
||||
if tmp == nil then
|
||||
-- in mac os
|
||||
magick = "/usr/local/bin/magick"
|
||||
else
|
||||
io.close(tmp)
|
||||
end
|
||||
local file = io.popen(magick .. " convert -append " .. table.concat(origin, " ") .. " " .. combined)
|
||||
file:read("*all")
|
||||
file:close()
|
||||
file = io.open(combined)
|
||||
local content = file:read("*all")
|
||||
file:close()
|
||||
os.remove(combined)
|
||||
ngx.say(content)
|
||||
+14
-4
@@ -21,8 +21,15 @@ while true do
|
||||
end
|
||||
|
||||
if typ == "header" then
|
||||
file_name = string.format("%s/%s.jpeg", file_dir, uuid())
|
||||
ngx.log(ngx.ERR, "file name", file_name)
|
||||
|
||||
--"Content-Disposition","form-data; name=\"files[]\"; filename=\"Song-of-joy.png\""
|
||||
--"Content-Disposition","form-data; name=\"files[]\"; filename=\"rhythm-recognition-1.png\""
|
||||
local key, val = res
|
||||
if (key == "Content-Disposition") then
|
||||
local ext = ngx.re.match(val, [[filename=".*\.(\w+)"]], "jo")
|
||||
file_name = string.format("%s/%s.%s", file_dir, uuid(), ext)
|
||||
end
|
||||
|
||||
if file_name then
|
||||
file = io.open(file_name, "w+")
|
||||
if not file then
|
||||
@@ -45,13 +52,16 @@ while true do
|
||||
elseif typ == "eof" then
|
||||
local http = require "resty.http"
|
||||
local httpc = http.new()
|
||||
local remote_url = string.format("%s/api/v1/ts/task_server/create_task", task_server)
|
||||
local remote_url = string.format("%s/api/v1/ts/create_task", task_server)
|
||||
local res, err = httpc:request_uri(
|
||||
remote_url,
|
||||
{
|
||||
method = "POST",
|
||||
headers = {
|
||||
host = "ngx"
|
||||
},
|
||||
body = {
|
||||
cjson.encode({task_type = 1, data = files, created_by = 'openresty'})
|
||||
cjson.encode({task_type = 1, data = files, created_by = 'ngx', updated_by = 'ngx'})
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user