@frostoven/libsquoosh

中文文档
0.4.8发布于 2024/04/29Apache-2.0CommonJS近 30 天下载 2609

Squoosh CLI 软件包的节点后端

中文使用文档
文档目录7 节
  1. 安装
  2. 插入图像
  3. 预处理和编码图像
  4. 关闭图像Pool
  5. 将编码的图像写入文件系统
  6. 正在提取图像信息
  7. 自动优化器

自由语

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

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

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

请注意,这是叉子 原始图书馆,已经 从退休以来 这个叉子继续工作,并有一些错误修正.

安装

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

$ npm install @frostoven/libsquoosh

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

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

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

插入图像

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

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

这个 ingestImage 函数可以接受任意 矩阵缓冲 是否来自 readFile() 或者说 fetch()。 。 。 .

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

预处理和编码图像

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

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 文件编号 defaultEncoderOptions。每个未指定值将使用指定的默认值. 这里需要更好的文件.

您可以在不同的步骤之间运行自己的代码, 例如, 如果您想改变图像的大小, 以它最初的高度为基础 。 (见 正在提取图像信息 学习如何获得图像维度).

关闭图像Pool

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

关闭此线的 ImagePool 管道 :

await imagePool.close();

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

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

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

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

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

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

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看起来像这样

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]看起来像这样

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
}

自动优化器

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

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

const encodeOptions: {
  mozjpeg: 'auto',
}

版本与发布记录

@frostoven/libsquoosh 共发布 8 个版本,首次发布于 2023/05/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.4.8latest2024/04/292 年前
0.4.72024/01/092 年前
0.4.62023/09/273 年前
0.4.52023/09/263 年前
0.4.42023/09/263 年前
0.4.32023/09/263 年前

关于 @frostoven/libsquoosh 的常见问题

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