GithubHelp home page GithubHelp logo

alextanhongpin / compress.js Goto Github PK

View Code? Open in Web Editor NEW
139.0 4.0 32.0 9.76 MB

A simple JavaScript based client-side image compression algorithm

License: MIT License

JavaScript 83.34% HTML 16.66%
compress base-conversion compression javascript javascript-client canvas fastest base64url base64 image

compress.js's Introduction

compress.js

A JavaScript client side image compression. This library uses the Canvas API to compress the image, and thus will not work on the node.js server-side.

Support me

Maintaining open source is not easy. Would be great if you could show some support!

"Buy Me A Coffee"

Advantage:

  • quick compression on the client-side
  • save data by compressing it on the client-side before sending to the server
  • mantains the aspect ratio of the images when resizing
  • fix image rotation issue when uploading images from Android an iOS
  • supports cropping the image
  • New: supports cropping to desired aspect ratio, e.g. 1:1, 4:3 etc

NOTE:

There are several limitations for this library:

  • When working with image/gif, the compressed image will no longer animate.
  • When working with image/png with transparent background, the compressed image will lose transparency and result in black background.

Installation

NPM Package here.

npm install compress.js --save

CDN

Using jsDelivr CDN:

Important

The script tag must include type="module" to work.

<!-- index.js is your file that needs to execute compress.js-->
<script src="./index.js" type="module"></script>

And in your index.js, you can import the desired version:

// index.js
"use strict";

import Compress from "https://cdn.jsdelivr.net/npm/[email protected]/build/compress.min.js";

const compressor = new Compress();

Import

const Compress = require('compress.js')

You can also include the build/compress.min.js in your project directory, and then importing it using type module in the script tag.

<!-- index.html -->
<script type="module" src="index.js"></script>
// index.js
import Compress from "./compress.min.js";

// ...

Demo

Try out our demo here.

Usage

import Compress from "./compress.min.js";

const compressor = new Compress();

// Listen to file upload events.
upload.addEventListener(
  "change",
  async function (evt) {
	const file = evt.target.files[0];
    const newFile = await compressor.compress(file, {
      quality: 0.95,
      crop: true, // If true, will crop a square image from the center.
      maxWidth: 320, // Image width will not exceed 320px.
      maxHeight: 320, // Image height will not exceed 320px.
    });

    // Display the image on the img element.
    img.src = URL.createObjectURL(newFile);
  },

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.