nginx+lua+image实现上传图片裁剪和加水印-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

nginx+lua+image实现上传图片裁剪和加水印

简介:

https://github.com/openresty/lua-nginx-module

https://github.com/simpl/ngx_devel_kit

Lua JIT

http://luajit.org/download.html

configure \
...
--add-module=/path/lua-nginx-module \
--add-module=/path/ngx_devel_kit 

make && make install

ImageMagick

http://www.imagemagick.org/

Modify

nginx-server.conf

  1. convert_bin imagemagick_install_path/bin/convert

  2. rewrite_by_lua_file nginx-imagemagick.lua save path

if want to allow more image size, please modify the image_sizes variable in nginx-imagemagick.lua


server

{


    listen       80;

    server_name upload.qq.com;

    index index.html index.htm index.php;

    root  /h1/upload.qq.com;

   location ~ ^(.+\.php)(.*)$ {  

        fastcgi_pass 127.0.0.1:9000;

        fastcgi_index index.php; 

        fastcgi_split_path_info ^(.+\.php)(.*)$;  

        fastcgi_param SCRIPT_FILENAME /path/to/php$fastcgi_script_name;

        fastcgi_param PATH_INFO $fastcgi_path_info; 

        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;

         include fcgi.conf;  

        }

rewrite ^/attachment/image/(.*).jpg$  /attachment/image/$1.jpg!0x0 last;#处理原始图显示为0x0图

       # rewrite ^/images/(.*).jpg  ^/images/$1.jpg!0x0 last;  

         location /attachment/image/ {

                set $image_root "/htdoc/upload.qq.com";

                set $file "$image_root$uri";

                set $convert_bin "/usr/local/bin/convert";

               if (!-f $file)

                {

                        rewrite_by_lua_file /usr/local/webserver/nginx/conf/nginx-imagemagick.lua;

                }

                expires max;

        }



        location ~ .*\.(js|css)?$

        {

                expires max;

        }


}

下面是lua代码 nginx-imagemagick.lua:


-- http://domain.com/111/photo/201411/05/5459e306820af926411357_320x320.jpg


-- config

local image_sizes = { "640x640", "320x320", "124x124", "140x140", "64x64", "60x60", "32x32", "0x0" }



-- parse uri

function parseUri(uri)

    local _, _, name, ext, size = string.find(uri, "(.+)(%..+)!(%d+x%d+)")

 --ngx.header.content_type = "text/plain";

    --ngx.say(name,size);

    if name and size and ext then

        return ngx.var.image_root .. name .. ext, size

    else

        return "",""

    end

end


function fileExists(name)

    local f = io.open(name, "r")

    if f ~= nil then

        io.close(f)

        return true

    else

        return false

    end

end

 --ngx.header.content_type = "text/plain";

    --ngx.say(name);

function sizeExists(size)

    for _, value in pairs(image_sizes) do

        if value == size then

            return true

        end

    end


    return false

end

 --ngx.header.content_type = "text/plain";

    --ngx.say(size);

function resize()

    local ori_filename, size = parseUri(ngx.var.uri)

--ngx.header.content_type = "text/plain";

    --ngx.say(ori_filename,size);

    if fileExists(ori_filename) == false or sizeExists(size) == false then

        ngx.exit(404)

    end


     local command = '';

    if size == '0x0' then

        command = table.concat({

            ngx.var.convert_bin,

            ori_filename,

            "/usr/local/webserver/nginx/conf/logo.png",

            "-gravity southeast -geometry +5+10 -composite",

            ngx.var.file,

        }, " ")

    else

        command = table.concat({

            ngx.var.convert_bin,

            ori_filename,

            "/usr/local/webserver/nginx/conf/logo.png",

            "-gravity southeast -geometry +5+10 -composite -resize",

            size,

            ngx.var.file,

        }, " ")

    end

--ngx.header.content_type = "text/plain";

    --ngx.say(command);

    os.execute(command)

end

 --ngx.header.content_type = "text/plain";

   --ngx.say(command);

resize()



再下面是nginx的mime配置

[root@localhost conf]# cat mime.types


types {

    text/html                             html htm shtml;

    text/css                              css;

    text/xml                              xml plist;

    image/gif                             gif gif!320x320 gif!64x64 gif!140x140;

    image/jpeg                            jpeg jpg jpg!320x320 jpg!64x64 jpg!140x140 jpg!200x200 jpg!0x0;



结果地址  http://upload.qq.com/images/13.jpg(原始图) 

http://upload.qq.com/images/13.jpg!0x0 (缩略图)

 http://upload.qq.com/images/13.jpg!64x64(缩略图)

 http://upload.qq.com/images/13.jpg!320x320 (缩略图)

尺寸自己定义

/usr/local/webserver/nginx/conf/logo.png 这个是水印图 


手动测试图片处理在系统里执行 

convert src.jpg logo.gif -gravity southeast -geometry +5+10 -composite dest.jpg



本文转自 liang3391 51CTO博客,原文链接:http://blog.51cto.com/liang3391/1608878

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章
最新文章
相关文章