Tengine+Lua+GraphicsMagick动态裁剪图片
- Tengine:https://github.com/alibaba/tengine
- Lua:http://www.lua.org/ftp/ ,这里使用 lua-5.3.1.tar.gz
- LuaJIT:http://luajit.org/download.html ,这里使用 LuaJIT-2.0.4.tar.gz
- GraphicdMagick:https://sourceforge.net/projects/graphicsmagick/files/graphicsmagick/ ,这里使用 GraphicsMagick-1.3.18.tar.gz
安装Lua
先安装依赖:
1 | yum install readline readline-devel -y |
解压文件
1 | tar -zxvf lua-5.3.1.tar.gz |
进入目录,执行:
1 | cd lua-5.3.1 |
验证安装成功:
1 | lua -v |
安装luaJIT
解压文件
1 | tar -zxvf LuaJIT-2.0.4.tar.gz |
进入目录,执行:
1 | cd LuaJIT-2.0.4 |
安装tengine
安装依赖:
1 | yum -y install pcre-devel openssl openssl-devel |
解压文件:
1 | unzip tengine-master.zip |
进入目录,执行:
1 | cd tengine-master |
再执行:
1 | make && make install |
验证安装成功:
1 | $ /usr/local/Tengine/sbin/nginx |
提示找不到文件,配置一个软连接:
1 | ln -s /usr/local/lib/libluajit-5.1.so.2 |
验证安装成功:
1 | $ /usr/local/Tengine/sbin/nginx |
手动创建该目录:
1 | mkdir -p /var/tmp/Tengine/client_body_temp |
然后,访问 http://192.168.56.100/ ,可以看到:
1 | Welcome to tengine! |
安装GraphicsMagick
安装依赖:
1 | yum install libjpeg libjpeg-devel libpng libpng-devel giflib giflib-devel freetype freetype-devel -y |
解压文件
1 | tar -zxvf GraphicsMagick-1.3.18.tar.gz |
进入目录,执行:
1 | cd GraphicsMagick-1.3.18 |
再编译安装:
1 | make && make install |
验证安装成功:
1 | /usr/local/GraphicsMagick/bin/gm version |
配置
创建 ImageResizer.lua 文件:
1 | mkdir /usr/local/Tengine/lua/ |
ImageResizer.lua:
1 | local command = "/usr/local/GraphicsMagick/bin/gm convert " .. ngx.var.request_filepath .. " -resize " .. ngx.var.width .. "x" .. ngx.var.height .. " +profile "*" " .. ngx.var.request_filepath .. "_" .. ngx.var.width .. "x" .. ngx.var.height .. "." .. ngx.var.ext; |
修改 /usr/local/Tengine/conf/nginx.conf:
1 | user root; # 裁剪图片需要root权限 |
重新加载配置文件:
1 | /usr/local/Tengine/sbin/nginx -s reload |
创建图片上传目录:
1 | mkdir -p /data/image/upload |
配置hosts:
1 | 192.168.56.100 img.javachen.com |
在 /data/image/upload 下存放一个图片 1.png,然后访问:http://192.168.56.100/1.png,再访问缩放图片:http://192.168.56.100/1.png_245x245.png
查看 /data/image/upload 目录是否有生成新的文件:
1 | $ ll /data/image/upload/ |