Skip to content

Commit 1607975

Browse files
author
Jiang Liangdong
committed
The first release.
1 parent 2ec28bc commit 1607975

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

img.yourdomain.net

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
server {
2+
listen 80;
3+
server_name img.yourdomain.net;
4+
access_log /var/log/nginx/img.yourdomain.net_access.log;
5+
error_log /var/log/nginx/img.yourdomain.net_error.log;
6+
root /var/lib/imgroot;
7+
8+
location / {
9+
#let the ngx.var.uri available
10+
set $tmpuri $uri;
11+
12+
rewrite_by_lua '
13+
local imgrootdir = "/var/lib/imgroot"
14+
local lfs = require("lfs")
15+
local fpath = imgrootdir .. ngx.var.uri
16+
local ft = lfs.attributes(fpath, "mode")
17+
if ft == nil then
18+
local dir,file = string.match(ngx.var.uri, "(.*/)(.*)")
19+
if file == nil then
20+
ngx.log(ngx.ERR, "Invalid image file \'"..ngx.var.uri.."\'")
21+
ngx.exit(404)
22+
else
23+
local base,op,w,h,ext = string.match(file, "(.*)_([CS])_(%d+)x(%d+)%.(.*)")
24+
if op ~= "C" and op ~= "S" then
25+
return
26+
end
27+
if w == nil or h == nil then
28+
ngx.log(ngx.ERR, "Invalid spec")
29+
ngx.exit(404)
30+
end
31+
local orig = imgrootdir .. dir .. base .. "." .. ext
32+
local ft = lfs.attributes(orig, "mode")
33+
if ft == nil then
34+
ngx.log(ngx.ERR, string.format("File %s not found", orig))
35+
ngx.exit(404)
36+
end
37+
local cmd = string.format("/usr/bin/gm identify -format \'%%wx%%h\' \'%s\'", orig)
38+
local stdout = io.popen(cmd)
39+
local txt = stdout.read(stdout)
40+
local ow,oh = string.match(txt, "(%d+)x(%d+)")
41+
if ow == nil or oh == nil then
42+
ngx.log(ngx.ERR, string.format("Unable to decode the original file %s", orig))
43+
ngx.exit(404)
44+
end
45+
46+
w = tonumber(w)
47+
h = tonumber(h)
48+
ow = tonumber(ow)
49+
oh = tonumber(oh)
50+
if w == ow and h == oh then
51+
local uri = dir .. base .. "." .. ext
52+
ngx.req.set_uri(uri, true)
53+
return
54+
end
55+
local offx, offy, nw, nh
56+
if op == "C" then
57+
local wr = w / ow
58+
local hr = h / oh
59+
local r = math.max(wr, hr)
60+
nw = math.min(ow, w / r)
61+
nh = math.min(oh, h / r)
62+
offx = (ow - nw) / 2
63+
offy = (oh - nh) / 2
64+
w = math.min(w , nw)
65+
h = math.min(h , nh)
66+
end
67+
68+
local dstdir = imgrootdir .. dir
69+
local dstfnm = dstdir .. file
70+
local cmd = nil
71+
if op == "C" then
72+
cmd = string.format("/usr/bin/gm convert \'%s\' -crop %dx%d+%d+%d -resize %dx%d \'%s\'", orig, nw, nh, offx, offy, w, h, dstfnm)
73+
else
74+
cmd = string.format("/usr/bin/gm convert \'%s\' -thumbnail %dx%d -background white -gravity center -extent %dx%d \'%s\'", orig, w, h, w, h, dstfnm)
75+
end
76+
local ec = os.execute(cmd)
77+
if ec ~= 0 then
78+
ngx.log(ngx.ERR, string.format("Create thumbnail fail , exit code is %d, cmd is %s", ec, cmd))
79+
ngx.exit(404)
80+
end
81+
end
82+
end
83+
';
84+
}
85+
86+
error_page 500 502 503 504 /50x.html;
87+
location = /50x.html {
88+
root html;
89+
}
90+
}
91+

0 commit comments

Comments
 (0)