nginx+Lua+cwebp+GraphicsMagick图片压缩

原创
2018/04/25 16:40
阅读数 1.8K

图片压缩支持URL:http://lua.img.com/thumb/2018/04/24/w.jpg_900x600.webp

原图片地址为http://lua.img.com/2018/04/24/w.jpg

图片高宽:900x600

转换格式:webp/png/gif/jpg

 

附上代码:

----=====================
---图片展示
---======================

local cjson = require "cjson";
ngx.log(ngx.ERR,'uri:',ngx.var.request_uri);
--ngx.say(ngx.var.request_uri);

--设置全局路径
imageRootPath  = '/home/ubuntu/ngImage/';
imgRootPath = '/home/ubuntu/ngImage';

recvicePath = ngx.var.request_uri;
-- 生成存储完整路径
path = imgRootPath..recvicePath;

--判断文件是否存在
function file_exists(path)
   local  f,err = io.open(path);
   if (err ~= nil) then
    return false;    
    end
    f:close(); --关闭文件句柄
    return true;
end

local res =  file_exists(path);
--ngx.log(ngx.ERR,"file exists:",res);

if (res == true) then 
  return  res;
end

--切割字符串
function split( str,reps )
    local resultStrList = {}
    string.gsub(str,'[^'..reps..']+',function ( w )
        table.insert(resultStrList,w)
    end)
    return resultStrList
end

local pInfo =  split(recvicePath,'_');

--ngx.log(ngx.ERR,'path:',cjson.encode(arr));

local len =  #pInfo;
if (len ~= 2) then
    ngx.log(ngx.ERR,'params errr:',recvicePath);    
    ngx.exit(404);
end

--查看源文件是否存在
local  rPath = string.sub(pInfo[1],7,string.len(pInfo[1]));

local oldPath = imgRootPath..rPath;

if ( file_exists(oldPath)  == false) then 
    ngx.log(ngx.ERR,'oldPath:','源文件不存在'..oldPath);
    ngx.exit(404);
end

---区分文件后缀是否为webp/还是常规图片
local suffix =  split(pInfo[2],'.');

len  = #suffix;
--local commond = 'gm convert'; --GraphicsMagick软件包

if (len ~= 2 ) then 
  ngx.log(ngx.ERR,'path error:',recvicePath);
  ngx.exit(404);
end


-- if (suffix[2] == 'webp') then 
--   commond = 'cwebp'; --webp 格式图片命令
-- end

---拆分长宽

local wh =  split(suffix[1],'x');
len =  #wh;
local w = nil;
local h = nil;
if ( len ~= 2) then 
    ngx.log(ngx.ERR,'w_h error:','长宽必须填写');
    ngx.exit(404);
end

w = wh[1]
h =  wh[2]

local command = nil;

--ngx.log(ngx.ERR,'path:',oldPath.."*** thumbPath:"..path);

--创建目录
local mp = string.sub(path,1,38);

--ngx.log(ngx.ERR,'mk path:',mp);

if (file_exists(mp) == false) then 
  local res =  os.execute( "mkdir -p "..mp);
   if (res ~= 0 ) then
     ngx.log(ngx.ERR,'mkdir erro:',mp);
    ngx.exit(404);
   end
end

if (suffix[2] == 'webp') then
   command = 'cwebp '..oldPath..' -resize '..w.." "..h ..' -o ' ..path      ; --webp 格式图片命令
else
   local whl = w * h;
   command  = "gm convert  -quality 100 "..oldPath.." -thumbnail '"..whl.."@'  -background transparent -gravity center -resize ".. w.."x".. h.." " ..path;
end

ngx.log(ngx.ERR,'commond:',command);

if (command ~= nil ) then
   local res = os.execute(command);
   ngx.log(ngx.ERR,'gm result:',res);
   if ( res ~= 0) then
      ngx.log(ngx.ERR,'gm result:','转换失败');
      ngx.exit(404);
   end
   return true;
end

ngx.log(ngx.ERR,'command:','command not exists');
ngx.exit(404);
 

ps:由于第一次写lua,一边学一遍写。代码写的很low。有很多需要改进的地方。有时间在处理,先记上。

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部