设为首页 收藏本站
查看: 1235|回复: 0

[经验分享] FASTDFS(六)nginx+lua+GraphicsMagick 图片自动缩放

[复制链接]

尚未签到

发表于 2016-12-26 07:48:30 | 显示全部楼层 |阅读模式
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/([0-9a-z]+).jpg$' {
root /var;
}
location ~ '/images/([0-9a-z]+)_([0-9]+)x([0-9]+).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     1  0 00:05 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   12100 10065  0 11:35 ?        00:00:00 nginx: worker process      
root     12108  9959  0 11:37 pts/1    00:00:00 grep nginx
访问
http://192.168.117.101/images/desktop_200x200.jpg
返回缩略图,/var/images多了对应的缩略图
  --配置nginx实现简单自动生成缩略图
  --进阶,将缩略图文件和原图分开存储

location ~ '/images/thumbnail/([0-9a-z]+)_([0-9]+)x([0-9]+).jpg$' {
root /var;
}
location ~ '/images/([0-9a-z]+)_([0-9]+)x([0-9]+).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_processes  1;
events {
worker_connections  1024;
}
http {
include       mime.types;
default_type  application/octet-stream;
sendfile        on;
keepalive_timeout  65;
lua_package_path "/usr/local/nginx/conf/lua/?.lua;;";
server {
listen       80;
server_name  localhost;
location ~ '/images/thumbnail/([0-9a-z]+)_([0-9]+)x([0-9]+).jpg$' {
root /var;
}
location ~ '/images/([0-9a-z]+)_([0-9]+)x([0-9]+).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 ~ "/([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 "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, "([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.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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-319343-1-1.html 上篇帖子: Nginx+Tomcat+FastDFS文件下载模块讲解 下篇帖子: FastDFS4.06与nginx环境搭建(一)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表