# @frostoven/libsquoosh 中文文档

规范地址：https://npmcn.com/package/%40frostoven/libsquoosh/

版本：0\.4\.8
npm 元数据更新时间：2024-04-29T12:16:00.920Z
中文译文更新时间：2026-11-15T15:55:05.761Z
软件包许可：Apache-2\.0

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

原始 npm 页面：https://www.npmjs.com/package/%40frostoven/libsquoosh
原文对照：https://npmcn.com/package/%40frostoven/libsquoosh/?lang=original
项目主页：https://squoosh.frostoven.com/
源码仓库：https://github.com/frostoven/Squoosh-with-CLI

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

- 包体积（解压后）：12.5 MB，22 个文件
- 模块格式：CommonJS
- TypeScript 类型：未声明
- Node.js 要求：\>=16\.0\.0
- 运行依赖：3 个
- 首次发布：2023-05-20T07:57:18.068Z
- 最新版本发布：2024-04-29T12:16:00.740Z
- 近 30 天下载量：2609（2026-10-16 至 2026-11-14，npm 官方下载统计）
- 已发布版本数：8（近 12 个月 0 个）

## 中文简介

Squoosh CLI 软件包的节点后端

## 安装

```sh
npm install @frostoven/libsquoosh
```

也可以使用：`pnpm add @frostoven/libsquoosh`、`yarn add @frostoven/libsquoosh`、`bun add @frostoven/libsquoosh`

## README

# 自由语

利布斯库什是一个 _实验_ 相关的 JavaScript 后端 [CLI 软件包](https://www.npmjs.com/package/@frostoven/squoosh-cli)。当你 在您自己的项目中可能使用此库, 请注意它不是 被认为已准备就绪供一般使用,没有立即计划 {\\fn黑体\\fs22\\bord1\\shad0\\3aHBE\\4aH00\\fscx67\\fscy66\\2cHFFFFFF\\3cH808080}这样就能做到 如果您使用这个包,您会冒着风险这样做.

libSquoosh使用工人池来平行地处理图像. 这边请 能够同时对许多图像应用相同的编码器.

libSquoosh目前不是镇上最快的图像压缩工具 并不打算成为。 但是,它足够快 压缩许多图像 足够快的一次.

请注意,这是叉子 [原始图书馆](https://www.npmjs.com/package/@squoosh/lib),已经 从退休以来 这个叉子继续工作,并有一些错误修正.

## 安装

libSquoosh 可以安装到您的本地工程, 命令如下:

```
$ npm install @frostoven/libsquoosh
```

你可以开始使用libSquoosh,在你的JS计划中添加这些行:

```js
import { ImagePool } from '@frostoven/libsquoosh';
import { cpus } from 'os';
const imagePool = new ImagePool(cpus().length);
```

这将创建一个图像池,其中包含一个基本的处理管道,可以用来摄取并编码图像. ImagePool 构造器采用一个参数,定义它允许在任何特定时间运行多少个平行操作. 默认情况下,这个数字被设定为它运行的系统中可用的CPU核心的数量.

## 插入图像

你可以摄取这样的新形象:

```js
import fs from 'fs/promises';
const file = await fs.readFile('./path/to/image.png');
const image = imagePool.ingestImage(imagePath);
```

这个 `ingestImage` 函数可以接受任意 [矩阵缓冲](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) 是否来自 `readFile()` 或者说 `fetch()`。 。 。 .

回来的 `image` 对象是原始图像的表达式,您现在可以对图像进行预处理、编码和提取信息.

## 预处理和编码图像

当一个图像被摄入后,可以开始预处理并编码到其他格式. 此示例将修改图像大小并编码为 `.jpg` 财务报告和审定财务报表 `.jxl` 图像 :

```js
await image.decoded; //Wait until the image is decoded before running preprocessors.

const preprocessOptions = {
  //When both width and height are specified, the image resized to specified size.
  resize: {
    enabled: true,
    width: 100,
    height: 50,
  },
  /*
  //When either width or height is specified, the image resized to specified size keeping aspect ratio.
  resize: {
    enabled: true,
    width: 100,
  }
  */
};
await image.preprocess(preprocessOptions);

const encodeOptions = {
  mozjpeg: {}, //an empty object means 'use default settings'
  jxl: {
    quality: 90,
  },
};
await image.encode(encodeOptions);
```

每个选项的默认值可见于 [`codecs.ts`](https://github.com/GoogleChromeLabs/squoosh/blob/dev/libsquoosh/src/codecs.ts) 文件编号 `defaultEncoderOptions`。每个未指定值将使用指定的默认值. _这里需要更好的文件._

您可以在不同的步骤之间运行自己的代码, 例如, 如果您想改变图像的大小, 以它最初的高度为基础 。 (见 [正在提取图像信息](https://npmcn.com/package/%40frostoven/libsquoosh/#readme-extracting-image-information) 学习如何获得图像维度).

## 关闭图像Pool

当您已经编码了您需要的一切时, 建议关闭图像Pool中的处理管道 。 这将不会删除您已经编码的图像, 但是它会防止您摄取和编码新图像 .

关闭此线的 ImagePool 管道 :

```js
await imagePool.close();
```

## 将编码的图像写入文件系统

当您已经编码了图像时, 您通常想要将其写入文件 .

此示例以编码为 `jpg` 并将其写入文件 :

```js
const rawEncodedImage = (await image.encodedWith.mozjpeg).binary;

fs.writeFile('/path/to/new/image.jpg', rawEncodedImage);
```

此示例通过所有编码的图像版本, 将其写入特定路径 :

```js
const newImagePath = '/path/to/image.'; //extension is added automatically

for (const encodedImage of Object.values(image.encodedWith)) {
  fs.writeFile(
    newImagePath + (await encodedImage).extension,
    (await encodedImage).binary,
  );
}
```

## 正在提取图像信息

有关已解码图像的信息,请访问 `Image.decoded`看起来像这样

```js
console.log(await image.decoded);
// Returns:
{
 bitmap: {
    data: Uint8ClampedArray(47736584) [
      225, 228, 237, 255, 225, 228, 237, 255, 225, 228, 237, 255,
      225, 228, 237, 255, 225, 228, 237, 255, 225, 228, 237, 255,
      225, 228, 237, 255,
      ... //the entire raw image
    ],
    width: 4606,  //pixels
    height: 2591  //pixels
  },
  size: 2467795  //bytes
}
```

关于被编码的图像的信息可见于 `Image.encodedWith[encoderName]`看起来像这样

```js
console.log(await image.encodedWith.jxl);
// Returns:
{
  optionsUsed: {
    quality: 75,
    baseline: false,
    arithmetic: false,
    progressive: true,
    ... //all the possible options for this encoder
  },
  binary: Uint8Array(1266975) [
      1,   0,   0,   1,   0,  1,  0,  0, 255, 219,  0, 132,
    113, 119, 156, 156, 209,  1,  8,  8,   8,   8,  9,   8,
      9,  10,  10,   9,
    ... //the entire raw encoded image
  ],
  extension: 'jxl',
  size: 1266975  //bytes
}
```

## 自动优化器

利布斯库什有一个 _实验_ 尽可能压缩图像的自动优化器,试图击中特定 [黄油](https://github.com/google/butteraugli) 目标值。 巴特劳格利的目标值越高,可以引进的文物就越多.

通过使用“自动”作为配置对象,您可以使用自动优化器.

```js
const encodeOptions: {
  mozjpeg: 'auto',
}
```

## 常见问题

### @frostoven/libsquoosh 是什么？

Squoosh CLI 软件包的节点后端

### 如何安装 @frostoven/libsquoosh？

在项目目录运行 npm install @frostoven/libsquoosh，或使用 pnpm add / yarn add / bun add。

### @frostoven/libsquoosh 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/frostoven\_\_libsquoosh 是否存在。

### @frostoven/libsquoosh 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @frostoven/libsquoosh 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=16\.0\.0。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @frostoven/libsquoosh 可以商用吗？

@frostoven/libsquoosh 使用 Apache-2\.0 许可。宽松许可：允许商业使用、修改和再分发，并附带明确的专利授权；分发时需保留许可与 NOTICE 文件，修改过的文件要注明改动。以上为概括，不构成法律意见，请以许可原文为准。

### @frostoven/libsquoosh 的下载量有多少？

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

### @frostoven/libsquoosh 还在维护吗？

最新版本 0\.4\.8 发布于 2024/04/29（2 年前）；至今共发布 8 个版本，其中过去 12 个月发布了 0 个；首次发布于 2023/05/20。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

