在此先把前面自己做的那个功能在此分享下,是一个模仿淘宝的,希望大神们有什么想法可以不吝赐教:
自己是通过前面的参考配置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
                                免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
                                如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
                            
                        暂无“Fastdfs与nginx进行压缩图片比率”评论...
                                    更新动态
2025年10月31日
                                2025年10月31日
                    - 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]
 
                        