# frontend-image-compress

> compress image-type file in frontend using canvas

Latest version **3.0.2** (published 2020-04-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install frontend-image-compress
pnpm add frontend-image-compress
yarn add frontend-image-compress
bun add frontend-image-compress
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.2 |
| Published | 2020-04-26 |
| First published | 2018-10-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | xhmm |
| Maintainers | xhmm |
| Keywords | image-compress, canvas, file-compress, frontend-compress |

## Links

- npm: https://www.npmjs.com/package/frontend-image-compress
- Repository: https://github.com/XHMM/frontend-image-compress
- Homepage: https://github.com/XHMM/frontend-image-compress#readme
- Issues: https://github.com/XHMM/frontend-image-compress/issues
- npm.io page: https://npm.io/package/frontend-image-compress

## 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

- 3.0.2 (latest) — 2020-04-26
- 3.0.1 — 2019-08-05
- 3.0.0 — 2019-03-16
- 2.0.1 — 2018-12-06
- 2.0.0 — 2018-10-13
- 1.1.0 — 2018-10-13
- 1.0.1 — 2018-10-11
- 1.0.0 — 2018-10-11

## README

A simple function using canvas to compress image file.


## Usage
### using cdn
```html
<input  type='file' id='test'/>
<script src="https://unpkg.com/frontend-image-compress@latest/dist/index.min.js"></script>
<script>
  const $file = document.querySelector('#test');
  $file.onchange = async function(e) {
    const file = e.target.files[0];
    window.Compress.compressImageFile(file).then(compressedFile=>{}).catch(err=>{
      alert(err.toString())
    })
  }
</script>
```
### using npm or yarn
- yarn: `yarn add frontend-image-compress`
- npm: `npm i frontend-image-compress`
```js
import { compressImageFile } from 'frontend-image-compress'

const $file = document.querySelector('#test');
$file.onchange = async function(e) {
  const file = e.target.files[0];
  const compressedFile = await compressImageFile(file)
}
```

## API
when using script tag, you need to use `window.Compress.xx`, `Compress` is just a namespace
### compressImageFile(file, quality):newFile
name | type  | description
---|---| ---
file | File | from input[type="file"]
quality | number |  range: 0~1, default is 0.7(decrease this value if you got console warning)
return | Promise\<File\> | compressed file

## Changelog
1. 2020-4-26 `3.0.1`：
    - \[breaking] filename changed when using cdn (from `frontend-image-compress.min.js` to `index.min.js`) 
      
1. 2019-03-16 `3.0.0`：
  - \[breaking] add namespace to methods 
  - support `commonjs`

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