Compressor.js is a client-side JavaScript image compression library that leverages the browser's native `HTMLCanvasElement.toBlob()` method for its core functionality. It performs lossy, asynchronous compression, meaning results can vary slightly across different web browsers and privacy settings. Primarily designed for pre-compressing images on the client before upload, it provides a straightforward API for handling `File` or `Blob` objects. The current stable version is 1.3.0, and the project appears actively maintained with a steady cadence of minor updates addressing bug fixes and introducing new options. Its key differentiator is its reliance on native browser APIs, simplifying its footprint but also making its compression effects browser-dependent. It ships with TypeScript types.
npm install compressorjsVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to select an image file from an input, compress it using Compressor.js with specific quality and dimension constraints, and then preview the compressed image or prepare it for upload.
Always test compression results across your target browsers. For applications requiring consistent compression across all clients or server-side processing, consider implementing server-side image optimization as a fallback or primary method.
Structure your code to ensure all operations dependent on the compressed image are executed inside the `success` callback provided to the Compressor constructor.
Inform users about this browser-specific privacy setting's impact. If compression is critical, provide alternative client-side strategies or prompt users to adjust their browser settings.
Review the `strict` option and its exceptions carefully. If you explicitly need a compressed output even if it's larger, set `strict: false`, but be aware of the potential for increased file sizes.
Upgrade to version 1.1.1 or later to resolve Node.js loading issues. For legacy Node.js environments, ensure proper Babel or transpilation configuration if you must use older versions of the library.
Ensure that images loaded from external domains are served with proper CORS headers (e.g., `Access-Control-Allow-Origin: *` or specific domains). For locally generated or user-provided files, this error is less common but can indicate browser security restrictions.
Ensure your JavaScript code executes after the DOM is ready. Place your script tag at the end of the `<body>` or wrap your initialization code in a `DOMContentLoaded` event listener (e.g., `document.addEventListener('DOMContentLoaded', () => { ... });`).Verify that the `file` argument passed to `new Compressor(file, options)` is a valid `File` or `Blob` object, and always instantiate the class with `new Compressor(...)`.
No dependency data recorded yet.