-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 208235d
Showing
5 changed files
with
861 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
nginx-lua-fastdfs-GraphicsMagick | ||
================== | ||
fastdfs开源的分布式文件系统,此脚本利用nginx lua模块,动态生成图片缩略图,fastdfs只存一份原图。lua通过socket获取fastdfs的原图,并存放到本地,根据不同规则url,例如:_60x60.jpg、_80x80.jpg,类似淘宝图片url规则。利用gm命令生成本地缩略图,第二次访问直接返回本地图片。定时任务凌晨清除7天内未访问的图片,节省空间。 | ||
|
||
图片访问举例 | ||
---------------- | ||
1. [http://192.168.1.113/group1/M00/00/00/wKgBcVN0wDiAILQXAAdtg6qArdU189.jpg](http://192.168.1.113/group1/M00/00/00/wKgBcVN0wDiAILQXAAdtg6qArdU189.jpg) | ||
2. [http://192.168.1.113/group1/M00/00/00/wKgBcVN0wDiAILQXAAdtg6qArdU189.jpg_80x80.jpg](http://192.168.1.113/group1/M00/00/00/wKgBcVN0wDiAILQXAAdtg6qArdU189.jpg_80x80.jpg) | ||
3. [http://gi1.md.alicdn.com/imgextra/i1/401612253/T2ASPfXE4XXXXXXXXX_!!401612253.jpg_60x60.jpg](http://gi1.md.alicdn.com/imgextra/i1/401612253/T2ASPfXE4XXXXXXXXX_!!401612253.jpg_60x60.jpg) | ||
4. [http://gi1.md.alicdn.com/imgextra/i1/401612253/T2ASPfXE4XXXXXXXXX_!!401612253.jpg_80x80.jpg](http://gi1.md.alicdn.com/imgextra/i1/401612253/T2ASPfXE4XXXXXXXXX_!!401612253.jpg_80x80.jpg) | ||
|
||
|
||
参考网址 | ||
---------------- | ||
1. [https://github.com/openresty/lua-nginx-module](https://github.com/openresty/lua-nginx-module) | ||
2. [https://github.com/azurewang/Nginx_Lua-FastDFS](https://github.com/azurewang/Nginx_Lua-FastDFS) | ||
3. [https://github.com/azurewang/lua-resty-fastdfs](https://github.com/azurewang/lua-resty-fastdfs) | ||
4. [http://rhomobi.com/topics/23](http://rhomobi.com/topics/23) | ||
5. [http://bbs.chinaunix.net/thread-4133106-1-1.html](http://bbs.chinaunix.net/thread-4133106-1-1.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# 凌晨2点执行,查找目录下面7天内没有被访问的文件并删除,释放空间 | ||
0 2 * * * find /data/images -atime -7 | xargs rm -rf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
-- 写入文件 | ||
local function writefile(filename, info) | ||
local wfile=io.open(filename, "w") --写入文件(w覆盖) | ||
assert(wfile) --打开时验证是否出错 | ||
wfile:write(info) --写入传入的内容 | ||
wfile:close() --调用结束后记得关闭 | ||
end | ||
|
||
-- 检测路径是否目录 | ||
local function is_dir(sPath) | ||
if type(sPath) ~= "string" then return false end | ||
|
||
local response = os.execute( "cd " .. sPath ) | ||
if response == 0 then | ||
return true | ||
end | ||
return false | ||
end | ||
|
||
-- 检测文件是否存在 | ||
local file_exists = function(name) | ||
local f=io.open(name,"r") | ||
if f~=nil then io.close(f) return true else return false end | ||
end | ||
|
||
local area = nil | ||
local originalUri = ngx.var.uri; | ||
local originalFile = ngx.var.file; | ||
local index = string.find(ngx.var.uri, "([0-9]+)x([0-9]+)"); | ||
if index then | ||
originalUri = string.sub(ngx.var.uri, 0, index-2); | ||
area = string.sub(ngx.var.uri, index); | ||
index = string.find(area, "([.])"); | ||
area = string.sub(area, 0, index-1); | ||
|
||
local index = string.find(originalFile, "([0-9]+)x([0-9]+)"); | ||
originalFile = string.sub(originalFile, 0, index-2) | ||
end | ||
|
||
-- check original file | ||
if not file_exists(originalFile) then | ||
local fileid = string.sub(originalUri, 2); | ||
-- main | ||
local fastdfs = require('restyfastdfs') | ||
local fdfs = fastdfs:new() | ||
fdfs:set_tracker("192.168.1.113", 22122) | ||
fdfs:set_timeout(1000) | ||
fdfs:set_tracker_keepalive(0, 100) | ||
fdfs:set_storage_keepalive(0, 100) | ||
local data = fdfs:do_download(fileid) | ||
if data then | ||
-- check image dir | ||
if not is_dir(ngx.var.image_dir) then | ||
os.execute("mkdir -p " .. ngx.var.image_dir) | ||
end | ||
writefile(originalFile, data) | ||
end | ||
end | ||
|
||
-- 创建缩略图 | ||
local image_sizes = {"80x80", "800x600", "40x40", "60x60"}; | ||
function table.contains(table, element) | ||
for _, value in pairs(table) do | ||
if value == element then | ||
return true | ||
end | ||
end | ||
return false | ||
end | ||
|
||
if table.contains(image_sizes, area) then | ||
local command = "gm convert " .. originalFile .. " -thumbnail " .. area .. " -background gray -gravity center -extent " .. area .. " " .. ngx.var.file; | ||
os.execute(command); | ||
end; | ||
|
||
if file_exists(ngx.var.file) then | ||
--ngx.req.set_uri(ngx.var.uri, true); | ||
ngx.exec(ngx.var.uri) | ||
else | ||
ngx.exit(404) | ||
end |
Oops, something went wrong.