fengyuanchen /compressorjs

JavaScript image compressor.

★ 5,784⑂ 455👁 36⑃ 24● 176▤ 14
JavaScriptMITCreated 2017-07-25fengyuanchen.github.io/compressorjs/
下载:v1.4.0v1.4.0 ↓1.4.02026-10-01
+0
日增长
5,784
stars
455
forks
36
watchers
24
prs
176
issues
14
releases

README

Compressor.js

   

 JavaScript image compressor. Uses the browser's native HTMLCanvasElement.toBlob() method to do the compression work, which means it is lossy compression, asynchronous, and has different compression effects in different browsers. Generally use this to precompress an image on the client side before uploading it.

- Website

 Table of Contents

- Main Files
- Getting Started
- Options
- Methods
- Static Methods
- Browser Support
- Contributing
- Versioning
- License

 Main Files

text
dist/
├── compressor.js        (UMD)
├── compressor.min.js    (UMD, compressed)
├── compressor.common.js (CommonJS, default)
└── compressor.esm.js    (ES Module)


 Getting Started

 Install

shell
npm install compressorjs


 Usage

 Syntax

js
new Compressor(file[, options])


- file
  - Type: File or Blob
  - The target image file to compress.

- options (optional)
  - Type: Object
  - The configuration options. Check out the available options.

Alternatively, you may use Compressor.create(file[, options]).

 Example

html



js
import Compressor from 'compressorjs';

document.getElementById('file').addEventListener('change', (e) = {
  const file = e.target.files[0];

  if (!file) {
    return;
  }

  new Compressor(file, {
    quality: 0.6,

    // The compression process is asynchronous,
    // which means you need to access the result inside the success hook.
    success(result) {
      const formData = new FormData();

      // The third parameter is required by the server.
      formData.append('file', result, result.name || 'compressed-image.jpg');

      fetch('/path/to/upload', {
        method: 'POST',
        body: formData,
      }).then(() = {
        console.log('Upload success');
      }).catch((error) = {
        console.error('Upload failed', error);
      });
    },
    error(err) {
      console.log(err.message);
    },
  });
});


⬆ back to top

 Tips

- Use quality between 0.6 and 0.8 for a good balance between file size and visual quality.
- Avoid quality: 1 unless you need the image to stay as close as possible to the original; it can increase the file size.
- For very large images, set maxWidth and maxHeight to limit the canvas size and avoid browser memory issues.
- convertTypes and convertSize are useful for automatically converting large PNG files to JPEG to reduce file size.

 Options

You may set compressor options with new Compressor(file, options).
If you want to change the global default options, you may use Compressor.setDefaults(options).

 strict

- Type: boolean
- Default: true

Indicates whether to output the original image instead of the compressed one when the size of the compressed image is greater than the original one's, except the following cases:

- The retainExif option is set to true.
- The mimeType option is set and its value is different from the mime type of the image.
- The width option is set and its value is greater than the natural width of the image.
- The height option is set and its value is 

Top issues

Latest release

v1.4.0 v1.4.0 2026-10-01
See CHANGELOG.md for details.

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON