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

news/2024/10/18 18:15:57/

目录

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

背景

效果图

  • 参数格式 : ?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/news/1447848.html

相关文章

深度图上色,深度图raw文件转为png,如何读取深度图raw文件?

raw文件,要知道宽、高、通道数、数据类型,就能顺利转化,下面是转化代码: import numpy as np import cv2# 图像的基本信息 width 640 # 图像宽度 height 480 # 图像高度 channels 1 # 图像通道数,例如3表示RGB d…

oracle 归档日志删除策略

未设置归档删除策略时, rman target / delete archivelog all; 可以直接删除归档。 如果设置了归档删除策略,delete archivelog all; 不能删除。如果加上 force 关键字可以删除。 RMAN> configure archivelog deletion policy to backed up 1 …

linux系列—— 使用systemd 的单元文件来定义如何挂载一个文件系统

使用systemd 的单元文件来定义如何挂载一个文件系统 1.打开文件2.写入内容3.启动4.解释 1.打开文件 sudo gedit /etc/systemd/system/your-unit-file-name.mount2.写入内容 [Unit] Description mount disk[Mount] What /dev/nvmexxxx Where /mnt/cf Type ntfs Options d…

【C 数据结构】二叉树

文章目录 【 1. 基本原理 】1.1 二叉树的性质1.2 满二叉树1.3 完全二叉树 【 2. 二叉树的顺序存储结构 】2.1 完全二叉树的顺序存储2.2 普通二叉树的顺序存储2.3 完全二叉树的还原 【 3. 二叉树的链式存储结构 】【 4. 二叉树的先序遍历 】4.1 递归实现4.2 非递归实现 【 5. 二…

基于昇腾AI | 英码科技EA500I使用AscendCL实现垃圾分类和视频物体分类应用

现如今,人工智能迅猛发展,AI赋能产业发展的速度正在加快,“AI”的需求蜂拥而来,但AI应用快速落地的过程中仍存在很大的挑战:向下需要适配的硬件,向上需要完善的技术支持,两者缺一不可。 基于此&…

【力扣周赛】第394场周赛

文章目录 1.统计特殊字母的数量2.使矩阵满足条件的最少操作次数 1.统计特殊字母的数量 题目链接 🍎该题涉及的小技巧:🐥 🐧①大写字母和对应的小写字母低5位都是相等的; 🐧②大写字母ASCII二进制第 6 位…

支持企业级 AI 应用,IT 基础架构应具备哪些能力?有哪些解决方案?

随着越来越多大语言模型(LLM)在行业落地,不少企业已开始进行生成式 AI 应用的试点、开发,或在生产环境中试用 AI 应用。这些应用场景不仅要求强大的算力,还非常考验 IT 基础设施对 GPU 的支持能力、资源调度灵活性、混…

Spring Boot集成Spring AI实现快速接入openAI

1.什么是Spring AI? Spring AI API 涵盖了广泛的功能。每个主要功能都在其专门的部分中进行了详细介绍。为了提供概述,可以使用以下关键功能: 跨 AI 提供商的可移植 API,用于聊天、文本到图像和嵌入模型。支持同步和流 API 选项。…