圆月山庄资源网 Design By www.vgjia.com

在此先把前面自己做的那个功能在此分享下,是一个模仿淘宝的,希望大神们有什么想法可以不吝赐教:

自己是通过前面的参考配置lua与nginx的结合使用,利用脚本语言lua的强大特性和nginx的特性来实现这个功能,在nginx.conf的配置文件中加入如下代码:

server {

  listen  22222;

  server_name localhost;

  # server_name somename alias another.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 ~ "/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/(.*)" ) {

     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[1-3]/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, "([0-9]+)x([0-9]+)");

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, "([0-9]+)x([0-9]+)");

 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;

~差不多这样就可以实现功能了,通过访问可以实现比率压缩,因为在上班时间暂时就这么写下了,目前的一个工作还有很多功能需要实现,如有看不懂的可以留言,大神们可以指教,谢谢!

      参考: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://www.jb51.net/sys/CentOS/55070.htm

http://wiki.nginx.org/HttpLuaModule

标签:
Fastdfs,nginx,压缩图片

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com