1.0.4 • Published 3 years ago

@hec9527/image-compress v1.0.4

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

image-compress

image-compress is a tool to compress image by HTMLCanvasElement and HTML5 Api

Usage

online usage

npm

import compress from 'image-compress';
const el = document.getElementById('file') as HTMLInputElement;

el.addEventListener('change', e => {
    const file: File = (e.target as any).files[0];
    if (!file) return;
    compress(file, { width: 1024 }).then(blob => {
        // upload blob or show blob localhost
    });
});

umd

<script src="path/to/index.umd.js"></script>
const el = document.getElementById('file');

el.addEventListener('change', e => {
    const file: File = e.target.files[0];
    if (!file) return;
    compressImage(file, { width: 1024 }).then(blob => {
        // upload blob or show blob localhost
    });
});

Arguments

compressImage function should receive two arguments

compressImage(file, option);

file

file is required, which should be HTMLImageElement or File object or string to load remote resource.

option

option is optional

attributetypedescription
qualitynumberthe output image quality, min 0, max 1, default is 0.9
widthnumberthe output image width, default is input image width. if height is set, but not width, it should be automatic calculation by origin image aspect ratio
heightnumberthe output image height, default is input image height. if width is set, but not height, is should be automatic calculation by origin image aspect ratio
mimestringthe output image extension, default is jpeg
1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago