jinquan26 发表于 2017-6-5 11:54:23

Fastdfs与nginx进行压缩图片比率随记

  前阵子,工作搞了一下Fastdfs与nginx进行压缩图片比率存储在server中,今天应用下工作时间记录下。本打算周六,老是由于一些事情给耽搁,眼下正在搞一个XMPP消息通讯协议,眼下还是一头雾水。没有实现什么功能,在此勉励,加油好好搞搞,搞好了记录下一些登录、退出、发送、接收的一些接口给client使用。
  在此先把前面自己做的那个功能在此分享下。是一个模仿淘宝的,希望大神们有什么想法能够指教:
  參考:http://www.v2ex.com/t/113845
  http://blog.sina.com.cn/openresty

  http://write.blog.csdn.net/postedit

  https://github.com/azurewang/lua-resty-fastdfs/blob/master/lib/resty/fastdfs/storage.lua

  https://github.com/azurewang/lua-resty-fastdfs/blob/master/lib/resty/fastdfs/storage.lua

  http://www.111cn.net/sys/CentOS/55070.htm

  http://wiki.nginx.org/HttpLuaModule

  自己是通过前面的參考配置lua与nginx的结合使用。利用脚本语言lua的强大特性和nginx的特性来实现这个功能。在nginx.conf的配置文件里增加例如以下代码:
  server {
      listen      22222;
      server_namelocalhost;
      #server_namesomenamealiasanother.alias;
      location /images/{
      alias /root/images;
       set $image_root /root;
      set $file $image_root$uri;
      content_by_lua '
               ngx.header.content_type = "text/plain";
               ngx.say(ngx.var.file);
         ';
      }
      location /lua{
         set $test "hello, world.";
            content_by_lua '
               ngx.header.content_type = "text/plain";
               ngx.say(ngx.var.test);
         ';
      }
      location /group1/M00 {
            alias /usr/local/servers/data/fdfs/data;
            set $image_root "/usr/local/servers/data/fdfs/data";
         #alias /root/images;
          # set $image_root "/root/images";
            if ( $uri ~ "/(+)/(+)/(+)/(+)/(.*)" ) {
               set $image_dir "$image_root/$3/$4";
               set $image_name "$5";
               set $file "$image_dir/$image_name";
             }
      #content_by_lua '
         #      ngx.header.content_type = "text/plain";
          #       ngx.say(ngx.var.image_dir);
         #      ngx.say(ngx.var.image_name);
            #   ngx.say(ngx.var.file);
         # ';
             if ( !-f $file ) {
                # 关闭lua代码缓存。方便调试lua脚本
                #lua_code_cache off;
                content_by_lua_file "/usr/local/servers/lua/convert.lua";
             }
               ngx_fastdfs_module;
      }
      # location ~ /group/M00{
         #root/usr/local/servers/data/fdfs/data; #/fdfs/storage/data;
          # ngx_fastdfs_module;
         #    }


       }

  这里面利用了nginx的正則表達式,正則表達式是相当强悍的,能够得到你须要訪问的uri的值。
  然后再convert.lua中写入例如以下代码:
  local area = nil
local originalUri = ngx.var.uri;
local originalFile = ngx.var.file;
local index = string.find(ngx.var.uri, "(+)x(+)");
if index then
    originalUri = string.sub(ngx.var.uri, 0, index-2);
    area = string.sub(ngx.var.uri, index);
    index1 = string.find(area, "([.])");
    area1 = string.sub(area, 0, index1-1);


    local index2 = string.find(originalFile, "(+)x(+)");
    originalFile1 = string.sub(originalFile, 0, index2-2)
end
local image_sizes = {"80x80", "800x600", "40x40", "60x60"};
function table.contains(table, element)
    for _, value in pairs(table) do
      if value == element then
            return true
      end
    end
    return false
end


if table.contains(image_sizes, area1) then
    local command = "/usr/bin/gm convert " .. originalFile1.. " -thumbnail " .. area1 .. " -background gray -gravity center -extent " .. area1 .. " " .. ngx.var.file;
    os.execute(command);
end;


~几乎相同这样就能够实现功能了。通过訪问能够实现比率压缩,由于在上班时间临时就这么写下了,眼下的一个工作还有非常多功能须要实现,如有看不懂的能够留言。大神们能够不吝赐教,谢谢!
页: [1]
查看完整版本: Fastdfs与nginx进行压缩图片比率随记