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

devtools/2024/9/22 23:42:49/

目录

    • 背景
    • 效果图
    • 准备
    • 代码
    • 参考

背景

效果图

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

在这里插入图片描述

准备

webp_13">安装cwebp等命令,转换文件格式

yum install  libwebp-devel libwebp-tools

ImageMagick_18">安装ImageMagick,压缩文件

yum install  ImageMagick

ImageMagick_23">下载Lua API 操控ImageMagick的依赖包

  • 网址:https://github.com/leafo/magick,到Releases下载,解压后得到如下:
    在这里插入图片描述
  • 复制magick包到lualib里,如下所示。
    在这里插入图片描述

代码

  • 修改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_filenamelocal image_process = ngx.var.arg_image_process
-- local image_process = ngx.req.get_uri_args()["image_process"]function fileExists(name)local f=io.open(name,"r")if f~=nil then io.close(f) return true else return false end
endfunction tryServeFile(name, contentType)if fileExists(name) thenlocal f = io.open(name, "rb")local content = f:read("*all")f:close()if contentType ~= "" thenngx.header["Content-Type"] = contentTypeendngx.print(content)return trueendreturn false
endfunction serveFileOr404(name, contentType)if not tryServeFile(name, contentType) thenngx.exit(404)end
endfunction ratioZip(originalFile,ratioFile) if not fileExists(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_numlocal h = img:get_height() * r_numimg:destroy()local size_str = tostring(math.floor(w)) .. "x" .. tostring(math.floor(h))magick.thumb(originalFile, size_str , ratioFile)endendfunction outputWebp(originalFile,commandExe)local newFile = originalFile .. ".converted.webp"local headers = ngx.req.get_headers()if headers ~= nil and headers["accept"] ~= nil and string.find(headers["accept"], "image/webp") ~= nil thenif not tryServeFile(newFile, "image/webp") thenos.execute(commandExe .. " -q 80 " .. originalFile .. " -o " .. newFile);serveFileOr404(originalFile, "image/webp")endelseif image_process ~= nil and string.find(image_process, "webp") ~= nil thenif not tryServeFile(newFile, "image/webp") thenos.execute(commandExe .. " -q 80 " .. originalFile .. " -o " .. newFile);serveFileOr404(originalFile, "image/webp")endelseserveFileOr404(originalFile, "")endendfunction zipAndWebp(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 ~= nil and string.find(image_process, "resize") ~= nil thenratio_num = string.match(image_process, "%d+")end-- ngx.say("imgp ",ratio_num)-- 是否要压缩if  ratio_num ~= nil and tonumber(ratio_num) > 0 thenlocal ratioFile = originalFile .. ratio_numratioZip(originalFile,ratioFile) outputWebp(ratioFile,commandExe)elseoutputWebp(originalFile,commandExe)endendif string.find(originalFile, ".png") ~= nil thenzipAndWebp(originalFile,"cwebp")
elseif string.find(originalFile, ".jpg") ~= nil thenzipAndWebp(originalFile,"cwebp")
elseif string.find(originalFile, ".jpeg") ~= nil thenzipAndWebp(originalFile,"cwebp")
elseif string.find(originalFile, ".gif") ~= nil thenoutputWebp(originalFile,"gif2webp")
else serveFileOr404(originalFile, "")
end
  • 参数格式
?image_process=format,webp/resize,p_20

参考

  • https://blog.rexskz.info/use-openresty-to-optimize-image-size-with-avif-and-webp.html
  • https://blog.csdn.net/F_angT/article/details/90073211
  • https://www.jianshu.com/p/b14c89b57493

http://www.ppmy.cn/devtools/22396.html

相关文章

在网站源码后台增加响应式布局

一本教材上的网站源码,后台在手机上查看还是按照电脑的页面样式,不方便查看和发布新内容。教材上讲了响应式布局。对于页面结构简单的网站,可以利用响应式,使页面自动适用各种屏幕的分辨率。 今天在一个网站源码的后台使用了响应…

二维码门楼牌管理应用平台建设:强化消防安全,隐患整改更高效

文章目录 前言一、二维码门楼牌管理应用平台概述二、消防检查隐患整改的重要性三、二维码门楼牌管理应用平台在消防隐患整改中的应用四、结论 前言 随着城市化的快速推进,消防安全管理面临着前所未有的挑战。二维码门楼牌管理应用平台的建设,为消防安全…

Linux常用命令简单介绍(面试常考!!!)

文件 ls (list files) : 列出当前目录下的的目录和文件chown (change owner) : 修改所属用户,也可以同时更改文件所属组chmod (change mode) : 修改用户的权限。chgrp (change group) : 修改所属组cp(copy file):…

数据结构-数组

目录 一维数组 二维数组 数据结构是计算机存储、组织数据的一种方式,相互之间存在一种或多种特定关系的数据元素的集合。数据结构研究的内容是如何按一定的逻辑结构,把数据组织起来,并选择适当的存储表示方法把逻辑结构组织好的数据存储到计算机的存储器里。数据结构和算法…

selenium在Pycharm中结合python的基本使用、交互、无界面访问

下载 下载与浏览器匹配的浏览器驱动文件,这里一定注意的是,要选择和浏览器版本号相同的驱动程序,否则后面会有很多问题。 (1)浏览器(以google为例)版本号的查询: 我这里的版本号是1…

addEventListener()方法中的参数,以及作用

addEventListener() 方法是 JavaScript 中用于向指定元素添加事件监听器的方法。它有两个参数: 事件名称 (type):这是一个字符串,表示要监听的事件名称。例如,click、mouseover、keydown 等。事件处理函数 (listener)&#xff1a…

代码随想录算法训练营DAY40\DAY41|C++动态规划Part.3|343.整数拆分、96.不同的二叉搜索树

DAY40休息日,本篇为DAY41的内容 文章目录 343.整数拆分思路dp含义递推公式(难点)初始化遍历顺序打印 CPP代码数学方法归纳证明法 96.不同的二叉搜索树思路dp含义递推公式初始化遍历顺序打印 CPP代码题目总结 343.整数拆分 力扣题目链接 文章…

verilog 从入门到看得懂---matlab 自动生成verilog

matlab 的强大不用多说,以前经常用simulink 生成c,最近尝试用simulink进行了verilog的生成,方法也很简单。 一个简单的示例如下。 1,新建一个模型文件,并且根据需要进行模型搭建 2.配置HDL生成模块 3.点击 generation…