sharp README

The project's README from github.com/lovell/sharp. As one markdown file: README.md.

sharp

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 (>= 20.9.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.

Documentation

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

Examples

npm install sharp
// ESM
import sharp from 'sharp';

// CJS
const sharp = require('sharp');
await sharp(inputBuffer)
  .resize({ width: 320, height: 240 })
  .toFile('output.webp', (err, info) => { ... });
const output = await sharp('input.jpg')
  .autoOrient()
  .resize({ width: 200 })
  .jpeg({ mozjpeg: true })
  .toBuffer();
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);

Contributing

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

Licensing

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.

sharp (lovell/sharp)

Searching Database…

32.8K ·GitHub
这是做什么的?sharp 是一个 Node.js 的高性能图像处理模块,基于 libvips 库构建。它最常见的用途是把常见格式的大图转换成更小、更适合网页使用的多种尺寸的文件。
这是一个我在自己代码里调用的库,还是一个有界面的独立应用?它是一个库,不是应用:一个 Node-API 模块,需要你在自己的 JavaScript 代码中调用。它没有图形界面,也没有命令行界面——缩放和格式转换都发生在你的程序内部。
我怎么把它加到项目里?用 'npm install sharp' 把它作为依赖添加即可。完整的安装说明和 API 文档都在 sharp.pixelplumbing.com 上。
我需要先编译 libvips 或安装其他东西吗?不需要手动配置 libvips:现在大多数 macOS、Windows 和 Linux 系统都无需额外安装任何东西,也没有运行时依赖。
它在旧版本的 Node.js 上能运行吗?不能——它需要一个支持 Node-API v9 的运行环境:Node.js 20.9.0 或更新版本,也可以用 Deno 或 Bun。更旧的 Node 版本不受支持。
和 ImageMagick 相比它有多快?得益于 libvips,缩放速度通常比最快的 ImageMagick 和 GraphicsMagick 设置还要快 4 到 5 倍。
这么快的速度会牺牲图片质量吗?不会——sharp 使用 Lanczos 重采样算法,速度快但质量不打折扣,而且色彩空间、内嵌的 ICC 配置文件和 Alpha 透明通道都能正确处理。
它支持哪些图片格式?README 中列出了 JPEG、PNG、WebP、GIF 和 AVIF,仓库简介里还提到了 TIFF,SVG 则出现在 GitHub 的主题标签中。
我可以在商业项目中使用 sharp 吗?可以——它采用 Apache License 2.0 许可证,这是一种宽松的许可证,允许商业使用、修改和再分发。
Creator
lovell
Size
58.8 MB
Stars
32.8K
Age
13y
Commits · 90 Days