sharp 正在参加 2021 年度 OSC 中国开源项目评选,请投票支持!
sharp 在 2021 年度 OSC 中国开源项目评选 中已获得 {{ projectVoteCount }} 票,请投票支持!
2021 年度 OSC 中国开源项目评选 正在火热进行中,快来投票支持你喜欢的开源项目!
2021 年度 OSC 中国开源项目评选 >>> 中场回顾
sharp 获得 2021 年度 OSC 中国开源项目评选「最佳人气项目」 !
授权协议 Apache-2.0
操作系统 跨平台
软件类型 开源软件
开源组织
地区 不详
投 递 者 Alias_Travis
适用人群 未知
收录时间 2021-09-22

软件简介

sharp 是一个高性能的 Node.js 模块,典型用例是将常见格式的大图像转换为较小的、对网络友好的 JPEG、PNG、WebP 和不同尺寸的 AVIF 图像。由于使用了 libvips 库,调整图像大小的速度通常比使用 ImageMagick 和 GraphicsMagick 快 4 到 5 倍。

颜色空间、嵌入的 ICC 配置文件和 alpha 透明通道都得到了正确处理,Lanczos 重采样可确保不会因速度而牺牲质量。除了图像调整大小外,还可以进行旋转、提取、合成和伽马校正等操作。

大多数运行 Node.js >= 12.13.0 的 macOS、Windows 和 Linux 系统不需要任何额外的安装或运行时依赖项。

例子

npm install sharp
const sharp = require('sharp');

回调

sharp(inputBuffer)
  .resize(320, 240)
  .toFile('output.webp', (err, info) => { ... });

Promise

sharp('input.jpg')
  .rotate()
  .resize(200)
  .jpeg({ mozjpeg: true })
  .toBuffer()
  .then( data => { ... })
  .catch( err => { ... });

Async/await

const semiTransparentRedPng = await sharp({
  create: {
    width: 48,
    height: 48,
    channels: 4,
    background: { r: 255, g: 0, b: 0, alpha: 0.5 }
  }
})
  .png()
  .toBuffer();

Stream

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);
展开阅读全文

代码

的 Gitee 指数为
超过 的项目

评论

点击引领话题📣 发布并加入讨论🔥
暂无内容
发表了博客
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
发表了问答
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
暂无内容
0 评论
4 收藏
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部