使用Nginx和LuaJIT(OpenResty)动态调整图像大小

在相当长一段时间内,受文章动态调整图像大小的启发,设置了使用ngx_http_image_filter_module调整图像大小,并且一切正常进行。但是,当经理需要获取具有确切大小的图像以上传到某些服务时,存在一个问题,因为 这些是他们的技术要求。例如,如果我们有一个原始图像,尺寸为1200x1200,而在调整大小时,我们写了类似?Resize = 600x400的图像,那么我们将在最小边缘400x400处按比例缩小图像也不可能获得具有更高分辨率(高画质)的图像。那些。?resize = 1500x1500将返回所有相同的图像1200x1200


一篇OpenResty文章抢救了我们:我们将NGINX转变为成熟的应用服务器,以了解Nginx如何与Lua一起使用以及Lua isage / lua-imagick本身的库-Lua pure-c绑定到ImageMagick。为什么选择这样的解决方案,而不是选择python中的东西-因为它既快速又方便。您甚至不需要创建任何文件,一切都在Nginx配置中正确完成(可选)。


那我们需要什么


示例将基于Debian给出。


安装nginx和nginx-extras


apt-get update
apt-get install nginx-extras

安装LuaJIT


apt-get -y install lua5.1 luajit-5.1 libluajit-5.1-dev

安装imagemagick


apt-get -y install imagemagick

和它的magickwand,在我的情况下是版本6


apt-cache search libmagickwand
apt-get -y install libmagickwand-6.q16-3 libmagickwand-6.q16-dev

Lua-imagick构建


我们克隆存储库(或者,或者提取并解压缩)


cd ~
git clone https://github.com/isage/lua-imagick.git
cd lua-imagick
mkdir build
cd build
cmake ..
make
make install

如果一切顺利,则可以配置Nginx。


backend , , , . Nginx, () . .


nginx backend config
# Backend image server
server {
    listen       8082;
    listen [::]:8082;
    set $files_root /var/www/example.lh/frontend/web;
    root $files_root;
    access_log off;
    expires 1d;

    location /files {
        #   
        set $w 700;
        set $h 700;
        set $q 89;

        #1-89 allowed
        if ($arg_q ~ "^([1-9]|[1-8][0-9])$") {
            set $q $arg_q;
        }

        if ($arg_resize ~ "([\d\-]+)x([\d\+\!\^]+)") {  
            set $w $1;
            set $h $2;
            rewrite  ^(.*)$   /resize/$w/$h/$q$uri     last;
        }

        rewrite  ^(.*)$   /resize/$w/$h/$q$uri     last;
    }

    location ~* ^/resize/([\d]+)/([\d\+\!\^]+)/([\d]+)/files/(.+)$ {
        default_type 'text/plain';

        set $w $1;
        set $h $2;
        set $q $3;
        set $fname $4;

        #     Lua    
        # content_by_lua_file /var/www/some.lua;
        # lua_code_cache off; #dev
        content_by_lua '
        local magick = require "imagick"
        local img = magick.open(ngx.var.files_root .. "/files/" .. ngx.var.fname)
        if not img then ngx.exit(ngx.HTTP_NOT_FOUND) end
        img:set_gravity(magick.gravity["CenterGravity"])

        if string.match(ngx.var.h, "%d+%+") then
            local h = string.gsub(ngx.var.h, "(%+)", "")
            resize = ngx.var.w .. "x" .. h
            --  png   
            img:set_bg_color(img:has_alphachannel() and "none" or img:get_bg_color())
            img:smart_resize(resize)
            img:extent(ngx.var.w, h)
        else
                img:smart_resize(ngx.var.w .. "x" .. ngx.var.h)
        end

        if ngx.var.arg_q then img:set_quality(ngx.var.q) end

        ngx.say(img:blob())
        ';
    }
}

# Upstream
upstream imageserver {
    server localhost:8082;
}

server {
    listen 80;
    server_name examaple.lh;

    #   jpg  png   imageserver
    location ~* ^/files/.+\.(jpg|png) {
        proxy_buffers 8 2m;
        proxy_buffer_size 10m;
        proxy_busy_buffers_size 10m;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_pass     http://imageserver;  # Backend image server
    }
}

, ( ) img:extent() resize + .


:


  • WxH (Keep aspect-ratio, use higher dimension)
  • WxH^ (Keep aspect-ratio, use lower dimension (crop))
  • WxH! (Ignore aspect-ratio)
  • WxH+ (Keep aspect-ratio, add side borders)


uri
?resize=400x200200x200
?resize=400x200^400x400
?resize=400x200!400x200 ( )
?resize=400x200+400x200 ()



有了这种方法的强大功能和简便性,您就可以使用相当复杂的逻辑来实现事物,例如添加watermark'i或通过有限的访问权限来实现授权。为了找出用于处理图像的API的功能,可以参考isage / lua-imagick库的文档


All Articles