ipx
基于 Sharp 和 libvips 的高性能、安全易用的图像代理。
这是 IPX v2 的活跃分支。请查看 ipx/v1 获取 v1 文档。
由 sharp 和 svgo 提供支持的高性能、安全且易于使用的图像优化器。
由 Nuxt Image 和 Netlify 使用,并向所有人开放!
使用 CLI
您可以使用 ipx
命令启动服务器。
使用 npx
npx ipx serve --dir ./
使用 bun
bun x npx ipx serve --dir ./
默认的服务目录是当前工作目录。
编程 API
您可以将 IPX 用作中间件或直接使用 IPX 接口。
import { createIPX, ipxFSStorage, ipxHttpStorage } from "ipx";
const ipx = createIPX({
storage: ipxFSStorage({ dir: "./public" }),
httpStorage: ipxHttpStorage({ domains: ["picsum.photos"] }),
});
示例:与 unjs/h3 一起使用
import { listen } from "listhen";
import { createApp, toNodeListener } from "h3";
import {
createIPX,
ipxFSStorage,
ipxHttpStorage,
createIPXH3Handler,
} from "ipx";
const ipx = createIPX({
storage: ipxFSStorage({ dir: "./public" }),
httpStorage: ipxHttpStorage({ domains: ["picsum.photos"] }),
});
const app = createApp().use("/", createIPXH3Handler(ipx));
listen(toNodeListener(app));
示例:使用 express
import { listen } from "listhen";
import express from "express";
import {
createIPX,
ipxFSStorage,
ipxHttpStorage,
createIPXNodeServer,
} from "ipx";
const ipx = createIPX({
storage: ipxFSStorage({ dir: "./public" }),
httpStorage: ipxHttpStorage({ domains: ["picsum.photos"] }),
});
const app = express().use("/", createIPXNodeServer(ipx));
listen(app);
URL 示例
获取原始图像
/_/static/buffalo.png
将格式更改为 webp
并保持其他设置与源文件相同
/f_webp/static/buffalo.png
保留原始格式 (png
) 并将宽度设置为 200
/w_200/static/buffalo.png
使用 embed
方法将图像大小调整为 200x200px
并将格式更改为 webp
/embed,f_webp,s_200x200/static/buffalo.png
配置
您可以使用 IPX_*
环境变量通用地自定义 IPX 配置。
IPX_ALIAS
- 默认值:
{}
- 默认值:
文件系统源选项
(仅在 CLI 下默认启用)
IPX_FS_DIR
- 默认值:
.
(当前工作目录)
IPX_FS_MAX_AGE
- 默认值:
300
HTTP(s) 源选项
(仅在 CLI 下默认启用)
IPX_HTTP_DOMAINS
- 默认值:
[]
IPX_HTTP_MAX_AGE
- 默认值:
300
IPX_HTTP_FETCH_OPTIONS
- 默认值:
{}
IPX_HTTP_ALLOW_ALL_DOMAINS
- 默认值:
false
修饰符
属性 | 文档 | 示例 | 备注 |
---|---|---|---|
width / w | 文档 | /width_200/buffalo.png | |
height / h | 文档 | /height_200/buffalo.png | |
resize / s | 文档 | /s_200x200/buffalo.png | |
kernel | 文档 | /s_200x200,kernel_nearest/buffalo.png | 支持的内核: nearest , cubic , mitchell , lanczos2 和 lanczos3 (默认)。 |
fit | 文档 | /s_200x200,fit_outside/buffalo.png | 设置 resize 的 fit 选项。 |
position / pos | 文档 | /s_200x200,pos_top/buffalo.png | 设置 resize 的 position 选项。 |
trim | 文档 | /trim_100/buffalo.png | |
extend | 文档 | /extend_{top}_{right}_{bottom}_{left}/buffalo.png | 使用提供的背景颜色或从图像中派生的像素来扩展/填充/突出图像的一个或多个边缘。 |
background / b | _ | /r_45,b_00ff00/buffalo.png | |
extract | 文档 | /extract_{left}_{top}_{width}_{height}/buffalo.png | 提取/裁剪图像的某个区域。 |
format / f | 文档 | /format_webp/buffalo.png | 支持的格式: jpg , jpeg , png , webp , avif , gif , heif , tiff 和 auto (仅在中间件中实验性支持) |
quality / q | _ | /quality_50/buffalo.png | 可接受值: 0 到 100 |
rotate | 文档 | /rotate_45/buffalo.png | |
enlarge | _ | /enlarge,s_2000x2000/buffalo.png | 允许图像放大。默认情况下,返回的图像在任何维度上都不会大于源图像,同时保留请求的宽高比。 |
flip | 文档 | /flip/buffalo.png | |
flop | 文档 | /flop/buffalo.png | |
sharpen | 文档 | /sharpen_30/buffalo.png | |
median | 文档 | /median_10/buffalo.png | |
blur | 文档 | /blur_5/buffalo.png | |
gamma | 文档 | /gamma_3/buffalo.png | |
negate | 文档 | /negate/buffalo.png | |
normalize | 文档 | /normalize/buffalo.png | |
threshold | 文档 | /threshold_10/buffalo.png | |
tint | 文档 | /tint_1098123/buffalo.png | |
grayscale | 文档 | /grayscale/buffalo.png | |
animated | - | /animated/buffalo.gif | 实验性 |