@astropub/codecs

中文文档
0.4.4发布于 2022/01/24(CC0-1.0 AND Apache-2.0 AND BSD-3-Clause)TypeScriptES Module零依赖近 30 天下载 4117

节点JS的编码器

中文使用文档
文档目录22 节
  1. 使用量
  2. API 密码
  3. load
  4. decode
  5. encode
  6. resize
  7. blur
  8. blurhash
  9. type
  10. ext
  11. DecodedImage
  12. DecodedImage#encode
  13. DecodedImage#blur
  14. DecodedImage#blurhash
  15. DecodedImage#resize
  16. DecodedImage#color
  17. EncodedImage
  18. EncodedImage#decode
  19. 类型
  20. ImageType
  21. ImageType
  22. 许可证

编解码器

编解码器 允许您使用读取、写出、编辑和分析图像.

npm install @astropub/codecs

使用量

import * as fs from 'node:fs/promises'
import * as codecs from '@astropub/codecs'

// load the JPG image
const image = await codecs.load(
    await fs.readFile('./kitten.jpg')
)

image.type   // string representing the image type ('image/jpeg')
image.data   // Uint8Array representing the image data
image.width  // number representing the image width
image.height // number representing the image height
image.ext    // string representing the image extension ('jpg')

const decoded = await image.decode()

decoded.data   // Uint8ClampedArray representing the decoded image data
decoded.width  // number representing the decoded image width
decoded.height // number representing the decoded image height

// encode the image as Avif & WebP, at 320, 640, & 960
for (const size of [ 320, 640, 960 ]) {
    const resized = await decoded.resize({ width: size })

    for (const type of [ 'image/avif', 'image/webp' ]) {
        const encoded = await resized.encode(type, { quality: 80 })

        encoded.type   // string representing the encoded image type ('image/webp')
        encoded.data   // Uint8Array representing the encoded image data
        encoded.width  // number representing the encoded image width (320 | 640 | 960)
        encoded.height // number representing the encoded image height
        encoded.ext    // string representing the encoded image extension ('webp')

        await fs.writeFile(`./kitten-${size}.${encoded.ext}`, encoded.data)
    }
}

API 密码

load

这个 load 函数返回已装入的图像。 它接受一个字符串路径,文件 URL,缓冲,响应,或打入箭头.

const image = await codecs.load('./kitten.jpg')

image.type   // string representing the image type ('image/jpeg')
image.data   // Uint8Array representing the image data
image.width  // number representing the image width
image.height // number representing the image height
image.ext    // string representing the image extension ('jpg')

decode

这个 decode 函数返回已解码的图像。 它接受缓冲或打字箭头.

const buffer = await fs.readFile('./kitten.jpg')

const decoded = await codecs.decode(buffer)

decoded.data   // Uint8ClampedArray representing the decoded image data
decoded.width  // number representing the decoded image width
decoded.height // number representing the decoded image height

个人解码器可供 avif, (中文(简体) ), jpg, (中文(简体) ), jxl, (中文(简体) ), png, (中文(简体) ), webp,以及 wp2。 。 。 .

import * as codecs from '@astropub/codecs'

codecs.avif.decode(await fs.readFile('./kitten.avif'))
codecs.jpg.decode(await fs.readFile('./kitten.jpg'))
codecs.jxl.decode(await fs.readFile('./kitten.jxl'))
codecs.png.decode(await fs.readFile('./kitten.png'))
codecs.webp.decode(await fs.readFile('./kitten.webp'))
codecs.wp2.decode(await fs.readFile('./kitten.wp2'))

encode

这个 encode 函数返回已编码的图像。 它接受解码图像.

const encodedImage = await codecs.encode(decoded, 'image/webp', { quality: 80 })

encoded.type   // string representing the encoded image type ('image/webp')
encoded.data   // Uint8Array representing the encoded image data
encoded.width  // number representing the encoded image width (320 | 640 | 960)
encoded.height // number representing the encoded image height
encoded.ext    // string representing the encoded image extension ('webp')

await fs.writeFile('./kitten.webp', encodedImage)

个人编码器可供 avif, (中文(简体) ), jpg, (中文(简体) ), jxl, (中文(简体) ), png, (中文(简体) ), webp,以及 wp2。 。 。 .

import * as codecs from '@astropub/codecs'

codecs.avif.encode(decoded)
codecs.jpg.encode(decoded)
codecs.jxl.encode(decoded)
codecs.png.encode(decoded)
codecs.webp.encode(decoded)
codecs.wp2.encode(decoded)

resize

这个 resize 函数返回已调整大小的图像。 它接受解码图像.

const resized = await codecs.resize(decoded, { width: 320 })

resized.data   // Uint8ClampedArray representing the resized image data
resized.width  // number representing the resized image width
resized.height // number representing the resized image height

如果没有指定,则调整大小 height 将从 width 使用公式 width / naturalWidth * naturalHeight。 。 。 .

blur

这个 blur 函数返回模糊的图像。 它接受解码图像.

const blurred = await codecs.blur(decoded, { radius: 30 })

blurhash

这个 blurhash 函数返回模糊的图像,使用 Wolt BlurHash 算法它接受解码图像.

const blurhashed = await decoded.blurhash({ width: 32 })

如未具体说明 height 将使用公式从图像宽度中确定 width / naturalWidth * naturalHeight。 。 。 .

type

这个 type 函数返回图像缓冲器的内容类型。 它接受缓冲或打字箭头.

// 'image/jpeg'
const type = await codecs.type(buffer)

ext

这个 ext 函数返回图像缓冲器的文件扩展名。 它接受缓冲或打字箭头.

// 'jpg'
const ext = await codecs.ext(buffer)

DecodedImage

这个 DecodedImage 类代表原始的已解码的图像数据.

const decoded = new DecodedImage(
    data   // Uint8ClampedArray
    width  // number
    height // number
)

DecodedImage#encode

这个 encode 函数 DecodedImage 返回当前已解码的图像中承诺的编码图像.

const encoded = await decoded.encoded('image/webp') // EncodedImage<'image/web', Uint8Array>

DecodedImage#blur

这个 blur 函数 DecodedImage 返回当前已解码的图像中承诺的模糊图像.

const blurred = await decoded.blur({ radius: 30 }) // DecodedImage

DecodedImage#blurhash

这个 blurhash 函数 DecodedImage 返回当前已解码图像中承诺的模糊图像.

const blurhash = await decoded.blurhash({ radius: 30 }) // DecodedImage

DecodedImage#resize

这个 resize 函数 DecodedImage 返回当前已解码的图像中承诺的调整图像.

const resized = await decoded.resize({ width: 320 }) // DecodedImage

DecodedImage#color

这个 color 财产 DecodedImage 返回已解码图像中的主要颜色.

decoded.color // [ 57, 52, 43 ]

EncodedImage

这个 EncodedImage 类代表分析,编码图像数据.

const encoded = new EncodedImage(
    type   // string ('image/avif' | 'image/gif' | 'image/jpeg' | 'image/jxl' | 'image/png' | 'image/svg+xml' | 'image/webp' | 'image/webp2')
    data   // Uint8Array
    width  // number
    height // number
)

EncodedImage#decode

这个 decode 函数 EncodedImage 从当前编码的图像返回已承诺的已解码的图像.

const decoded = await encoded.decoded()

类型

ImageType

这个 ImageType 类型代表已知的图像内容类型.

import type { ImageType } from '@astropub/codecs'

// 'image/avif' | 'image/gif' | 'image/jpeg' | 'image/jxl' | 'image/png' | 'image/svg+xml' | 'image/webp' | 'image/webp2'
ImageType

ImageType

这个 ImageType 类型代表已知的图像内容类型.

import type { ImageType } from '@astropub/codecs'

// 'image/avif' | 'image/gif' | 'image/jpeg' | 'image/jxl' | 'image/png' | 'image/svg+xml' | 'image/webp' | 'image/webp2'
ImageType

许可证

编解码器 通常是一个重混 斯库什!。 。 。 .

该项目的原始代码根据CC0-1.0许可证颁发.

代码从 斯库什! 依据Apache-2.0许可,版权Google Inc.

代码从 Avif 编码器 依据BSD许可,版权为Joe Drago.

代码从 莫兹杰普格 根据修改后的(3-clause)BSD许可,版权为Viktor Szathmáry.

代码从 联 合 国 依据Apache-2.0许可,版权Google Inc.

代码从 氧化金 根据麻省理工学院的许可,版权为Joshua Holmer.

代码从 网络P 由Modific (3-clause) BSD License,版权:Google Inc.授权使用.

代码从 网络P2 依据Apache-2.0许可,版权Google Inc.

代码从 模糊 获得麻省理工学院许可,版权为Olli Mahlamäki.

版本与发布记录

@astropub/codecs 共发布 10 个版本,首次发布于 2022/01/06;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.4.4latest2022/01/244 年前
0.4.32022/01/234 年前
0.4.22022/01/234 年前
0.4.12022/01/234 年前
0.4.02022/01/234 年前
0.3.22022/01/174 年前

关于 @astropub/codecs 的常见问题

@astropub/codecs 是什么?

节点JS的编码器

如何安装 @astropub/codecs?

在项目目录运行 npm install @astropub/codecs,或使用 pnpm add / yarn add / bun add。

@astropub/codecs 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@astropub/codecs 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@astropub/codecs 可以商用吗?

@astropub/codecs 使用 (CC0-1.0 AND Apache-2.0 AND BSD-3-Clause) 许可。声明的许可为 (CC0-1.0 AND Apache-2.0 AND BSD-3-Clause),请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@astropub/codecs 的下载量有多少?

根据 npm 官方下载统计,@astropub/codecs 在 2026/10/07 – 2026/11/05 这 30 天内被下载 4,117 次,日均约 137 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@astropub/codecs 还在维护吗?

最新版本 0.4.4 发布于 2022/01/24(4 年前);至今共发布 10 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/01/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。