All Projects → xkeshi → Image Compressor

xkeshi / Image Compressor

Licence: mit
[Deprecated] No longer maintained, please use https://github.com/fengyuanchen/compressorjs

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Image Compressor

Imager
Automated image compression for efficiently distributing images on the web.
Stars: ✭ 266 (+59.28%)
Mutual labels:  image-processing, image-compression
Resizer
An image resizing library for Android
Stars: ✭ 406 (+143.11%)
Mutual labels:  image-processing, image-compression
Compressor
An android image compression library.
Stars: ✭ 6,745 (+3938.92%)
Mutual labels:  compressor, image-compression
Image Shrinker
App for macOS. Minify your images and graphics with just one drop. Autorenamed in the same place where it comes from. Immediately!
Stars: ✭ 217 (+29.94%)
Mutual labels:  image-compression, compressor
Image Actions
A Github Action that automatically compresses JPEGs, PNGs and WebPs in Pull Requests.
Stars: ✭ 844 (+405.39%)
Mutual labels:  image-processing, image-compression
Gaussianblur
An easy and fast library to apply gaussian blur filter on any images. 🎩
Stars: ✭ 473 (+183.23%)
Mutual labels:  image-processing, image-compression
Crunch
Crunch is a tool for lossy PNG image file optimization. It combines selective bit depth, color type, and color palette reduction with zopfli DEFLATE compression algorithm encoding using the pngquant and zopflipng PNG optimization tools. This approach leads to a significant file size gain relative to lossless approaches at the expense of a relatively modest decrease in image quality (see example images below).
Stars: ✭ 3,074 (+1740.72%)
Mutual labels:  image-processing, image-compression
Optimize Images
A command-line interface (CLI) utility written in pure Python to help you reduce the file size of images.
Stars: ✭ 141 (-15.57%)
Mutual labels:  image-processing, image-compression
Imgp
📸 High-performance cli batch image resizer and rotator
Stars: ✭ 744 (+345.51%)
Mutual labels:  image-processing, image-compression
Compressor
An easy to use and well designed image compress library for Android, based on Android native image library. Put forward a framework for quick switch from different compress algorithm.
Stars: ✭ 476 (+185.03%)
Mutual labels:  image-processing, compressor
Lerc
Limited Error Raster Compression
Stars: ✭ 126 (-24.55%)
Mutual labels:  image-processing, image-compression
Essential Image Optimization
Essential Image Optimization - an eBook
Stars: ✭ 1,950 (+1067.66%)
Mutual labels:  image-processing, image-compression
Degate
Open source software for chip reverse engineering.
Stars: ✭ 156 (-6.59%)
Mutual labels:  image-processing
Pngtastic
A pure Java PNG image optimization and manipulation library
Stars: ✭ 159 (-4.79%)
Mutual labels:  image-processing
Spectrum
A client-side image transcoding library.
Stars: ✭ 1,908 (+1042.51%)
Mutual labels:  image-processing
Mesh Flow Video Stabilization
Online video stabilization using a novel MeshFlow motion model
Stars: ✭ 154 (-7.78%)
Mutual labels:  image-processing
Bimef
Code and data for the research paper "A Bio-Inspired Multi-Exposure Fusion Framework for Low-light Image Enhancement" (Submitted to IEEE Transactions on Cybernetics)
Stars: ✭ 161 (-3.59%)
Mutual labels:  image-processing
Open Solution Data Science Bowl 2018
Open solution to the Data Science Bowl 2018
Stars: ✭ 159 (-4.79%)
Mutual labels:  image-processing
Uiimage Imagecompress
An iOS library to compress images and optimize uploads
Stars: ✭ 155 (-7.19%)
Mutual labels:  compressor
Similarimagehunter
Mac 上寻找相似内容的图片工具
Stars: ✭ 155 (-7.19%)
Mutual labels:  image-processing

image-compressor

Downloads Version

A simple JavaScript image compressor. Uses the Browser's native canvas.toBlob API to do the compression work. General use this to precompress a client image file before upload it.

Table of contents

Main

dist/
├── image-compressor.js        (UMD)
├── image-compressor.min.js    (UMD, compressed)
├── image-compressor.common.js (CommonJS, default)
└── image-compressor.esm.js    (ES Module)

Getting started

Install

npm install image-compressor.js

Usage

Syntax

new ImageCompressor([file[, options]])

file

The target image file for compressing.

options

  • Type: Object
  • Optional

The options for compressing. Check out the available options.

Example

<input type="file" id="file" accept="image/*">
import axios from 'axios';
import ImageCompressor from 'image-compressor.js';

document.getElementById('file').addEventListener('change', (e) => {
  const file = e.target.files[0];

  if (!file) {
    return;
  }

  new ImageCompressor(file, {
    quality: .6,
    success(result) {
      const formData = new FormData();

      formData.append('file', result, result.name);

      // Send the compressed image file to server with XMLHttpRequest.
      axios.post('/path/to/upload', formData).then(() => {
        console.log('Upload success');
      });
    },
    error(e) {
      console.log(e.message);
    },
  });
});

⬆ back to top

Options

checkOrientation

  • Type: boolean
  • Default: true

Indicates if read the image's Exif Orientation value (JPEG image only), and then rotate or flip the image automatically with the value.

Notes:

  • Don't trust this all the time as some JPEG images have incorrect (not standard) Orientation values.
  • If the size of target image is too large (e.g., greater than 10 MB), you should disable this option to avoid out-of-memory crash, see #40 for more information.

maxWidth

  • Type: number
  • Default: Infinity

The max width of the output image. The value should be greater then 0.

Avoid to get a blank output image, you might need to set the maxWidth and maxHeight options to limited numbers, because of the size limits of a canvas element.

maxHeight

  • Type: number
  • Default: Infinity

The max height of the output image. The value should be greater then 0.

minWidth

  • Type: number
  • Default: 0

The min width of the output image. The value should be greater then 0 and should not be greater than the maxWidth.

minHeight

  • Type: number
  • Default: 0

The min height of the output image. The value should be greater then 0 and should not be greater than the maxHeight.

width

  • Type: number
  • Default: undefined

The width of the output image. If not specified, the natural width of the original image will be used, or if the height option is set, the width will be computed automatically by the natural aspect ratio.

height

  • Type: number
  • Default: undefined

The height of the output image. If not specified, the natural height of the original image will be used, or if the width option is set, the height will be computed automatically by the natural aspect ratio.

Note: In order to keep the same aspect ratio to the original image, if the width option is set, will use it compute the height automatically, which means the height option will be ignored.

quality

  • Type: number
  • Default: 0.8

The quality of the output image. It must be a number between 0 and 1. Be careful to use 1 as it may make the size of the output image become larger. Check out canvas.toBlob for more detail.

Note: This option only available for image/jpeg and image/webp images.

Examples (in Chrome 61):

Quality Input size Output size Compression ratio Description
0 2.12 MB 114.61 KB 94.72% -
0.2 2.12 MB 349.57 KB 83.90% -
0.4 2.12 MB 517.10 KB 76.18% -
0.6 2.12 MB 694.99 KB 67.99% Recommend
0.8 2.12 MB 1.14 MB 46.41% Recommend
1 2.12 MB 2.12 MB 0% Not recommend
NaN 2.12 MB 2.01 MB 5.02% -

mimeType

  • Type: string
  • Default: 'auto'

The mime type of the output image. By default, the original mime type of the source image file will be used.

convertSize

  • Type: number
  • Default: 5000000 (5MB)

PNG files over this value will be converted to JPEGs. To disable this, just set the value to Infinity. See #2.

Examples (in Chrome 61):

convertSize Input size (type) Output size (type) Compression ratio
5 MB 1.87 MB (PNG) 1.87 MB (PNG) 0%
5 MB 5.66 MB (PNG) 450.24 KB (JPEG) 92.23%
5 MB 9.74 MB (PNG) 883.89 KB (JPEG) 91.14%

beforeDraw(context, canvas)

  • Type: Function
  • Default: null
  • Parameters:
    • context: The 2d rendering context of the canvas.
    • canvas: The canvas for compression.

The hook function to execute before draw the image into the canvas for compression.

new ImageCompressor(file, {
  beforeDraw(context) {
    context.fillStyle = '#fff';
  },
});

drew(context, canvas)

  • Type: Function
  • Default: null
  • Parameters:
    • context: The 2d rendering context of the canvas.
    • canvas: The canvas for compression.

The hook function to execute after drew the image into the canvas for compression.

new ImageCompressor(file, {
  drew(context) {
    context.filter = grayscale(100%);
  },
});

success(result)

  • Type: Function
  • Default: null
  • Parameters:
    • result: The compressed image (a Blob object).

The hook function to execute when success to compress the image.

error(err)

  • Type: Function
  • Default: null
  • Parameters:
    • err: The compression error (an Error object).

The hook function to execute when fail to compress the image.

⬆ back to top

Methods

compress(file[, options])

  • file:
    • Type: File or Blob
    • The target image file for compressing.
  • options (optional):
    • Type: Object
    • The options for compressing.
  • (return value):
    • Type: Promise

Compress an image file.

const imageCompressor = new ImageCompressor();

imageCompressor.compress(file, options)
  .then((result) => {
    // Handle the compressed image file.
  })
  .catch((err) => {
    // Handle the error
  })

⬆ back to top

Browser support

  • Chrome (latest)
  • Firefox (latest)
  • Safari (latest)
  • Opera (latest)
  • Edge (latest)
  • Internet Explorer 10+ (requires a Promise polyfill as es6-promise)

Versioning

Maintained under the Semantic Versioning guidelines.

License

MIT © Xkeshi

⬆ back to top

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].