fastdfs支持动态缩略图
一, 源码包准备:
GraphicsMagick-1.3.20.tar.gz
jpegsrc.v9a.tar.gz
lua-5.3.0.tar.gz
LuaJIT-2.0.2.tar.gz
lua-nginx-module-0.9.14.tar.gz
ngx_devel_kit0.2.19.tar.gz
git clone https://github.com/agentzh/echo-nginx-module
restyfastdfs.lua http://download.csdn.net/detail/dwl764457208/9216459
fastdfs.lua http://download.csdn.net/detail/dwl764457208/9216463
安装系统的相关依赖包
yum install libtermcap-devel ncurses-devel libevent-devel readline-devel pcre-*
安装lua-5.3.0
tar -xzvf lua-5.3.0.tar.gz
cd lua-5.3.0/
make linux
make install
安装LuaJIT-2.0.2:
tar -zxvf LuaJIT-2.0.2.tar.gz
cd LuaJIT-2.0.2
make
make install
安装jpegsrc.v9a.tar.gz
tar -xzvf GraphicsMagick-1.3.20.tar.gz
cd GraphicsMagick-1.3.20/
./configure
make
make install
安装lua-nginx-module-0.9.16
tar -xzvf lua-nginx-module-0.9.16.tar.gz
安装ngx_devel_kit0.2.19:
tar -xzvf ngx_devel_kit0.2.19.tar.gz
建立软链接
ln -sv /usr/local/lib/libjpeg.so.9 /lib64/libjpeg.so.9
ln -sv /usr/local/lib/libluajit-5.1.so /lib64/liblua-5.1.so.2
chmod -R 777 /mnt/fastdfs_storage_data/ 访问图片需要的权限
将fastdfs.lua和restyfastdfs.lua拷贝至/etc/fdfs
其中修改fastdfs.lua
fdfs:set_tracker(“192.168.65.133”,22122)
修改一下nginx配置文件属性:
vi /etc/nginx/nginx.conf
worker_processes 4;
events {
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
lua_package_path "/etc/fdfs/?.lua;;";
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
default_type text/html;
content_by_lua 'ngx.say("hello,world
")';
}
location /group1/M00 {
alias /mnt/fastdfs_storage_data;
set $image_root "/mnt/fastdfs_storage_data/data";
if ($uri ~ "/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/(.*)") {
set $image_dir "$image_root/$3/$4/";
set $image_name "$5";
set $file "$image_dir$image_name";
}
if (!-f $file) {
# 关闭lua代码缓存,方便调试lua脚本
lua_code_cache off;
content_by_lua_file "/etc/fdfs/fastdfs.lua";
}
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
重新编译nginx
mkdir /etc/nginx
cd nginx-1.9.6
./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin --conf-path=/etc/nginx/nginx.conf --with-pcre
--add-module=/usr/local/ngx_devel_kit-0.2.19
--add-module=/usr/local/lua-nginx-module-0.9.16
--add-module=/usr/local/echo-nginx-module/echo-nginx-module
--add-module=/usr/local/fastdfs-nginx-module/src
--with-http_gzip_static_module
--with-http_stub_status_module --with-http_ssl_module
make
make install
service nginx restart
fastdfs.lua脚本内容:由于restyfastdfs.lua脚本内容太多。可以下载。
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.65.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 .. " -resize " .. 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