图像转换器:JavaScript 库

图像转换:解码位图、HEIC 和 PDF 页面;裁剪、缩放、旋转、翻转、滤镜、达到目标文件大小;编码为 20 多种格式;将大量文件打包成 ZIP。

  • @openlibsdev/image-converter
  • v1.0.0
  • ES module
  • MIT

安装

使用打包工具或 Node 风格的导入:

npm install @openlibsdev/image-converter
import ImageConverter from "@openlibsdev/image-converter";

或直接在页面中使用,无需构建(jsDelivr 会解析依赖):

<script type="module">
import ImageConverter from "https://cdn.jsdelivr.net/npm/@openlibsdev/image-converter@1.0.0/+esm";
</script>

用法

import ImageConverter from "@openlibsdev/image-converter";

const file = document.querySelector("input[type=file]").files[0];
const [image] = await ImageConverter.decodeFile(file);   // one item per page
const blob = await ImageConverter.convert(image, { format: "webp", quality: 0.8, resize: true, maxW: 800, maxH: 800 });
const name = ImageConverter.outputName(file.name, "webp");

API

ImageConverter.decodeFile(file)

将文件解码为图像,每页一张:[{name, size, dataUrl, w, h}]。

ImageConverter.convert(image, settings)

按设置转换图像并返回 Blob。

ImageConverter.zip(items)

将文件打包为一个 ZIP Blob。

ImageConverter.canDecode(file)

文件能否打开。

ImageConverter.normalizeSettings(s)

补全缺失的设置。

ImageConverter.outputName(name, format)

带新格式扩展名的文件名。

ImageConverter.FORMATS

输出格式。

ImageConverter.FILTERS

滤镜。

ImageConverter.DEFAULT_SETTINGS

默认设置。

ImageConverter.setPdfWorkerUrl(url)

设置 pdf.js worker 的地址。

ImageConverter.APP

库的名称、版本和发布日期:{name, version, created}。

ImageConverter.DEFAULT_FORMAT

未指定或指定了未知格式时使用的输出格式(“png”)。

ImageConverter.normalizeFormat(fmt)

返回已知的输出格式:也接受旧的 MIME 名称(“image/jpeg”);未知的一律成为默认格式。

ImageConverter.projectData(settings)

作为待保存项目对象的设置:{app, version, settings}。

ImageConverter.readProject(data)

来自项目对象的设置;不是本转换器的项目时为 null。

依赖

库加载的内容及其许可证(全部列在“许可证”页面)。

名称版本许可证文件
@openlibsdev/font-forge1.0.0MITGitHub
@openlibsdev/image-forge1.0.0MITGitHub
@imagemagick/magick-wasm0.0.35Apache-2.0 许可证 · 源代码
@imagemagick/magick-wasm0.0.35Apache-2.0 许可证 · 源代码
imagetracerjs1.2.6Unlicense 许可证 · 源代码
jspdf2.5.2MIT 许可证 · 源代码
jszip3.10.1(MIT OR GPL-3.0-or-later) 许可证 · 源代码
pdfjs-dist3.11.174Apache-2.0 许可证 · 源代码
pdfjs-dist3.11.174Apache-2.0 许可证 · 源代码