Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

browser-image-compression

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

browser-image-compression

Compress images in the browser

  • 2.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
234K
increased by7.48%
Maintainers
1
Weekly downloads
 
Created

What is browser-image-compression?

The browser-image-compression npm package is a JavaScript library that allows you to compress images directly in the browser. It is useful for reducing the file size of images before uploading them to a server, which can save bandwidth and improve performance.

What are browser-image-compression's main functionalities?

Compress Image

This feature allows you to compress an image file to a specified size and dimension. The code sample demonstrates how to use the package to compress an image file with a maximum size of 1MB and a maximum width or height of 1920 pixels.

const imageCompression = require('browser-image-compression');

async function compressImage(file) {
  const options = {
    maxSizeMB: 1,
    maxWidthOrHeight: 1920,
    useWebWorker: true
  };
  try {
    const compressedFile = await imageCompression(file, options);
    console.log('Compressed file:', compressedFile);
  } catch (error) {
    console.error('Error compressing image:', error);
  }
}

Get Image File from URL

This feature allows you to fetch an image from a URL and convert it into a File object. The code sample demonstrates how to fetch an image from a URL and create a File object from the fetched Blob.

const imageCompression = require('browser-image-compression');

async function getImageFileFromUrl(url) {
  try {
    const response = await fetch(url);
    const blob = await response.blob();
    const file = new File([blob], 'image.jpg', { type: blob.type });
    console.log('Image file:', file);
  } catch (error) {
    console.error('Error fetching image:', error);
  }
}

Draw Image on Canvas

This feature allows you to draw an image on a canvas element. The code sample demonstrates how to read an image file, create an Image object, and draw it on a canvas element.

const imageCompression = require('browser-image-compression');

function drawImageOnCanvas(file) {
  const img = new Image();
  const reader = new FileReader();

  reader.onload = function (e) {
    img.src = e.target.result;
    img.onload = function () {
      const canvas = document.createElement('canvas');
      const ctx = canvas.getContext('2d');
      canvas.width = img.width;
      canvas.height = img.height;
      ctx.drawImage(img, 0, 0);
      document.body.appendChild(canvas);
    };
  };

  reader.readAsDataURL(file);
}

Other packages similar to browser-image-compression

Keywords

FAQs

Package last updated on 06 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc