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

软件准备

  • 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来打印内容

$ lua
Lua 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 ~/.bashrc
export LUAJIT_LIB=/usr/local/lib
export 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 -t
nginx: the configuration file /usr/local/Tengine/conf/nginx.conf syntax is ok
nginx: 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

查看命令帮助:

$ ./gm
GraphicsMagick 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 version
GraphicsMagick 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                     yes

Host type: x86_64-unknown-linux-gnu

Configured 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

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值