compress-base64
Version:
compress base64
89 lines (68 loc) • 1.45 kB
Markdown
A library for compressing base64 in browser environment.
```bash
npm i compress-base64
```
or
```bash
yarn add compress-base64
```
or
```bash
pnpm add compress-base64
```
For example.
```js
import compress from 'compress-base64';
if (typeof FileReader === 'function') {
const reader = new FileReader();
reader.onload = (event) => {
compress(event.target.result, {
width: 400,
type: 'image/png',
max: 200, // max size
min: 20, // min size
quality: 0.8,
}).then((result) => {
console.log(result);
});
};
reader.readAsDataURL(file);
} else {
alert('Your browser does not support FileReader');
}
```
Introduce this resource.
```html
<script src="https://unpkg.com/compress-base64@latest"></script>
```
You can also download this resource locally and then import it.
```html
<script src="/path/compress-base64/umd/index.js"></script>
```
Use it.
```js
if (typeof FileReader === 'function') {
const reader = new FileReader();
reader.onload = (event) => {
window.CompressBase64.default(event.target.result, {
width: 100,
type: 'image/png',
max: 7, // max size
min: 5,
quality: 0.8,
}).then((result) => {
console.log(result.length);
resolve(result);
});
};
reader.readAsDataURL(file);
} else {
alert('Your browser does not support FileReader');
}
```