npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

canvas-compressor-image

v1.0.0

Published

An efficient JavaScript library that utilizes canvas to compress images by adjusting their quality or size. Ideal for optimizing images for better performance and faster loading times.

Downloads

5

Readme

Compressor Image

An efficient JavaScript library that utilizes canvas to compress images by adjusting their quality or size. Ideal for optimizing images for better performance and faster loading times.

Online documentation

Online demo

Instruction

This plugin is especially effective for large images that haven't been previously compressed by another software. To optimize compression further, consider resizing the image beforehand.

Installation

npm install canvas-compressor-image

Vanilla

Declaration

export type FileExtension = 'jpg' | 'jpeg' | 'png' | 'webp' | 'gif' | 'bmp' | 'heic';
export type OutputFormat = 'image/jpeg' | 'image/png' | 'image/webp' | 'image/gif' | 'image/bmp' | 'image/heic';
export type CompressOptions = {
  src: string;
  targetLength: number;
  quality?: number;
  outputFormat?: OutputFormat;
  minQuality?: number;
  maxWidth?: number;
  maxHeight?: number;
};
export type CompressorImageReturn = {
  base64: string;
  outputFormat?: OutputFormat;
  width: number;
  height: number;
  quality: number;
  length: number;
};
/**
 * @author AZOULAY Jordan<[email protected]>
 *
 * compressor-image is a lightweight and efficient Node.js module designed to compress images using the power of the HTML Canvas API. Whether you're dealing with JPEG, PNG, or other image formats, compressor-image aims to reduce the file size while maintaining good visual quality. Ideal for web applications, content management systems, or any scenario where bandwidth and storage optimizations are crucial.
 */
export default class CompressorImage {
  /**
   * Determines the optimal image format based on a given file extension.
   * @params extension {FileExtension}
   */
  static determineOutputFormat(extension: FileExtension): OutputFormat;
  /**
   * Converts the size of a base64 string to megabytes (Mo).
   * @params base64 {string}
   */
  static sizeBase64ToMo(base64: string): number;
  /**
   * Compresses and optimizes an image.
   * @params options {Pick<CompressOptions, 'src' | 'quality' | 'maxWidth' | 'maxHeight'>}
   */
  static compress(
    options: Pick<CompressOptions, 'src' | 'quality' | 'maxWidth' | 'maxHeight' | 'outputFormat'>,
  ): Promise<CompressorImageReturn>;
  /**
   * Compresses the image to approximate a desired file size in MB.
   * @params options {Pick<CompressOptions, 'src' | 'minQuality' | 'targetLength' | 'maxWidth' | 'maxHeight'>}
   */
  static compressTargetLength(
    options: Pick<CompressOptions, 'src' | 'minQuality' | 'targetLength' | 'maxWidth' | 'maxHeight' | 'outputFormat'>,
  ): Promise<CompressorImageReturn>;
}

React

Declaration

declare const useCompressorImage: () => {
  compress: (
    options: Pick<CompressOptions, 'src' | 'quality' | 'maxWidth' | 'maxHeight' | 'outputFormat'>,
  ) => Promise<import('../').CompressorImageReturn>;
  compressTargetLength: (
    options: Pick<CompressOptions, 'src' | 'minQuality' | 'targetLength' | 'maxWidth' | 'maxHeight' | 'outputFormat'>,
  ) => Promise<import('../').CompressorImageReturn>;
  determineOutputFormat: (extension: FileExtension) => import('../').OutputFormat;
  sizeBase64ToMo: (base64: string) => number;
};
export default useCompressorImage;

CompressOptions

| Parameter | Type | Description | | :------------- | :------- | :--------------------------------------------------------------------------------------------------------- | | src | string | Source of image or base 64 (need to set outputFormat value) | | targetLength | number | Target length in MB to which the image should be approximated. | | quality | number | Default: 0.8 Quality given as Number for the quality of the new image beetween 0 and 1 | | outputFormat | number | Optional This option is useful if your src is a base64 to determine the type of file to compress. | | minQuality | number | Default: 0.2. Compression halts if this quality is reached while trying to approach the target length. | | maxWidth | number | Resizes the image for optimal compression if image width exceeds this value. | | maxHeight | number | Resizes the image for optimal compression if image height exceeds this value. |

Usage

Compress by quality

I wish to compress an image with a quality of 60%.

import CompressorImage from "canvas-compressor-image";
...

CompressorImage.compress({
  src: "https://mydomain.com/my-image.jpg",
  quality: 0.6,
  maxWidth: 1920,
  maxHeight: 1080
}).then((result) => {
  img.src = `data:${result.outputFormat || 'application/octet-stream'};base64,${result.base64}`;
})

OR REACT

import React, {useState} from "react";
import useCompressorImage from "canvas-compressor-image/lib/react";
...

function App(){
  const [src, setSrc] = useState("");
  const {compress} = useCompressorImage();
  return  (<div>
    <img src={src} />
    <button onClick={() => {
      compress({
        src: "https://mydomain.com/my-image.jpg",
        quality: 0.6,
        maxWidth: 1920,
        maxHeight: 1080
      }).then((base64) => {
      setSrc(`data:${result.outputFormat || 'application/octet-stream'};base64,${result.base64}`)
      })
    }}>Load src compress</button>
  </div>)
}

Compresses to the maximum desired image length

I have a 5MB image and I would like it to come as close as possible to 2MB without going below 60% quality.

import CompressorImage from "canvas-compressor-image";
...

CompressorImage.compressTargetLength({
  src: "https://mydomain.com/my-image.jpg",
  targetLength: 2,
  minQuality: 0.6,
  maxWidth: 1920,
  maxHeight: 1080
}).then((base64) => {
   img.src = `data:${result.outputFormat || 'application/octet-stream'};base64,${result.base64}`;
})

OR REACT

import React, {useState} from "react";
import useCompressorImage from "canvas-compressor-image";
...

function App(){
  const [src, setSrc] = useState("");
  const {compressTargetLength} = useCompressorImage();
  return  (<div>
    <img src={src} />
    <button onClick={() => {
      compressTargetLength({
        src: "https://mydomain.com/my-image.jpg",
        targetLength: 2,
        minQuality: 0.6,
        maxWidth: 1920,
        maxHeight: 1080
      }).then((result) => {
        setSrc(result.base64Formatted);
      })
    }}>Load src compress</button>
  </div>)
}

Compresses a base64 value

import CompressorImage from "canvas-compressor-image";
...

CompressorImage.compress({
  src: "/9j/4AAQSkZJRgABAQAASABIAAD/4QBMRX........",
  quality: 0.6,
  outputFormat: "image/jpeg",
  maxWidth: 1920,
  maxHeight: 1080
}).then((base64) => {
   img.src = `data:${result.outputFormat || 'application/octet-stream'};base64,${result.base64}`;
})

OR REACT

import React, {useState} from "react";
import useCompressorImage from "canvas-compressor-image";
...

function App(){
  const [src, setSrc] = useState("");
  const {compress} = useCompressorImage();
  return  (<div>
    <img src={src} />
    <button onClick={() => {
      compress({
        src: "/9j/4AAQSkZJRgABAQAASABIAAD/4QBMRX........",
        quality: 0.6,
        outputFormat: "image/jpeg",
        maxWidth: 1920,
        maxHeight: 1080
      }).then((result) => {
        setSrc(result.base64Formatted);
      })
    }}>Load src compress</button>
  </div>)
}