使用Tengine+Lua+GM实现图片自动裁剪

使用的各个版本

在这里插入图片描述
1.在/usr/local/下新建gm文件夹 并把所需的安装包上传到服务器上并解压(文件夹名字随意)

mkdir -p /usr/local/em

2.安装Lua&安装依赖

yum install -y readline
yum install -y readline-devel

解压安装Lua(源码编译安装)

tar -zxvf lua-5.3.1 -C /usr/local/gm/
//解压完后 cd到解压的目录下
cd /usr/local/gm/lua-5.3.1/
//编译安装
make linux
make install

解压安装LuaJIT (源码编译安装)

tar -zxvf LuaJIT-2.0.5.tar.gz -C /usr/local/gm/
cd /usr/local/gm/LuaJIT-2.0.5/
make
make install
//检查lua版本lua -v		


出现这个就好了

3.安装Tengine
进入Tengine源码目录,使用configure配置安装路径以及需要安装的模块

1.使用configure配置安装路径

cd /usr/local/Tengine/

./configure --prefix=/usr/local/Tengine --dso-path=/usr/local/Tengine/modules --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_concat_module --with-http_lua_module --http-proxy-temp-path=/var/tmp/Tengine/proxy_temp --http-fastcgi-temp-path=/var/tmp/Tengine/fastcgi_temp --http-uwsgi-temp-path=/var/tmp/Tengine/uwsgi_temp --http-scgi-temp-path=/var/tmp/Tengine/cgi_temp --http-client-body-temp-path=/var/tmp/Tengine/client_body_temp --http-log-path=/var/log/Tengine/access.log --error-log-path=/var/log/Tengine/error.log
执行后报错-bash: ./configure: Permission denied(权限不够)
修改configure权限后再次执行:chmod 777 configure
如果报错:./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.
SSL模块需要OpenSSL库。
执行:yum -y install openssl openssl-devel
再次执行configure
make
make install

如果启动: /usr/local/Tengine/sbin/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
软连接

ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2

再次启动:nginx: [emerg] mkdir() “/var/tmp/Tengine/client_body_temp” failed (2: No such file or directory)

错误原因是没有这个目录,手动创建:mkdir -p /var/tmp/Tengine/client_body_temp

再次启动后浏览器访问
页面出现Welcome Tengine则成功
4.GraphicsMagick
安装依赖

yum install -y libjpeg libjpeg-devel libpng libpng-devel giflib giflib-devel freetype freetype-devel

进入GM源码目录,使用configure配置安装路径以及需要安装的模块

cd /usr/local/GraphicsMagick/

./configure --prefix=/usr/local/GraphicsMagick --enable-shared

安装GM(源码编译安装)

make
make install

查看GM版本信息:/usr/local/GraphicsMagick/bin/gm version

配置Lua脚本文件(ImageResizer.lua )
创建目录

mkdir -p /usr/local/Tengine/lua/

创建文件vi /usr/local/Tengine/lua/ImageResizer.lua

文件中添加以下信息

local command = "/usr/local/GraphicsMagick/bin/gm convert   -auto-orient -strip " .. 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;
os.execute(command);    
ngx.exec(ngx.var.request_uri);

授予权限:可执行
chmod 777 /usr/local/Tengine/lua/ImageResizer.lua

Tengine配置(nginx.conf )
编辑 vi /usr/local/Tengine/conf/nginx.conf
1.把user改为root用户
在这里插入图片描述
2.修改servier_name 给定一个名字
创建图片路径并在配置文件中添加
mkdir -p /data/itrip/uploadimg
在这里插入图片描述

4.location配置
在这里插入图片描述
expires 1h; # 缓存时间
add_header Cache-Control max-age=3600; # 缓存时间
access_log /var/log/Tengine/host_access.log;

5.图片裁剪过滤

location / {
            root   /data/itrip/uploadimg;
            expires 1h;
			add_header Cache-Controller max-age=3600;
			access_log /var/log/Tengine/host_access.log;
        }
		location ~* ^(.+.(jpg|jpeg|gif|png))_(\d+)x(\d+).(jpg|jpeg|gif|png)$ {
			root /data/itrip/uploadimg;
			if (!-f $request_filename) {
				lua_code_cache on;
				set $request_filepath /data/itrip/uploadimg$1;
				set $width $3;
				set $height $4;
				set $ext $5;
				content_by_lua_file /usr/local/Tengine/lua/ImageResizer.lua;
			}
		}

最后检查配置文件
在这里插入图片描述
重新启动Tengine
在这里插入图片描述
出现这个是因为index被删了
随便找几张图片放在/data/itrip/uploadimg/
然后再地址栏输入图片完整名称如
在这里插入图片描述
最后再地址栏输入 _200x50.jpg 以实现图片的剪裁
在这里插入图片描述

发布了6 篇原创文章 · 获赞 2 · 访问量 581

猜你喜欢

转载自blog.csdn.net/weixin_44554142/article/details/103408363
今日推荐