compressorjs is a standalone JavaScript library designed for compressing and converting images directly in the browser using the Canvas API. It provides a flexible API for resizing and optimizing image files before they are uploaded or stored. @uppy/compressor, on the other hand, is a plugin for the Uppy file uploader framework that integrates image compression capabilities into the upload pipeline. It leverages compression logic (often powered by compressorjs internally) to automatically process files before they are sent to the server, specifically tailored for workflows already using Uppy.
Reducing image file size before upload is a critical optimization for modern web applications. It saves bandwidth, speeds up transfers, and reduces server storage costs. Two common solutions in the JavaScript ecosystem are compressorjs and @uppy/compressor. While they share the same goal, they serve different architectural roles. Let's explore how they differ in practice.
compressorjs is a standalone library.
fetch, XMLHttpRequest, or any custom logic.// compressorjs: Standalone usage
import { Compressor } from 'compressorjs';
const file = input.files[0];
new Compressor(file, {
quality: 0.6,
success(result) {
// result is a Blob, ready to upload manually
const formData = new FormData();
formData.append('file', result);
fetch('/upload', { method: 'POST', body: formData });
},
});
@uppy/compressor is a plugin for the Uppy uploader.
@uppy/core to function.// @uppy/compressor: Plugin usage
import Uppy from '@uppy/core';
import Compressor from '@uppy/compressor';
const uppy = new Uppy()
.use(Compressor, {
quality: 0.6,
// Automatically runs before upload
});
// Uppy handles the upload after compression
uppy.upload();
compressorjs exposes detailed options for image manipulation.
width, height, resize, and mimeType directly.success, error) handle the flow explicitly.// compressorjs: Detailed configuration
new Compressor(file, {
width: 800,
height: 600,
resize: 'cover',
mimeType: 'image/webp',
success(result) {
console.log('Compressed to WebP:', result.size);
},
error(err) {
console.error('Compression failed:', err.message);
},
});
@uppy/compressor simplifies configuration for upload flows.
// @uppy/compressor: Simplified configuration
uppy.use(Compressor, {
quality: 0.6,
mimeType: 'image/webp',
// Uppy handles errors via its own logger/events
});
uppy.on('upload-success', (file, response) => {
console.log('Upload complete after compression');
});
compressorjs returns a new Blob object.
// compressorjs: Manual file handling
new Compressor(file, {
success(result) {
// You must create a new File or Blob to upload
const compressedFile = new File([result], file.name, {
type: result.type,
lastModified: Date.now(),
});
// Proceed with your own upload logic
},
});
@uppy/compressor replaces the file in Uppy's state.
// @uppy/compressor: Automatic file replacement
// No need to manually swap files.
// The plugin intercepts the file before the request.
uppy.use(Compressor, {
quality: 0.6,
});
// When uppy.upload() is called, it sends the compressed file
compressorjs requires explicit error callbacks.
// compressorjs: Explicit error handling
new Compressor(file, {
error(err) {
// Decide whether to upload original or show error
alert('Compression failed, uploading original.');
uploadOriginal(file);
},
});
@uppy/compressor integrates with Uppy's error system.
upload-error or plugin events.// @uppy/compressor: Integrated error handling
uppy.on('upload-error', (file, error) => {
// Handles both network and compression errors uniformly
console.error('Upload or compression failed:', error);
});
These tools are specific to image optimization. Consider alternatives when:
| Feature | compressorjs | @uppy/compressor |
|---|---|---|
| Type | π οΈ Standalone Library | π Uppy Plugin |
| Dependency | β None | β Requires @uppy/core |
| File Output | π Returns Blob (Manual handling) | π Auto-replaces in Uppy state |
| Configuration | βοΈ Detailed API options | βοΈ Plugin options |
| Error Handling | β οΈ Callback-based | π’ Uppy Event System |
| Best For | π Custom upload logic | π§© Uppy-based upload flows |
Think in terms of your upload architecture:
compressorjs. It is lightweight, flexible, and does not force an upload framework on you.@uppy/compressor. It saves development time by integrating directly into the existing pipeline, ensuring files are optimized without extra wiring.Final Thought: Both tools solve the same problem but at different layers of the stack. compressorjs is the engine; @uppy/compressor is the integration of that engine into a specific vehicle. Choose based on whether you need the engine alone or the complete vehicle.
Choose @uppy/compressor if your project already uses the Uppy file uploader framework. It integrates compression directly into the upload pipeline, handling file replacement automatically before the request is sent, which reduces boilerplate code for Uppy users. This is the most efficient path if you want seamless compression without manually wiring up file processing events.
Choose compressorjs if you need a standalone image compression utility that works independently of any file uploader. It is ideal for custom upload implementations, local file processing before storage, or when you want to avoid tying your compression logic to a specific upload framework. This gives you full control over when and how compression happens without importing a heavy uploader library.
The Compressor plugin for Uppy optimizes images (JPEG, PNG, WEBP), saving on average up to 60% in size (roughly 18 MB for 10 images). It uses Compressor.js.
Uppy is being developed by the folks at Transloadit, a versatile file encoding service.
import Uppy from '@uppy/core'
import Compressor from '@uppy/compressor'
const uppy = new Uppy()
uppy.use(Compressor)
npm install @uppy/compressor
We recommend installing from yarn or npm, and then using a module bundler such as Parcel, Vite or Webpack.
Alternatively, you can also use this plugin in a pre-built bundle from
Transloaditβs CDN: Smart CDN. In that case Uppy will attach itself to the
global window.Uppy object. See the
main Uppy documentation for instructions.
Documentation for this plugin can be found on the Uppy website.