compressorjs vs @uppy/compressor
Image Compression Libraries Comparison
1 Year
compressorjs@uppy/compressor
What's Image Compression Libraries?

Image compression libraries are essential tools in web development that help reduce the size of image files without significantly compromising quality. This is crucial for optimizing web performance, improving load times, and enhancing user experience. By utilizing these libraries, developers can ensure that images are efficiently processed and delivered, which is particularly important for mobile users and those with slower internet connections. Both @uppy/compressor and compressorjs serve the purpose of compressing images, but they differ in their features, ease of use, and integration capabilities.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
compressorjs182,0455,417154 kB112 years agoMIT
@uppy/compressor22,33129,55634.4 kB1602 months agoMIT
Feature Comparison: compressorjs vs @uppy/compressor

Integration

  • compressorjs:

    compressorjs is a standalone library that can be integrated into any project without the need for additional dependencies. This makes it versatile and easy to use in various environments, from simple web pages to complex applications.

  • @uppy/compressor:

    @uppy/compressor is designed to work seamlessly with the Uppy ecosystem, making it easy to integrate into applications that already use Uppy for file uploads. It provides a cohesive experience with other Uppy plugins, enhancing the overall functionality of file handling.

Compression Quality

  • compressorjs:

    compressorjs offers a straightforward approach to image compression with options to specify the quality level. It uses advanced algorithms to ensure that images retain as much detail as possible while reducing file size, making it suitable for various use cases.

  • @uppy/compressor:

    @uppy/compressor provides configurable options for compression quality, allowing developers to balance between file size and image fidelity. This flexibility is beneficial for applications that require high-quality images while still optimizing performance.

Ease of Use

  • compressorjs:

    compressorjs is lightweight and easy to implement, requiring minimal setup. Its API is straightforward, allowing developers to quickly integrate image compression into their projects without extensive configuration.

  • @uppy/compressor:

    @uppy/compressor is user-friendly and designed with modern web development practices in mind. It offers a simple API and clear documentation, making it accessible for developers of all skill levels, especially those familiar with Uppy.

Performance

  • compressorjs:

    compressorjs is designed to be fast and efficient, providing quick image compression without blocking the main thread. This ensures that the user interface remains responsive while images are being processed.

  • @uppy/compressor:

    @uppy/compressor is optimized for performance, leveraging the capabilities of the Uppy framework to ensure efficient image processing. It is designed to handle large files and multiple uploads without significant delays, enhancing the user experience.

Customization

  • compressorjs:

    compressorjs offers basic customization features, allowing developers to set quality and max dimensions. While it may not be as feature-rich as Uppy, it provides sufficient options for most standard use cases.

  • @uppy/compressor:

    @uppy/compressor allows for extensive customization options, enabling developers to tailor the compression settings to fit their specific needs. This includes adjusting quality, dimensions, and more, providing flexibility in how images are handled.

How to Choose: compressorjs vs @uppy/compressor
  • compressorjs:

    Choose compressorjs if you need a lightweight, standalone solution for image compression that can be easily integrated into any project without dependencies. It is ideal for projects where you want to minimize bundle size and have full control over the compression process.

  • @uppy/compressor:

    Choose @uppy/compressor if you are already using the Uppy file uploader or need a modular solution that integrates well with other Uppy plugins. It offers a user-friendly interface and is designed for modern web applications that require a seamless file upload experience.

README for compressorjs

Compressor.js

Coverage Status Downloads Version Gzip Size

JavaScript image compressor. Uses the Browser's native canvas.toBlob API 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 a image on the client side before uploading it.

Table of contents

Main

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

Getting started

Install

npm install compressorjs

Usage

Syntax

new Compressor(file[, options])

file

The target image file for compressing.

options

  • Type: Object
  • Optional

The options for compressing. Check out the available options.

Example

<input type="file" id="file" accept="image/*">
import axios from 'axios';
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 have to access the `result` in the `success` hook function.
    success(result) {
      const formData = new FormData();

      // The third parameter is required for server
      formData.append('file', result, result.name);

      // Send the compressed image file to server with XMLHttpRequest.
      axios.post('/path/to/upload', formData).then(() => {
        console.log('Upload success');
      });
    },
    error(err) {
      console.log(err.message);
    },
  });

});

⬆ back to top

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 greater than the natural height of the image.
  • The minWidth option is set and its value is greater than the natural width of the image.
  • The minHeight option is set and its value is greater than the natural height of the image.
  • The maxWidth option is set and its value is less than the natural width of the image.
  • The maxHeight option is set and its value is less than the natural height of the image.

checkOrientation

  • Type: boolean
  • Default: true

Indicates whether to read the image's Exif Orientation value (JPEG image only), and then rotate or flip the image automatically with the value.

Notes:

  • Don't trust this all the time as some JPEG images have incorrect (not standard) Orientation values.
  • If the size of the target image is too large (e.g., greater than 10 MB), you should disable this option to avoid an out-of-memory crash.
  • The image's Exif information will be removed after compressed, so if you need the Exif information, you may need to upload the original image as well.

retainExif

  • Type: boolean
  • Default: false

Indicates whether to retain the image's Exif information after compressed.

maxWidth

  • Type: number
  • Default: Infinity

The max-width of the output image. The value should be greater than 0.

Avoid getting a blank output image, you might need to set the maxWidth and maxHeight options to limited numbers, because of the size limits of a canvas element, recommend to use 4096 or lesser.

maxHeight

  • Type: number
  • Default: Infinity

The max height of the output image. The value should be greater than 0.

minWidth

  • Type: number
  • Default: 0

The min-width of the output image. The value should be greater than 0 and should not be greater than the maxWidth.

minHeight

  • Type: number
  • Default: 0

The min-height of the output image. The value should be greater than 0 and should not be greater than the maxHeight.

width

  • Type: number
  • Default: undefined

The width of the output image. If not specified, the natural width of the original image will be used, or if the height option is set, the width will be computed automatically by the natural aspect ratio.

height

  • Type: number
  • Default: undefined

The height of the output image. If not specified, the natural height of the original image will be used, or if the width option is set, the height will be computed automatically by the natural aspect ratio.

resize

  • Type: string
  • Default: "none"
  • Options: "none", "contain", and "cover".

Sets how the size of the image should be resized to the container specified by the width and height options.

Note: This option only available when both the width and height options are specified.

quality

  • Type: number
  • Default: 0.8

The quality of the output image. It must be a number between 0 and 1. If this argument is anything else, the default values 0.92 and 0.80 are used for image/jpeg and image/webp respectively. Other arguments are ignored. Be careful to use 1 as it may make the size of the output image become larger.

Note: This option only available for image/jpeg and image/webp images.

Check out canvas.toBlob for more detail.

Examples:

| Quality | Input size | Output size | Compression ratio | Description | | --- | --- | --- | --- | --- | | 0 | 2.12 MB | 114.61 KB | 94.72% | - | | 0.2 | 2.12 MB | 349.57 KB | 83.90% | - | | 0.4 | 2.12 MB | 517.10 KB | 76.18% | - | | 0.6 | 2.12 MB | 694.99 KB | 67.99% | Recommend | | 0.8 | 2.12 MB | 1.14 MB | 46.41% | Recommend | | 1 | 2.12 MB | 2.12 MB | 0% | Not recommend | | NaN | 2.12 MB | 2.01 MB | 5.02% | - |

mimeType

  • Type: string
  • Default: 'auto'

The mime type of the output image. By default, the original mime type of the source image file will be used.

convertTypes

  • Type: Array or string (multiple types should be separated by commas)
  • Default: ['image/png']
  • Examples:
    • ['image/png', 'image/webp']
    • 'image/png,image/webp'

Files whose file type is included in this list, and whose file size exceeds the convertSize value will be converted to JPEGs.

convertSize

  • Type: number
  • Default: 5000000 (5 MB)

Files whose file type is included in the convertTypes list, and whose file size exceeds this value will be converted to JPEGs. To disable this, just set the value to Infinity.

Examples:

| convertSize | Input size (type) | Output size (type) | Compression ratio | | --- | --- | --- | --- | | 5 MB | 1.87 MB (PNG) | 1.87 MB (PNG) | 0% | | 5 MB | 5.66 MB (PNG) | 450.24 KB (JPEG) | 92.23% | | 5 MB | 9.74 MB (PNG) | 883.89 KB (JPEG) | 91.14% |

beforeDraw(context, canvas)

  • Type: Function
  • Default: null
  • Parameters:
    • context: The 2d rendering context of the canvas.
    • canvas: The canvas for compression.

The hook function to execute before drawing the image into the canvas for compression.

new Compressor(file, {
  beforeDraw(context, canvas) {
    context.fillStyle = '#fff';
    context.fillRect(0, 0, canvas.width, canvas.height);
    context.filter = 'grayscale(100%)';
  },
});

drew(context, canvas)

  • Type: Function
  • Default: null
  • Parameters:
    • context: The 2d rendering context of the canvas.
    • canvas: The canvas for compression.

The hook function to execute after drawing the image into the canvas for compression.

new Compressor(file, {
  drew(context, canvas) {
    context.fillStyle = '#fff';
    context.font = '2rem serif';
    context.fillText('watermark', 20, canvas.height - 20);
  },
});

success(result)

  • Type: Function
  • Default: null
  • Parameters:
    • result: The compressed image (a File (read only) or Blob object).

The hook function to execute when successful to compress the image.

error(err)

  • Type: Function
  • Default: null
  • Parameters:
    • err: The compression error (an Error object).

The hook function executes when fails to compress the image.

⬆ back to top

Methods

abort()

Abort the compression process.

const compressor = new Compressor(file);

// Do something...
compressor.abort();

No conflict

If you have to use another compressor with the same namespace, just call the Compressor.noConflict static method to revert to it.

<script src="other-compressor.js"></script>
<script src="compressor.js"></script>
<script>
  Compressor.noConflict();
  // Code that uses other `Compressor` can follow here.
</script>

Browser support

  • Chrome (latest)
  • Firefox (latest)
  • Safari (latest)
  • Opera (latest)
  • Edge (latest)
  • Internet Explorer 10+

Contributing

Please read through our contributing guidelines.

Versioning

Maintained under the Semantic Versioning guidelines.

License

MIT © Chen Fengyuan

⬆ back to top