# @astropub/codecs 中文文档

规范地址：https://npmcn.com/package/%40astropub/codecs/

版本：0\.4\.4
npm 元数据更新时间：2022-04-04T15:55:02.827Z
中文译文更新时间：2026-11-07T01:19:03.668Z
软件包许可：\(CC0-1\.0 AND Apache-2\.0 AND BSD-3-Clause\)

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40astropub/codecs
原文对照：https://npmcn.com/package/%40astropub/codecs/?lang=original
项目主页：https://github.com/astro-community/codecs#readme
源码仓库：https://github.com/astro-community/codecs

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：9.3 MB，66 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2022-01-06T07:43:47.607Z
- 最新版本发布：2022-01-23T22:17:10.183Z
- 近 30 天下载量：4117（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：10（近 12 个月 0 个）

## 中文简介

节点JS的编码器

## 安装

```sh
npm install @astropub/codecs
```

也可以使用：`pnpm add @astropub/codecs`、`yarn add @astropub/codecs`、`bun add @astropub/codecs`

## README

# 编解码器

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

```shell
npm install @astropub/codecs
```

## 使用量

```js
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,缓冲,响应,或打入箭头.

```js
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` 函数返回已解码的图像。 它接受缓冲或打字箭头.

```js
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`。 。 。 .

```js
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` 函数返回已编码的图像。 它接受解码图像.

```js
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`。 。 。 .

```js
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` 函数返回已调整大小的图像。 它接受解码图像.

```js
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` 函数返回模糊的图像。 它接受解码图像.

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

### `blurhash`

这个 `blurhash` 函数返回模糊的图像,使用 [Wolt BlurHash 算法](https://github.com/woltapp/blurhash/blob/master/Algorithm.md)它接受解码图像.

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

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

### `type`

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

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

### `ext`

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

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

### `DecodedImage`

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

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

### `DecodedImage#encode`

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

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

### `DecodedImage#blur`

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

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

### `DecodedImage#blurhash`

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

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

### `DecodedImage#resize`

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

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

### `DecodedImage#color`

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

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

### `EncodedImage`

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

```js
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` 从当前编码的图像返回已承诺的已解码的图像.

```js
const decoded = await encoded.decoded()
```

## 类型

### `ImageType`

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

```js
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` 类型代表已知的图像内容类型.

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

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

## 许可证

**编解码器** 通常是一个重混 [斯库什!](https://github.com/GoogleChromeLabs/squoosh)。 。 。 .

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

代码从 [斯库什!](https://github.com/GoogleChromeLabs/squoosh) 依据Apache-2.0许可,版权Google Inc.

代码从 [Avif 编码器](https://github.com/AOMediaCodec/libavif) 依据BSD许可,版权为Joe Drago.

代码从 [莫兹杰普格](https://github.com/GoogleChromeLabs/squoosh/blob/12889d9d503a325e78823a8ebc48b54d4c2b9124/codecs/mozjpeg/LICENSE.codec.md) 根据修改后的(3-clause)BSD许可,版权为Viktor Szathmáry.

代码从 [联 合 国](https://github.com/GoogleChromeLabs/squoosh/tree/023304803f988ae39b98f95a369bed4f6f000953/codecs/jxl) 依据Apache-2.0许可,版权Google Inc.

代码从 [氧化金](https://github.com/shssoichiro/oxipng) 根据麻省理工学院的许可,版权为Joshua Holmer.

代码从 [网络P](https://github.com/webmproject/libwebp) 由Modific (3-clause) BSD License,版权:Google Inc.授权使用.

代码从 [网络P2](https://github.com/GoogleChromeLabs/squoosh/tree/89105bbb22e6247e568af97250477268d7284d11/codecs/wp2) 依据Apache-2.0许可,版权Google Inc.

代码从 [模糊](https://github.com/woltapp/blurhash) 获得麻省理工学院许可,版权为Olli Mahlamäki.

## 常见问题

### @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 和提交记录判断。

