Socket
Socket
Sign inDemoInstall

@awesome-cordova-library/compressor-image

Package Overview
Dependencies
0
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @awesome-cordova-library/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.


Version published
Weekly downloads
2
Maintainers
1
Install size
23.3 kB
Created
Weekly downloads
 

Readme

Source

id: compressor-image title: Compressor Image tags:

  • cordova
  • capacitor
  • ionic
  • javascript
  • typescript
  • plugin
  • mobile
  • image
  • compressor

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 @awesome-cordova-library/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<jazoulay@joazco.com>
 *
 * 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

ParameterTypeDescription
srcstringSource of image or base 64 (need to set outputFormat value)
targetLengthnumberTarget length in MB to which the image should be approximated.
qualitynumberDefault: 0.8 Quality given as Number for the quality of the new image beetween 0 and 1
outputFormatnumberOptional This option is useful if your src is a base64 to determine the type of file to compress.
minQualitynumberDefault: 0.2. Compression halts if this quality is reached while trying to approach the target length.
maxWidthnumberResizes the image for optimal compression if image width exceeds this value.
maxHeightnumberResizes 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 "@awesome-cordova-library/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 "@awesome-cordova-library/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 "@awesome-cordova-library/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 "@awesome-cordova-library/compressor-image/lib/react";
...

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 "@awesome-cordova-library/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 "@awesome-cordova-library/compressor-image/lib/react";
...

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>)
}

Keywords

FAQs

Last updated on 11 Sep 2023

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc