# kuma-imagemin

> 一个整合了mozjpeg和pngquant的图片压缩工具

Latest version **0.5.2** (published 2021-03-29) · GPL v3 license · 0 weekly downloads

## Install

```sh
npm install kuma-imagemin
pnpm add kuma-imagemin
yarn add kuma-imagemin
bun add kuma-imagemin
```

Provides the command `kuma-imagemin`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.2 |
| Published | 2021-03-29 |
| First published | 2021-03-21 |
| Weekly downloads | 0 |
| License | GPL v3 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 14.15.0 |
| Dependencies | 15 |
| Unpacked size | 4.2 MB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| Author | xiek |
| Maintainers | xiek881028 |
| Keywords | kuma, imagemin |

## Links

- npm: https://www.npmjs.com/package/kuma-imagemin
- npm.io page: https://npm.io/package/kuma-imagemin

## Dependencies (15)

- [md5](https://npm.io/package/md5.md) ^2.3.0
- [chalk](https://npm.io/package/chalk.md) ^4.1.0
- [leven](https://npm.io/package/leven.md) ^3.1.0
- [slash](https://npm.io/package/slash.md) ^3.0.0
- [figlet](https://npm.io/package/figlet.md) ^1.5.0
- [semver](https://npm.io/package/semver.md) ^7.3.5
- [pkg-dir](https://npm.io/package/pkg-dir.md) ^5.0.0
- [fs-extra](https://npm.io/package/fs-extra.md) ^9.1.0
- [commander](https://npm.io/package/commander.md) ^7.2.0
- [env-paths](https://npm.io/package/env-paths.md) ^2.2.1
- [file-type](https://npm.io/package/file-type.md) ^16.3.0
- [image-size](https://npm.io/package/image-size.md) ^0.9.7
- [kuma-helpers](https://npm.io/package/kuma-helpers.md) ^1.0.0
- [pretty-bytes](https://npm.io/package/pretty-bytes.md) ^5.6.0
- [update-notifier](https://npm.io/package/update-notifier.md) ^5.1.0

## Alternatives

- [exif-parser](https://npm.io/package/exif-parser.md) — 3.8M weekly downloads
- [vite-plugin-compression](https://npm.io/package/vite-plugin-compression.md) — 569.5K weekly downloads
- [pica](https://npm.io/package/pica.md) — 442.4K weekly downloads
- [@reportportal/client-javascript](https://npm.io/package/@reportportal/client-javascript.md) — 408.8K weekly downloads
- [@tldraw/state](https://npm.io/package/@tldraw/state.md) — 316.0K weekly downloads

## Recent versions

- 0.5.2 (latest) — 2021-03-29
- 0.5.1 — 2021-03-25
- 0.5.0 — 2021-03-25
- 0.4.6 — 2021-03-24
- 0.4.5 — 2021-03-24
- 0.4.4 — 2021-03-24
- 0.4.3 — 2021-03-24
- 0.4.2 — 2021-03-23
- 0.4.1 — 2021-03-23
- 0.4.0 — 2021-03-23
- 0.3.1 — 2021-03-22
- 0.2.1 — 2021-03-22
- 0.2.0 — 2021-03-21

## README

# kuma-imagemin

[![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)

一个基于`mozjpeg`和`pngquant`压缩算法进行图片压缩的小工具。它通过将两个压缩算法的代码由`C`编译成`Javascript`，可以在`浏览器`与`Node`环境下运行，同时降低了依赖安装的难度。

> jpeg 压缩方法基于[mozjpeg-js](https://github.com/as-com/mozjpeg-js)
>
> png 压缩方法基于[pngquantjs](https://github.com/psych0der/pngquantjs)

## 安装

```bash
npm install kuma-imagemin
```

## 使用

### 命令行调用

```bash
# 压缩文件
npx kuma-imagemin min <文件（夹）路径>

# 查看帮助
# 更多操作请自行看帮助信息
npx kuma-imagemin -h

```

### 方法调用

```javascript
const fs = require('fs-extra');
const {
  minPng,
  minJpg,
  minDir,
  clearOrigin,
  clearLog,
  log,
  isMin,
  resetByOrigin,
} = require('kuma-imagemin');

// 压缩png
const pngRes = minPng(fs.readFileSync('test.png'), {
  quality: '60-80',
});
// 压缩后的png数据，格式为 Uint8Array
console.log(pngRes.data);
// 压缩率
console.log(pngRes.ratio);

// 压缩jpg
const jpgRes = minJpg(fs.readFileSync('test.png'), {
  quality: 0.6,
});
// 压缩后的jpg数据，格式为 Uint8Array
console.log(jpgRes.data);
// 压缩率
console.log(jpgRes.ratio);

// 压缩指定文件夹
minDir('img', {
  // 是否生成备份源文件
  backup: true,
  // 是否强制压缩文件
  force: false,
});

// 删除指定文件夹内的备份源文件
clearOrigin('img');

// 删除插件压缩日志（记录所有被压缩过文件的md5，删除后所有文件都当成未被压缩）
clearLog();

// 添加压缩日志记录
log({
  'img/test.png': '38d034676322c1c6350190ccfc3eca58' // md5
});

// 判断一个文件是否已经被压缩（依赖日志记录）
isMin('img/test.png');

// 用备份源文件还原压缩后的文件（需要在压缩时保留备份源文件）
resetByOrigin('img');
```

## License

由于`mozjpeg-js`使用`BSD 3`协议，`pngquantjs`使用`GPL v3`协议。故`kuma-imagemin`只能使用`GPL v3`协议。

---
_Source: https://npm.io/package/kuma-imagemin · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
