使用Tengine+Lua+GraphicsMagick实现图片自动裁剪缩放

来源:互联网 发布:好听的淘宝会员名大全 编辑:程序博客网 时间:2024/05/19 13:57

软件准备

  • Tengine

官网:http://tengine.taobao.org/

$ wget http://tengine.taobao.org/download/tengine-2.2.0.tar.gz
  • Lua

官网:http://www.lua.org/

$ wget http://www.lua.org/ftp/lua-5.3.4.tar.gz
  • LuaJIT

官网:http://luajit.org/

$ wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
  • GraphicsMagick

官网:http://www.graphicsmagick.org/

$ wget https://sourceforge.net/projects/graphicsmagick/files/graphicsmagick/1.3.26/GraphicsMagick-1.3.26.tar.gz

软件安装

1.安装 Lua 依赖工具(Lua安装所需)

$ sudo yum -y install readline readline-devel

如果不安装readline和readline-devel,在安装Lua时会报如下错误
致命错误:readline/readline.h:没有那个文件或目录

2. 安装 Lua

$ tar -zxvf lua-5.3.4.tar.gz$ cd lua-5.3.4$ make linux && make install

执行命令后如果可以看到lua的版本信息,使用print来打印内容

$ luaLua 5.3.4  Copyright (C) 1994-2017 Lua.org, PUC-Rio> print('Hello Lua')Hello Lua

3. 安装LuaJIT

$ tar -zxvf LuaJIT-2.0.5.tar.gz$ cd LuaJIT-2.0.5$ sudo make && make install

配置环境变量(笔者配置在用户变量,你也可以配置在profile中)

$ vim ~/.bashrcexport LUAJIT_LIB=/usr/local/libexport LUAJIT_INC=/usr/local/include/luajit-2.0$ source ~/.bashrc

4. 安装Tengine

$ tar -zxvf tengine-2.2.0.tar.gz$ cd tengine-2.2.0$ ./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

执行上面的命令后,打印的信息中笔者看到有Lua的库找不到(如果Lua库找不到在启动Tengine时会报错)

这里写图片描述

解决方法:配置LuaJIT的环境变量或执行如下命令

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

编译安装

$ sudo make && make install

测试

$ /usr/local/Tengine/sbin

这里写图片描述

执行命令测试,出现如下类似信息,表示安装成功

$ ./nginx -tnginx: the configuration file /usr/local/Tengine/conf/nginx.conf syntax is oknginx: configuration file /usr/local/Tengine/conf/nginx.conf test is successful

执行 ./nginx -t 后,笔者遇到两个问题,错误及解放方法如下

  • 错误1:

./nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory

解决方法:执行如下命令

ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
  • 错误2:

错误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

$ mkdir -p /var/tmp/Tengine/client_body_temp

进入 /usr/local/Tengine/sbin 目录下,输入如下命令启动

$ ./nginx

启动成功后,在浏览器输入ip,可以看到 tengine 的欢迎页面

这里写图片描述

5.安装 GraphicsMagick 依赖工具

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

6. 安装GraphicsMagick

$ tar -zxvf GraphicsMagick-1.3.26.tar.gz$ cd GraphicsMagick-1.3.26$ ./configure --prefix=/usr/local/GraphicsMagick --enable-shared$ make && make install

编译完成后进入GraphicsMagick的bin目录下

$ cd /usr/local/GraphicsMagick/bin

查看命令帮助:

$ ./gmGraphicsMagick 1.3.26 2017-07-04 Q8 http://www.GraphicsMagick.org/Copyright (C) 2002-2017 GraphicsMagick Group.Additional copyrights and licenses apply to this software.See http://www.GraphicsMagick.org/www/Copyright.html for details.Usage: ./gm command [options ...]Where commands include:     animate - animate a sequence of images      batch - issue multiple commands in interactive or batch mode  benchmark - benchmark one of the other commands    compare - compare two images  composite - composite images together    conjure - execute a Magick Scripting Language (MSL) XML script    convert - convert an image or sequence of images    display - display an image on a workstation running X       help - obtain usage message for named command   identify - describe an image or image sequence     import - capture an application or X server screen    mogrify - transform an image or sequence of images    montage - create a composite image (in a grid) from separate images       time - time one of the other commands    version - obtain release version

查看版本信息,输入如下命令后,如果看到如下类似的信息,表示安装成功

$ ./gm versionGraphicsMagick 1.3.26 2017-07-04 Q8 http://www.GraphicsMagick.org/Copyright (C) 2002-2017 GraphicsMagick Group.Additional copyrights and licenses apply to this software.See http://www.GraphicsMagick.org/www/Copyright.html for details.Feature Support:  Native Thread Safe       yes  Large Files (> 32 bit)   yes  Large Memory (> 32 bit)  yes  BZIP                     yes  DPS                      no  FlashPix                 no  FreeType                 yes  Ghostscript (Library)    no  JBIG                     no  JPEG-2000                no  JPEG                     yes  Little CMS               no  Loadable Modules         no  OpenMP                   yes (201107)  PNG                      yes  TIFF                     no  TRIO                     no  UMEM                     no  WebP                     no  WMF                      no  X11                      yes  XML                      yes  ZLIB                     yesHost type: x86_64-unknown-linux-gnuConfigured using the command:  ./configure  '--prefix=/usr/local/GraphicsMagick' '--enable-shared'Final Build Parameters:  CC       = gcc -std=gnu99  CFLAGS   = -fopenmp -g -O2 -Wall -pthread  CPPFLAGS = -I/usr/include/freetype2 -I/usr/include/libxml2  CXX      = g++  CXXFLAGS = -pthread  LDFLAGS  =   LIBS     = -lfreetype -ljpeg -lpng15 -lX11 -llzma -lbz2 -lxml2 -lz -lm -lgomp -lpthread

使用 GraphicsMagick 来裁剪一张图

$ rz   # 上传图片test.jpg到~/photo路径下

这里写图片描述

执行命令裁剪图片

$ sudo ./gm convert ~/photo/test.jpg -resize 200x200 ~/photo/small.jpg

下载裁剪完成后的图片(图片变小但不会失真)

$ sz ~/photo/small.jpg

这里写图片描述

2.测试

1.nginx + lua 测试

配置nginx.conf文件

$ cd /usr/local/Tengine/$ cd conf

这里写图片描述

为了保险起见,我们先备份一下nginx.conf文件

$ cp nginx.conf nginx.conf.bak

编辑nginx.conf文件

# vim nginx.conf

找到 #user nobody; 在它的下一行添加如下代码

user root

在server块(大概在)中添加如下代码

location /lua/ {    default_type 'text/html';    content_by_lua 'ngx.say("<h1>hello, lua</h1>")';}

保存退出,后执行如下命令重新加载ngnix

# ./nginx -s reload

浏览器访问:IP/lua/

这里写图片描述

2.裁剪图

(1).配置nginx

# cd /usr/local/Tengine/conf# vim nginx.conf

找到 #user nobody; 在它的下一行添加如下代码

user root

server 中新增location

server {        listen       80;        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            #root   html;            root   /home/hadoop/images;            index  index.html index.htm;        }        location /lua/ {            default_type 'text/html';            content_by_lua 'ngx.say("<h1>hello, lua</h1>")';        } # url格式:如:xxxx.gif_数字x数字.gif        location ~* ^(.+\.(jpg|jpeg|gif|png))_(\d+)x(\d+)\.(jpg|jpeg|gif|png)$ {                  root /home/hadoop/images;       # 根目录(必须设置)                if (!-f $request_filename) {       # 如果文件不存在才裁剪            # 在编写外部 Lua 脚本时,设置为 off Nginx 不会缓存 Lua,方便调试                        lua_code_cache off;                        set $request_filepath /home/hadoop/images/$1; # 原始图片路径                        set $width $3;     # 设置裁剪/缩放的宽度                        set $height $4;     # 设置裁剪/缩放的高度                        set $ext $5;     # 图片文件格式后缀                        content_by_lua_file /usr/local/Tengine/lua/ImageResizer.lua;      # 加载外部lua文件         }}

(2).编写脚本

进入如下目录

# cd /usr/local/Tengine/

新建文件夹lua,lua下新建文件

# mkdir lua# touch ImageResizer.lua# vim ImageResizer.lua

ImageResizer.lua 文件中编写代码如下

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;os.execute(command);            # 执行裁剪命令ngx.exec(ngx.var.request_uri);  # 输出裁剪后的图片

(3).执行并查看结果

输入如下命令重新加载nginx,使用修改的配置立即生效

# ./nginx -s reload

浏览器访问:http://IP/test.jpg_300x300.jpg

这里写图片描述

阅读全文
0 0
原创粉丝点击