码迷,mamicode.com
首页 > 其他好文 > 详细

nginx+lua_nginx+GraphicsMagick生成实时缩略图

时间:2015-08-16 18:14:54      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:

暂做笔记,带后续验证通过后,再补充 1、2、3 步。

一、安装 lua

二、安装 GraphicsMagick

三、安装nginx

四、配置 nginx

  nginx.conf

http
   {
        lua_package_path /usr/local/openresty/nginx/lua/?.lua;;;
        
        server {
                listen       80;
                server_name  img.rhythmk.org;
                root  /home/wwwroot/static/image;
                
                #对类似_100x100.gif/jpg/png/jpeg进行缩略图处理
                location ~* _([0-9]+)x([0-9]+)\.(gif|jpg|png|jpeg)$ {                 #匹配文件名规则
                        root  /home/wwwroot/static/image;                             #站点根目录
                        set $image_root /home/wwwroot/static/image;                   #图片目录
                        set $thumbnail_root /home/wwwroot/static/thumbnail;           #缩略图存放目录
                        #如果缩略图文件存在,直接返回
                        set $file $thumbnail_root$uri;
                        if (-f $file) {
                                rewrite ^/(.*)$ /thumbnail/$1 last;
                        }
                        #如果缩略图文件不存在,则应用缩略图模块处理
                        if (!-f $file) {
                                rewrite_by_lua_file lua/thumbnail.lua;
                        }
                }
         }
 
     #include conf/*.conf;
}

lua/thumbnail.lua

    local index = string.find(ngx.var.uri, "([0-9]+)x([0-9]+)");  
    local originalUri = string.sub(ngx.var.uri, 0, index-2);  
    local area = string.sub(ngx.var.uri, index);  
    index = string.find(area, "([.])");  
    area = string.sub(area, 0, index-1);  
  
    local image_sizes = {"80x80", "800x600", "40x40"};  
    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, area) then  
        local command = "gm convert " .. ngx.var.image_root .. originalUri .. " -thumbnail " .. area .. " -background gray -gravity center -extent " .. area .. " " .. ngx.var.file;  
        os.execute(command);  
        ngx.req.set_uri(ngx.var.uri, true);  
    else  
        ngx.exit(404);  
    end;  

 

nginx+lua_nginx+GraphicsMagick生成实时缩略图

标签:

原文地址:http://www.cnblogs.com/rhythmK/p/4734673.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!