这款高速 Node-API 模块的典型用例是将常见格式的大图像转换为更小、更适合网页浏览的不同尺寸的 JPEG、PNG、WebP、GIF 和 AVIF 图像。
它可以与所有支持 Node-API v9 的 JavaScript 运行时一起使用,包括Node.js(^18.17.0 或 >= 20.3.0)、Deno 和 Bun。
由于使用了 libvips,调整图像大小通常比使用最快的 ImageMagick 和 GraphicsMagick 设置快 4 到 5 倍。
色彩空间、嵌入的 ICC 配置文件和 Alpha 透明通道均能正确处理。Lanczos 重采样确保不会为了速度而牺牲质量。
除了图像大小调整外,还可以执行旋转、提取、合成和伽玛校正等操作。
大多数现代 macOS、Windows 和 Linux 系统不需要任何额外的安装或运行时依赖项。
访问 sharp.pixelplumbing.com 获取完整的
安装说明、
API 文档、
基准测试 和
更新日志。
npm install sharp
const sharp = require('sharp');
sharp(inputBuffer)
.resize(320, 240)
.toFile('output.webp', (err, info) => { ... });
sharp('input.jpg')
.rotate()
.resize(200)
.jpeg({ mozjpeg: true })
.toBuffer()
.then( data => { ... })
.catch( err => { ... });
const semiTransparentRedPng = await sharp({
create: {
width: 48,
height: 48,
channels: 4,
background: { r: 255, g: 0, b: 0, alpha: 0.5 }
}
})
.png()
.toBuffer();
const roundedCorners = Buffer.from(
'<svg><rect x="0" y="0" width="200" height="200" rx="50" ry="50"/></svg>'
);
const roundedCornerResizer =
sharp()
.resize(200, 200)
.composite([{
input: roundedCorners,
blend: 'dest-in'
}])
.png();
readableStream
.pipe(roundedCornerResizer)
.pipe(writableStream);
贡献者指南
涵盖报告错误、请求功能和提交代码更改。
版权所有 2013 Lovell Fuller 及他人。
根据 Apache 许可证 2.0 版(以下简称“许可证”)授权;您不得在未遵守本许可证的情况下使用此文件。您可以在以下网址获取许可证副本:
https://www.apache.org/licenses/LICENSE-2.0
除非适用法律要求或书面同意,否则,根据本许可证分发的软件均按“原样”分发,不附带任何明示或暗示的保证或条件。请参阅许可证,了解本许可证下特定语言的权限和限制。