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

目录

    • 背景
    • 效果图
    • 准备
      • 安装`cwebp`等命令,转换文件格式
      • 安装`ImageMagick`,压缩文件
      • 下载Lua API 操控ImageMagick的依赖包
    • 代码
    • 参考

背景

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

效果图

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

在这里插入图片描述

准备

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

yum install  libwebp-devel libwebp-tools

安装ImageMagick,压缩文件

yum install  ImageMagick

下载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.mzph.cn/web/4433.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Llama-7b-Chinese本地推理

Llama-7b-Chinese 本地推理 基础环境信息(wsl2安装Ubuntu22.04 miniconda) 使用miniconda搭建环境 (base) :~$ conda create --name Llama-7b-Chinese python3.10 Channels:- defaults Platform: linux-64 Collecting package metadata (repodata.js…

JavaScrip--数组迭代

Array.forEach() forEach() 方法为每个数组元素调用一次函数&#xff08;回调函数&#xff09;。 var txt ""; var numbers [45, 4, 9, 16, 25]; numbers.forEach(myFunction);function myFunction(value, index, array) {txt txt value "<br>&quo…

Linux下软硬链接和动静态库制作详解

目录 前言 软硬链接 概念 软链接的创建 硬链接的创建 软硬链接的本质区别 理解软链接 理解硬链接 小结 动静态库 概念 动静态库的制作 静态库的制作 动态库的制作 前言 本文涉及到inode和地址空间等相关概念&#xff0c;不知道的小伙伴可以先阅读以下两篇文章…

智慧校园建设指导

智慧校园是一个庞大的业务系统&#xff0c;他涉及到校园事务的各个方面&#xff0c;包括教务&#xff0c;考务&#xff0c;教工&#xff0c;学工&#xff0c;办公&#xff0c;科研等。因此&#xff0c;建设符合学校业务需求的智慧校园平台&#xff0c;不仅需要做到认真负责外&a…

C语言位运算详解(移位操作符、位操作符)

目录 一、整数在内存中的存储方式 二、移位操作符 1、左移操作符 2、右移操作符 a.逻辑右移 b.算数右移 ps、移位操作符使用警告 三、位操作符 用例代码&#xff1a; a.按位与&#xff08;&&#xff09; b.按位或&#xff08;|&#xff09; c.按位异或&#xf…

【笔试强训】Day4 --- Fibonacci数列 + 单词搜索 + 杨辉三角

文章目录 1. Fibonacci数列2. 单词搜索3. 杨辉三角 1. Fibonacci数列 【链接】&#xff1a;Fibonacci数列 解题思路&#xff1a;简单模拟题&#xff0c;要最少的步数就是找离N最近的Fibonacci数&#xff0c;即可能情况只有比他小的最大的那个Fibonacci数以及比他大的最小的那…

《软件设计师教程:计算机网络浅了解计算机之间相互运运作的模式》

​ 个人主页&#xff1a;李仙桎 &#x1f525; 个人专栏: 《软件设计师》 ⛺️生活的理想&#xff0c;就是为了理想的生活! ​ ⛺️前言&#xff1a;各位铁汁们好啊&#xff01;&#xff01;&#xff01;&#xff0c;今天开始继续学习中级软件设计师考试相关的内容&#xff0…

Java高阶私房菜:JVM垃圾回收机制及算法原理探究

目录 垃圾回收机制 什么是垃圾回收机制 JVM的自动垃圾回收机制 垃圾回收机制的关键知识点 初步了解判断方法-引用计数法 GCRoot和可达性分析算法 什么是可达性分析算法 什么是GC Root 对象回收的关键知识点 标记对象可回收就一定会被回收吗&#xff1f; 可达性分析算…

【免费源码下载】完美运营版商城 虚拟商品全功能商城 全能商城小程序 智慧商城系统 全品类百货商城php+uniapp

简介 完美运营版商城/拼团/团购/秒杀/积分/砍价/实物商品/虚拟商品等全功能商城 干干净净 没有一丝多余收据 还没过手其他站 还没乱七八走的广告和后门 后台可以自由拖曳修改前端UI页面 还支持虚拟商品自动发货等功能 挺不错的一套源码 前端UNIAPP 后端PHP 一键部署版本&am…

52832 不使用micro_lib ,同时使用AC6编译器且使用printf问题

1. 因为我的工程用AC6是因为要跑自己的C 和 TensorFlow lite micro. 所以是C&#xff0c;C混合的工程&#xff0c;但是一直没法打印&#xff0c;所以写一个总结。 基本说明&#xff1a; micro_lib这种情况不要选&#xff0c;因为存在C文件 第一个坑&#xff1a; 第二个坑&…

【pytorch】内容链接汇总

pytorch报错部分 链接备注RuntimeError: one of the variables needed for gradient computation has been modified代码报错姿态估计hrnet报错&#xff1a;ModuleNotFoundError: No module named ‘nms.cpu_nms‘HRNetRuntimeError: Invalid device string: ‘0‘gpuexpected…

windows 避免电脑强制息屏

许多打工人的电脑被公司设置了隔一段时间没有操作&#xff0c;就会自动息屏&#xff0c;如何避免这种事发生呢 方案一 自动操作鼠标的软件 如果能自由安装软件&#xff0c;可以下载自动移动鼠标的软件&#xff0c;设置鼠标每隔多长时间做一次什么操作&#xff0c;防止锁屏 方…

LIUNX:系统编程动态库加载(1)

目录 操作系统角度理解 如何加载 怎么管理库 编址 操作系统角度理解 如何加载 首先main想要运行&#xff0c;首先要为main创建task_struct和mm_struct&#xff0c;然后将main的代码和数据加载到内存&#xff0c;将main的代码通过页表映射到mm_struct的正文代码段&#xff0…

村庄集中式生活废水处理设备技术工艺

诸城市鑫淼环保小编带大家了解一下村庄集中式生活废水处理设备技术工艺 工艺&#xff1a; 生物接触氧化法指由浸没在污水中的填料和曝气系统构成的污水处理方法&#xff0c;在有氧条件下&#xff0c;污水与填料表面的生物膜广泛接触&#xff0c;使污水得到净化。填料指在污水处…

【C++】循环语句中引起的循环引用问题

在C中&#xff0c;循环语句&#xff08;如for, while, do-while&#xff09;与引用的结合使用可能会引发一些特定的问题&#xff0c;尤其是当涉及到循环引用或者在循环中不当管理引用时。 1. 循环引用问题 循环引用通常与智能指针&#xff08;如std::shared_ptr&#xff09;相…

leetcode-比较版本号-88

题目要求 思路 1.因为字符串比较大小不方便&#xff0c;并且因为需要去掉前导的0&#xff0c;这个0我们并不知道有几个&#xff0c;将字符串转换为数字刚好能避免。 2.当判断到符号位的时候加加&#xff0c;跳过符号位。 3.判断数字大小&#xff0c;来决定版本号大小 4.核心代…

掌握Midjourney视觉艺术的关键提示词指南

在数字艺术的海洋中&#xff0c;Midjourney以其独特的图像生成能力脱颖而出&#xff0c;为艺术家和创意工作者提供了前所未有的创造自由。要真正掌握这一工具&#xff0c;理解并有效使用各种提示词至关重要。本文将深入探索Midjourney中的“风格关键词”、“场景关键词”、“视…

Unity | 优化专项-包体 | 字体

1. 字体包体占用 常用汉字字体文件大小通常在 10M~12M 左右&#xff0c;大概包含常见汉字 3.5w 个。我国汉字有大约将近十万个&#xff0c;全字库的大小对于游戏包体是灾难性的 在小游戏中&#xff0c;即使是常见汉字&#xff0c;大小也足以影响小游戏总包体&#xff0c;进而…

大模型日报2024-04-27

大模型日报 2024-04-27 大模型资讯 AI未来趋势&#xff1a;检索增强型生成&#xff08;RAG&#xff09;技术融合语言与搜索 摘要: 随着专家暗示大型语言模型&#xff08;LLMs&#xff09;的技术发展接近极限&#xff0c;检索增强型生成&#xff08;RAG&#xff09;成为关注焦点…

qmt教程2----订阅单股行情,提供源代码

链接 qmt教程2----订阅单股行情&#xff0c;提供源代码 (qq.com) qmt教程1---qmt安装&#xff0c;提供下载链接 今天我重新封装了全部qmt的内容&#xff0c;包括数据&#xff0c;交易 qmt交易 我本来打算全部上次git的&#xff0c;但是考虑到毕竟是实盘的内容&#xff0c;就放…