compress image-type file in frontend using canvas
npm install frontend-image-compressA simple function using canvas to compress image file.
html
`
$3
- 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
$3
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\ | compressed fileChangelog
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`