使用Tengine+Lua+GM實現圖片自動裁剪

使用Tengine+Lua+GM實現圖片自動裁剪

1.把需要的tar包放進虛擬機

(1)GraphicsMagick-1.3.18.tar.gz
(2)lua-5.3.1.tar.gz
(3)LuaJIT-2.0.5.tar.gz
2.解壓並且安裝依賴
(1)安裝依賴

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

(2)安裝Lua

make linux
make install

(3)安裝LuaJIT

make
make install

可以在LuajIT目錄下檢查lua版本 : lua -v

3.安裝Tengine
(1)使用configure配置安裝路徑
首先權限:chmod 777 configure

使用configure配置安裝路徑(執行以下命令)

./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

(2)啓動: 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

再次啓動後瀏覽器訪問:IP地址

4.GraphicsMagick安裝依賴

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

進入GM目錄,使用configure配置安裝路徑以及需要安裝的模塊

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

5.安裝GM
先解壓 然後去GM的目錄下make&make install

6.配置
(1)找到Lua腳本文件:/usr/local/Tengine/lua/ImageResizer.lua
(2)創建目錄:mkdir -p /usr/local/Tengine/lua/
(3)創建文件:vi /usr/local/Tengine/lua/ImageResizer.lua
(4)文件中添加一下信息

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;  
            }
        }

(5)授予權限,可執行:chmod 777 /usr/local/Tengine/lua/ImageResizer.lua
(6)編輯 nginx.conf
1.把user改爲root用戶(把前面的#號去掉):
user root;

2.修改servier_name 給定一個名字

server_name  itrip.images.project;
root /data/itrip/uploadimg;
3.創建圖片路徑並在配置文件中添加

mkdir -p /data/itrip/uploadimg

4.location配置(把原來的那兩行刪掉)

root /data/itrip/uploadimg;
			expires 1h;
			add_header Cache-Control max-age=3600;
			access_log /var/log/Tengine/host_access.log;

5.圖片裁剪過濾:(在location配置下面)

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;  
        }
    }

7.重新啓動Tengine

 sbin/nginx -s reload
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章