8244 发表于 2016-12-26 07:48:30

FASTDFS(六)nginx+lua+GraphicsMagick 图片自动缩放

1.安装GraphicsMagick
--使用yum安装GraphicsMagick

# yum install ImageMagick

--查看安装结果

# yum list installed | grep ImageMagick*
ImageMagick.x86_64   6.5.4.7-7.el6_5@base
--验证安装结果

# convert -sample 200x200 desktop.jpg desktop-200x200.jpg
# convert -sample 200x200 desktop.png desktop-200x200.png
2.安装lua-nginx-module
--下载安装LuaJIT

# wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz
# tar -zxvf LuaJIT-2.0.4.tar.gz
# cd LuaJIT-2.0.2
# make
# make install
--安装lua-nginx-module
下载ngx_devel_kit,nginx_lua_module解压

// 先导入环境变量,告诉nginx去哪里找luajit
# export LUAJIT_LIB=/usr/local/lib
# export LUAJIT_INC=/usr/local/include/luajit-2.0
// 查看ngixn版本极其编译参数
# /usr/local/nginx/sbin/nginx -V
// 添加ngx_devel_kit,lua-nginx-module模块,重新编译nginx
// 切勿make install,否则就成了覆盖安装
# ./configure --prefix=/usr/local/nginx \
--add-module=/usr/local/src/fastdfs-nginx-module/src \
--add-module=/usr/local/src/ngx_devel_kit-0.2.19 \
--add-module=/usr/local/src/lua-nginx-module-0.9.16
# make
// 备份旧的nginx程序,用新的nginx程序覆盖旧的
# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
# cp ./objs/nginx /usr/local/nginx/sbin/nginx
// 再次查看ngixn版本极其编译参数,确认安装成功
# /usr/local/nginx/sbin/nginx -V
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
// 将libluajit-5.1.so.2安装到/usr/lib中并重新加载
# ln -s /usr/local/lib/libluajit-5.1.so.2 /usr/lib/libluajit-5.1.so.2
# ldconfig
# /usr/local/nginx/sbin/nginx -V
// 测试lua-nginx-module模块
// nginx配置文件加入如下配置:
location ~* ^/2328(/.*) {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, ttlsa lua")';
}
# curl http://localhost/2328/
hello, ttlsa lua
--配置nginx实现简单自动生成缩略图

// 修改nginx配置文件nginx.conf
location ~ '/images/(+).jpg$' {
root /var;
}
location ~ '/images/(+)_(+)x(+).jpg$' {
root /var;
set $image_root '/var/images';
set $fileName $1;
set $width $2;
set $height $3;
set $origin $image_root/$fileName.jpg;
set $file $image_root/${fileName}_${width}x$height.jpg;
if (!-f $file) {
rewrite_by_lua '
local command = "convert -sample "..ngx.var.width.."x"..ngx.var.height.." "..ngx.var.origin.." "..ngx.var.file;
os.execute(command);
';
}
}
// nginx重新加载配置
# /usr/local/nginx/sbin/nginx -s reload
// 在/var/images中上传desktop.jpg图片
访问
http://192.168.117.101/images/desktop_200x200.jpg
返回404
// 查看日志
# tail -f /usr/local/nginx/logs/error.log
convert: unable to open image `/var/images/desktop_100x100.jpg': Permission denied @ blob.c/OpenBlob/2480.
// nginx: worker process 的用户是nobody,没有root权限,无法操作/var/images的文件
// 修改/var/images的权限为所有人可修改# ps -ef | grep nginx
root   10065   10 00:05 ?      00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   12100 100650 11:35 ?      00:00:00 nginx: worker process      
root   1210899590 11:37 pts/1    00:00:00 grep nginx
访问
http://192.168.117.101/images/desktop_200x200.jpg
返回缩略图,/var/images多了对应的缩略图
  --配置nginx实现简单自动生成缩略图
  --进阶,将缩略图文件和原图分开存储

location ~ '/images/thumbnail/(+)_(+)x(+).jpg$' {
root /var;
}
location ~ '/images/(+)_(+)x(+).jpg$' {
root /var;
set $image_root '/var/images';
set $fileName $1;
set $width $2;
set $height $3;
set $origin $image_root/$fileName.jpg;
set $file $image_root/thumbnail/${fileName}_${width}x$height.jpg;
set $uriNew /images/thumbnail/${fileName}_${width}x$height.jpg;
if (-f $file) {
rewrite ^ $uriNew;
break;
}
if (!-f $origin) {
return 404;
}
rewrite_by_lua '
local width = tonumber(ngx.var.height);
local height = tonumber(ngx.var.height);
if width and height then
local command = "convert -sample "..ngx.var.width.."x"..ngx.var.height.." "..ngx.var.origin.." "..ngx.var.file;
os.execute(command);
ngx.req.set_uri(ngx.var.uriNew, true);
else
ngx.exit(ngx.HTTP_NOT_FOUND);
end
';
}

  3.nginx + lua-nginx-module + fastdfs 实现动态缩略图
  /usr/local/nginx
  |-conf
  |-lua
  |-fastdfs.lua
  |-restyfastdfs.lua
  |-storage.lua
  |-tracker.lua
  |-utils.lua
  |-nginx.conf
  --主要的配置
  nginx.conf

worker_processes1;
events {
worker_connections1024;
}
http {
include       mime.types;
default_typeapplication/octet-stream;
sendfile      on;
keepalive_timeout65;
lua_package_path "/usr/local/nginx/conf/lua/?.lua;;";
server {
listen       80;
server_namelocalhost;
location ~ '/images/thumbnail/(+)_(+)x(+).jpg$' {
root /var;
}
location ~ '/images/(+)_(+)x(+).jpg$' {
root /var;
set $image_root '/var/images';
set $fileName $1;
set $width $2;
set $height $3;
set $origin $image_root/$fileName.jpg;
set $file $image_root/thumbnail/${fileName}_${width}x$height.jpg;
set $uriNew /images/thumbnail/${fileName}_${width}x$height.jpg;
if (-f $file) {
rewrite ^ $uriNew;
break;
}
if (!-f $origin) {
return 404;
}
rewrite_by_lua '
local width = tonumber(ngx.var.height);
local height = tonumber(ngx.var.height);
if width and height then
local command = "convert -sample "..ngx.var.width.."x"..ngx.var.height.." "..ngx.var.origin.." "..ngx.var.file;
os.execute(command);
ngx.req.set_uri(ngx.var.uriNew, true);
else
ngx.exit(ngx.HTTP_NOT_FOUND);
end
';
}
location /group1/M00 {
alias /var/images;
#set $image_root "/usr/local/openresty/nginx/proxy_tmp/images";
set $image_root "/var/images";
if ($uri ~ "/(+)/(+)/(+)/(+)/(.*)") {
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 "conf/lua/fastdfs.lua";
}
#ngx_fastdfs_module;
}
error_page   500 502 503 504/50x.html;
location = /50x.html {
root   html;
}
}
}

  fastdfs.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, "(+)x(+)");
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, "(+)x(+)");
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.117.100", 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 = "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
  参考资料:
  https://github.com/openresty/lua-nginx-module
http://www.ttlsa.com/nginx/nginx-modules-ngx_lua/
http://www.iyunv.com/os/201504/387948.html
  http://houxiyang.com/archives/112/
  https://github.com/hpxl/nginx-lua-fastdfs-GraphicsMagick
  https://github.com/azurewang/lua-resty-fastdfs
页: [1]
查看完整版本: FASTDFS(六)nginx+lua+GraphicsMagick 图片自动缩放