• 灵活自定义缩略图片尺寸大小方案分享(nginx,lua_nginx,GraphicsMagick)


    http://www.iteye.com/topic/1125126

    在开发电子商务网站时,同一个图片需要不同尺寸的缩略图片,一般有两种策略生成缩略图,一种在上传图片时,生成需要的缩略图,另一种是请求指定尺寸的图片时生存缩略图片,第一种方式有一定限制,就是需要提前知道所有尺寸的图片,如果前端ui设计改变了图片大小,需要重新生成。而第二种方式更加灵活,这里采用第二种方案(也是查看taobao网站图片名称,猜想出来的方案,并加以验证,后来证实淘宝也是采用这个方案,只是淘宝使用ImageMagick)。 
        这里主要借助lua_nginx module调用GraphicsMagick命令生存生存缩略图片,缩略图片的尺寸包含在请求图片名称中,例如:xxxxx.jpg.80x80.jpg返回的就是xxx.jpg的80x80尺寸的图片大小。nginx配置如下: 
        
        上传图片名称使用32位随机字符替换掉,图片存放目录为图片名称前六个字母,每两个一组,构造三层目录结构存放,这样可以均匀存放图片在不同目录。避免目录存放文件数量限制。 
        为了避免随意生成不同尺寸的缩略图,这里做了限制,在image_sizes中定义了需要的缩略图尺寸。 

    Java代码  收藏代码
    1. location /testImg/ {  
    2.     rewrite_by_lua '  
    3.         local image_root = "/home/tomcat/eisp-files";  
    4.         function file_notexists(name)  
    5.            local f=io.open(name,"r")  
    6.            if f~=nil then io.close(f) return false else return true end  
    7.         end  
    8.           
    9. local uri = ngx.re.sub(ngx.var.uri, "/testImg/([0-9a-zA-Z]+)/([0-9a-zA-Z]{2})([0-9a-zA-Z]{2})([0-9a-zA-Z]{2})([0-9a-zA-Z]+).([0-9a-zA-Z]+)", "/images/$1/$2/$3/$4/$2$3$4$5.$6", "o");  
    10.         local index = string.find(uri, "([0-9]+)x([0-9]+)");  
    11.         local originalUri = string.sub(uri, 0, index-2);  
    12.         local area = string.sub(uri, index);  
    13.         index = string.find(area, "([.])");  
    14.         area = string.sub(area, 0, index-1);  
    15.   
    16.         if file_notexists(image_root .. uri) then  
    17.            local image_sizes = {"80x80", "800x600", "40x40"};  
    18.            function table.contains(table, element)  
    19.               for _, value in pairs(table) do  
    20.                  if value == element then  
    21.                     return true  
    22.                  end  
    23.               end  
    24.               return false  
    25.            end  
    26.   
    27.            if table.contains(image_sizes, area) then  
    28.                local command = "gm convert " .. image_root ..  originalUri  .. " -thumbnail " .. area .. " -background gray -gravity center -extent " .. area .. " " .. image_root .. uri;  
    29.                os.execute(command);  
    30.                ngx.req.set_uri(uri, true);  
    31.            else  
    32.                ngx.exit(404);  
    33.            end;  
    34.         else  
    35.            ngx.req.set_uri(uri, true);  
    36.         end;  
    37.     ';  
    38. }  
    39.   
    40. location /images/ {  
    41.     alias /home/tomcat/eisp-files/images/;  
    42.     expires 7d;  
    43. }  




    最后使用配置:2012-12-15 

    Java代码  收藏代码
      1. location /images/ {  
      2.     set $image_root /home/nds/nds-files;  
      3.   
      4.     if ($uri ~* "/images/([0-9a-zA-Z]+)/([0-9a-zA-Z]{2})([0-9a-zA-Z]{2})([0-9a-zA-Z]{2})([0-9a-zA-Z]+).(.*)") {  
      5.        set $filePath "$image_root/$1/$2/$3/$4/$2$3$4$5.$6";  
      6.        set $reqPath  "/$1/$2/$3/$4/$2$3$4$5.$6";  
      7.     }  
      8.   
      9.     set $file "$image_root$reqPath";  
      10.   
      11.     if (-f $file) {  
      12.        rewrite "/images/(.+)" /innerImages$reqPath last;  
      13.     }  
      14.   
      15.     if (!-f $file) {  
      16.        rewrite_by_lua '  
      17.           local index = string.find(ngx.var.filePath, "([0-9]+)x([0-9]+)");  
      18.           local originalUri = string.sub(ngx.var.filePath, 0, index-2);  
      19.           local area = string.sub(ngx.var.filePath, index);  
      20.           index = string.find(area, "([.])");  
      21.           area = string.sub(area, 0, index-1);  
      22.   
      23.           local image_sizes = {"155x155", "400x400","104x104", "50x50", "40x40", "56x56", "172x172","800x600"};  
      24.           function table.contains(table, element)  
      25.              for _, value in pairs(table) do  
      26.                 if value == element then  
      27.                    return true  
      28.                 end  
      29.              end  
      30.              return false  
      31.           end  
      32.   
      33.           if table.contains(image_sizes, area) then  
      34.              local command = "gm convert " ..  originalUri  .. " -thumbnail " .. area .. " -background white -gravity center -extent " .. area .. " " .. ngx.var.file;  
      35.              os.execute(command);  
      36.              ngx.req.set_uri("/innerImages" .. ngx.var.reqPath, true);  
      37.           else  
      38.              ngx.exit(404);  
      39.           end;  
      40.        ';  
      41.     }  
      42. }  
      43.   
      44. location /innerImages/ {  
      45.     alias /home/nds/nds-files/;  
      46.     expires max;   
      47.     tcp_nodelay off;   
      48.     tcp_nopush on;  
      49. }  
  • 相关阅读:
    Protobuf for Python测试保存和读取文件
    windows下使用pip安装python的第三方lxml库
    WIN10 CMD 启动虚拟WIFI
    关于 DataGridTextColumn的IsReadOnly
    wpf converter converterparameter 绑定多参数
    wpf 客户端 添加qq客服咨询
    错误信息:内存位置访问无效。 (Exception from HRESULT: 0x800703E6)
    acrgis导出成tiff图片,全是黑色
    webStrom访问只一个很简单的html文件的时候显示local host无法访问。。
    pandas 将excel一列拆分成多列重新保存
  • 原文地址:https://www.cnblogs.com/ziyunlong/p/7920831.html
Copyright © 2020-2023  润新知