AVIF似乎是一种新格式,并没有找到太多信息。但是,让我们立即看看调查结果:
从 [1] 我找到了一本烹饪书,如何在 Nginx 上提供这些服务:
http {
# ... Omitted.
map $http_accept $ai {
"~avif" "a";
"~webp" "w";
default "";
}
types {
image/avif avif;
}
server {
# ... Omitted.
# Rewrite .i files.
location ~ \.i$ {
# Change .i request to .avif file.
if ($ai = "a") {
rewrite ^(.*)$ $1.avif last;
}
# Change .i to .webp file.
if ($ai = "w") {
rewrite ^(.*)$ $1.webp last;
}
# If no AVIF support, use PNG image.
if ($ai = "") {
rewrite ^(.*)$ $1.png last;
}
}
}
}
该解决方案依赖于 Accept 标头和 map 语句。这里的图像假定以 .i 结尾,但如果修改的话,这也适用于 .png 和其他格式。
对服务器的请求应具有“image/avif”类型。
有一个名为 Squoosh 的服务,您可以在其中转换图像。 [2] 中还提到了一些依赖于 Sharp 的编程方法。
关于转换的代码:
import * as sharp from 'sharp';
sharp('input.png')
.toFormat('heif', { quality: 30, compression: 'av1' })
.toFile('output.avif')
.then(info => console.log(info));
Sources:
[1] https://www.dotnetperls.com/nginx-examples
[2] https://dev.to/adamlacombe/how-to-convert-images-to-avif-in-nodejs-5083