Sharp:高性能 Node.js 图像处理/优化
Sharp: High performance Node.js image processing/optimization

原始链接: https://github.com/lovell/sharp

## Sharp:JavaScript 的高速图像处理 Sharp 是一个 Node-API 模块,专为极速图像转换和处理而设计。它擅长将大型图像调整为适合 Web 的格式,如 JPEG、PNG、WebP、GIF 和 AVIF,其速度比 ImageMagick/GraphicsMagick 快 4-5 倍,这归功于它使用了 libvips。 Sharp 兼容 Node.js、Deno 和 Bun(需要 Node-API v9 支持),能够准确处理色彩空间、ICC 配置文件和透明度。它通过 Lanczos 重采样提供高质量的缩放,以及旋转、提取、合成和伽马校正等功能。 在现代系统中,安装通常无需依赖。该模块支持文件和缓冲区输入/输出,并可集成到流式处理管道中。详细文档、基准测试和安装指南可在 sharp.pixelplumbing.com 找到。它是开源的,采用 Apache 2.0 许可。

Hacker News 新闻 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 Sharp: 高性能 Node.js 图像处理/优化 (github.com/lovell) 6 分,nateb2022 1小时前 | 隐藏 | 过去 | 收藏 | 1 条评论 Daiz 3分钟前 [–] 已经使用(并偶尔贡献)Sharp 相当一段时间了,无论是工作还是个人项目。 当你需要处理图像时,这是一个很棒的库。回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

sharp logo

The typical use case for this high speed Node-API module is to convert large images in common formats to smaller, web-friendly JPEG, PNG, WebP, GIF and AVIF images of varying dimensions.

It can be used with all JavaScript runtimes that provide support for Node-API v9, including Node.js (^18.17.0 or >= 20.3.0), Deno and Bun.

Resizing an image is typically 4x-5x faster than using the quickest ImageMagick and GraphicsMagick settings due to its use of libvips.

Colour spaces, embedded ICC profiles and alpha transparency channels are all handled correctly. Lanczos resampling ensures quality is not sacrificed for speed.

As well as image resizing, operations such as rotation, extraction, compositing and gamma correction are available.

Most modern macOS, Windows and Linux systems do not require any additional install or runtime dependencies.

Visit sharp.pixelplumbing.com for complete installation instructions, API documentation, benchmark tests and changelog.

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);

A guide for contributors covers reporting bugs, requesting features and submitting code changes.

Copyright 2013 Lovell Fuller and others.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

联系我们 contact @ memedata.com