# @mxsir/image-tiny

> Compress Images

Latest version **0.1.0** (published 2022-08-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install @mxsir/image-tiny
pnpm add @mxsir/image-tiny
yarn add @mxsir/image-tiny
bun add @mxsir/image-tiny
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2022-08-18 |
| First published | 2022-05-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 1.6 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 22 |
| Author | mxismean |
| Maintainers | mxismean |

## Links

- npm: https://www.npmjs.com/package/@mxsir/image-tiny
- Repository: https://github.com/mxismean/image-tiny
- Homepage: https://github.com/mxismean/image-tiny#readme
- Issues: https://github.com/mxismean/image-tiny/issues
- npm.io page: https://npm.io/package/@mxsir/image-tiny

## Recent versions

- 0.1.0 (latest) — 2022-08-18
- 0.0.6 — 2022-08-09
- 0.0.5 — 2022-06-01
- 0.0.4 — 2022-05-31
- 0.0.3 — 2022-05-31
- 0.0.2 — 2022-05-31
- 0.0.1 — 2022-05-31

## README

# NPM 插件：图片压缩（浏览器端本地压缩）

## NPM 🔗

[@mxsir/image-tiny](https://www.npmjs.com/package/@mxsir/image-tiny)

## 概述

这是一款图片压缩插件，不依赖网络、服务器，实现的是客户端压缩；

压缩率近似 TinyPng；

支持格式：png、jpg、gif

## 安装

```bash
yarn add @mxsir/image-tiny
```

## 插件导出方法

```javascript
/**
 * @description: 图像压缩
 * @param {File} file 原始 File 文件对象
 * @param {Number} quality 压缩质量，10-90，建议 80
 * @return {Promise<File>} 压缩过的 File 文件对象
 */
imageTiny(file, quality = 80){...}
```

## 使用方法

```javascript
import imageTiny from '@mxsir/image-tiny';

// compressedFile 就是压缩过的图片文件
const compressedFile = imageTiny(file, quality = 80)

```

## ❗️注意

**引入项目特殊配置：**

由于插件里使用了 `wasm` 文件，这个文件不会自动打包到你的项目里，所以要特殊处理一下，你项目的构建工具需要把这个 `wasm` 文件 copy 到你的项目的静态文件目录里。


- vite 或 rollup 可以使用 `rollup-plugin-copy` 这个插件进行静态文件拷贝；

- webpack 可以用 `copy-webpack-plugin` 这个插件进行静态文件拷贝；


这样你的项目打包之后，打包产物里就会有这个 wasm 文件了，插件才可以正常运行。

vite 例子：

vite.config.ts

```javascript
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import copy from 'rollup-plugin-copy';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    copy({
      targets: [{ src: 'node_modules/@mxsir/image-tiny/dist/pngtiny-custom.wasm', dest: 'public' }],
      verbose: true,
      hook: 'writeBundle',
    }),
  ],
  build: {
    sourcemap: false,
  },
});

```

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