0


centos7 openresty lua 自适应webp和缩放图片

目录

背景

  • 缩小图片体积,提升加载速度,节省流量。

效果图

  • 参数格式 : ?image_process=format,webp/resize,p_20在这里插入图片描述

在这里插入图片描述

准备

安装

cwebp

等命令,转换文件格式

yum install  libwebp-devel libwebp-tools

安装

ImageMagick

,压缩文件

yum install  ImageMagick

下载Lua API 操控ImageMagick的依赖包

代码

  • 修改conf文件,添加如下内容。
      location ~ \.(jpe?g|png|gif)$ {
               add_header Access-Control-Allow-Origin *;
              add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
               add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
               content_by_lua_file /usr/local/openresty/nginx/conf/lua/image-convert.lua;
        }
  • 创建/usr/local/openresty/nginx/conf/lua/image-convert.lua 文件。添加一下内容。
local originalFile = ngx.var.request_filename
 

local image_process = ngx.var.arg_image_process
-- local image_process = ngx.req.get_uri_args()["image_process"]functionfileExists(name)local f=io.open(name,"r")if f~=nilthen io.close(f)returntrueelsereturnfalseendendfunctiontryServeFile(name, contentType)iffileExists(name)thenlocal f = io.open(name,"rb")local content = f:read("*all")
        f:close()if contentType ~=""then
            ngx.header["Content-Type"]= contentType
        end
        ngx.print(content)returntrueendreturnfalseendfunctionserveFileOr404(name, contentType)ifnottryServeFile(name, contentType)then
        ngx.exit(404)endendfunctionratioZip(originalFile,ratioFile)ifnotfileExists(ratioFile)thenlocal ratio_num = string.match(image_process,"%d+")local magick =require("magick")local img =assert(magick.load_image(originalFile))local r_num =tonumber(ratio_num)/100local w = img:get_width()* r_num
        local h = img:get_height()* r_num
        img:destroy()local size_str =tostring(math.floor(w)).."x"..tostring(math.floor(h))
        magick.thumb(originalFile, size_str , ratioFile)endendfunctionoutputWebp(originalFile,commandExe)local newFile = originalFile ..".converted.webp"local headers = ngx.req.get_headers()if headers ~=niland headers["accept"]~=niland string.find(headers["accept"],"image/webp")~=nilthenifnottryServeFile(newFile,"image/webp")then
             os.execute(commandExe .." -q 80 ".. originalFile .." -o ".. newFile);serveFileOr404(originalFile,"image/webp")endelseif image_process ~=niland string.find(image_process,"webp")~=nilthenifnottryServeFile(newFile,"image/webp")then
             os.execute(commandExe .." -q 80 ".. originalFile .." -o ".. newFile);serveFileOr404(originalFile,"image/webp")endelseserveFileOr404(originalFile,"")endendfunctionzipAndWebp(originalFile,commandExe)--ngx.header["Content-Type"] = "text/html; charset=UTF-8"--ngx.say("imgp ",image_process,string.find(image_process, "resize")," 比例 ",number)local ratio_num =nilif image_process ~=niland string.find(image_process,"resize")~=nilthen
        ratio_num = string.match(image_process,"%d+")end-- ngx.say("imgp ",ratio_num)-- 是否要压缩if  ratio_num ~=nilandtonumber(ratio_num)>0thenlocal ratioFile = originalFile .. ratio_num
        ratioZip(originalFile,ratioFile)outputWebp(ratioFile,commandExe)elseoutputWebp(originalFile,commandExe)endendif string.find(originalFile,".png")~=nilthenzipAndWebp(originalFile,"cwebp")elseif string.find(originalFile,".jpg")~=nilthenzipAndWebp(originalFile,"cwebp")elseif string.find(originalFile,".jpeg")~=nilthenzipAndWebp(originalFile,"cwebp")elseif string.find(originalFile,".gif")~=nilthenoutputWebp(originalFile,"gif2webp")elseserveFileOr404(originalFile,"")end
  • 参数格式
?image_process=format,webp/resize,p_20

参考


本文转载自: https://blog.csdn.net/baidu_19473529/article/details/136945247
版权归原作者 愤怒的苹果ext 所有, 如有侵权,请联系我们删除。

“centos7 openresty lua 自适应webp和缩放图片”的评论:

还没有评论