暂做笔记,带后续验证通过后,再补充 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;
时间: 2024-11-07 11:39:58