网站首页lua/js

nginx_lua配置图片服务器

发布时间:2017-04-29 09:01:08编辑:slayer.hover阅读(6381)

           

    使用环境:  centos, 安装好nginx_lua 和GraphicsMagick(shell命令行下 gm)


    实现方式:

    1. nginx配置图片服务器,正则匹配图片的url, 在参数中获取对应的参数

    server
    {
        listen        80;
        server_name   img.site.com;
        index         index.php;
        set  $root_path  "/home/webroot/web/img";
        root $root_path;
        
        # 参数中附带了宽度和高度,不按比例缩放
        location ~* (.*?)\@(\d+)w_(\d+)h\.(jpg|jpeg|gif|png)$ {
            set $root "/home/webroot/web/thumbnail";
            root $root;    
            set $thumbnail_root $root;
            set $img_original_root $root_path;         
            set $file $thumbnail_root$uri;             #如果缩略图文件存在,直接返回
    
            if (!-f $file) {    # 如果文件不存在时才需要裁剪
                    set $request_filepath $img_original_root$1.$4;    # 设置原始图片路径
                    set $scale         0;    # 不按比例缩放
                    set $img_width    $2;    # 设置裁剪/缩放的宽度
                    set $img_height $3;    # 设置裁剪/缩放的高度
                    set $img_ext $4;    # 图片文件格式后缀
                    content_by_lua_file   /usr/local/nginx/conf/lua/img.lua;    # 加载图片处理 Lua 文件
            }
            expires 3d;
        }
    
        # 参数中只有宽度,按比例缩放图片    
        location ~* (.*?)\@(\d+)\.(jpg|jpeg|gif|png)$ {
            set $root "/home/webroot/web/thumbnail";
            root $root;    
            set $thumbnail_root $root;
            set $img_original_root $root_path;         
            set $file $thumbnail_root$uri;             #如果缩略图文件存在,直接返回
            if (!-f $file) {   # 如果文件不存在,裁剪生成图片
                    set $request_filepath $img_original_root$1.$3;
                    set $scale         1;    # 按比例缩放
                    set $img_width     $2;    # 设置裁剪/缩放的宽度
                    set $img_ext     $3;    # 图片文件格式后缀
                    content_by_lua_file   /usr/local/nginx/conf/lua/img.lua;    # 加载图片处理 Lua 文件
            }
                
            expires 3d;
        }
        location ~ .*\.(js|css)$ {
            expires 1d;
            access_log off;
        }
        access_log  /home/logs/img.log access;
    }


    2.调用img.lua脚本,gm命令生成对应大小的图片.

    local function is_dir(sPath)
        if type(sPath) ~= "string" then return false end
        local response = os.execute("cd " .. sPath)
        if response == 0 then
            return true
        end
        return false
    end
    function file_exists(name)
        local f = io.open(name, "r")
        if f ~= nil then io.close(f) return true else return false end
    end
    function getFileDir(filename)
        return string.match(filename, "(.+)/[^/]*%.%w+$") --*nix system
    end
    function strippath(filename)
        return string.match(filename, ".+/([^/]*%.%w+)$") -- *nix system
    end
    function stripextension(filename)
        local idx = filename:match(".+()%.%w+$")
        if (idx) then
            return filename:sub(1, idx - 1)
        else
            return filename
        end
    end
    function getExtension(filename)
        return filename:match(".+%.(%w+)$")
    end
    local gm_path = 'gm'
    if not is_dir(getFileDir(ngx.var.file)) then
        os.execute("mkdir -p " .. getFileDir(ngx.var.file))
    end
    if (file_exists(ngx.var.request_filepath)) then
        local    sc=tonumber(ngx.var.scale)
        if (sc == 0) then
            local cmd = gm_path .. ' convert  ' .. ngx.var.request_filepath    
            local  w=tonumber(ngx.var.img_width)
            local  h=tonumber(ngx.var.img_height)
            if w>1200 then
                w=1200
            end
            if h>1200 then
                h=1200
            end
            w=tostring(w)
            h=tostring(h)
            cmd = cmd  ..' -resize  "'.. w .. "x" .. h .. '>" '       
            cmd = cmd .. "  -background white -gravity center -extent "..w.."x"..h.." "    
            cmd = cmd .. ngx.var.file;
            ngx.log(ngx.ERR, cmd);
            os.execute(cmd);
            ngx.exec(ngx.var.uri);        
        else
            local cmd = gm_path .. ' convert  ' .. ngx.var.request_filepath
            local  w=tonumber(ngx.var.img_width)
            if w>1200 then
            w=1200
            end
            w=tostring(w)
            cmd = cmd  ..' -resize  "'.. w .. '" '                
            cmd = cmd .. ngx.var.file;
            ngx.log(ngx.ERR, cmd);
            os.execute(cmd);
            ngx.exec(ngx.var.uri);
        end
    else
        ngx.exit(ngx.HTTP_NOT_FOUND);
    end


    在浏览器里打开图片时,

    http://img.site.com/uploads/abc@300.jpg,会生成http://img.site.com/uploads/abc.jpg的宽度为300的按比例缩放的图片。

    http://img.site.com/uploads/abc@300w_200h.jpg,则会生成http://img.site.com/uploads/abc.jpg的宽度为300,高度为200的不按比例缩放的图片。


    附带上软件的安装:

    #gm图片处理软件安装
    wget  http://soft.ileiming.com/gm.tar.gz
    tar -zxvf gm.tar.gz
    cd gm
    tar -zxvf GraphicsMagick-1.3.12.tar.gz
    cd GraphicsMagick-1.3.12
    ./configure -prefix=/usr/local/gm
    make && make install
    ln -s /usr/local/gm/bin/gm /usr/bin
    
    
    #nginx_lua&&luajit安装
    tar -zxvf LuaJIT-2.0.0-beta10.tar.gz
    cd LuaJIT-2.0.0-beta10
    make && make install PREFIX=/usr/local/lj2
    
    ln -sf luajit-2.0.0-beta10 /usr/local/lj2/bin/luajit
    unzip simpl-ngx_devel_kit-v0.2.19-0-g8dd0df5.zip
    tar -zxvf  chaoslawful-lua-nginx-module-v0.9.2-61-ge4e085f.tar.gz
    unzip agentzh-echo-nginx-module-v0.49-1-g84feae9.zip
    tar -zxvf ngx_cache_purge-1.6.tar.gz




评论