All Projects → wuwhs → js-image-compressor

wuwhs / js-image-compressor

Licence: other
A simple image compressor of javascript

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to js-image-compressor

Compressorjs
JavaScript image compressor.
Stars: ✭ 3,500 (+3400%)
Mutual labels:  image-compression, image-compressor
Biscuit
一款Android 便捷高效图片压缩库,更多自定义,灵活配置,缩放部分逆向微信朋友圈压缩效果推算得来,效果非常接近!
Stars: ✭ 395 (+295%)
Mutual labels:  picture, image-compression
image-optimizer
A free and open source tool for optimizing images and vector graphics.
Stars: ✭ 740 (+640%)
Mutual labels:  image-compression, image-compressor
visage
Add virtual makeup to picture of a face.
Stars: ✭ 97 (-3%)
Mutual labels:  picture
elm-playground-3d
A simple way to create three-dimensional pictures, animations, and games.
Stars: ✭ 25 (-75%)
Mutual labels:  picture
qoix
Elixir implementation of the Quite OK Image format
Stars: ✭ 18 (-82%)
Mutual labels:  image-compression
Apple-App-Icons
This repository is for hosting Apple App Icons Sketch file, .sketch
Stars: ✭ 31 (-69%)
Mutual labels:  picture
wp-github-gos
利用 github api 实现的一个存储图片/附件的 wordpress 插件
Stars: ✭ 42 (-58%)
Mutual labels:  picture
docker-imgproxy
🌐 An ultra fast, production-grade on-the-fly image processing web server. Designed for high throughput with Nginx caching. Powered by imgproxy.
Stars: ✭ 45 (-55%)
Mutual labels:  image-compression
image-compressor
Frontend javascript module for resizing and compressing images
Stars: ✭ 41 (-59%)
Mutual labels:  image-compressor
api
docs.nekos.moe/
Stars: ✭ 31 (-69%)
Mutual labels:  image-compression
imagezero
Fast Lossless Color Image Compression Library
Stars: ✭ 49 (-51%)
Mutual labels:  image-compression
autoencoder based image compression
Autoencoder based image compression: can the learning be quantization independent? https://arxiv.org/abs/1802.09371
Stars: ✭ 21 (-79%)
Mutual labels:  image-compression
Cat-Face-Detector-with-OpenCV-and-JavaFX
📹 A Small OpenCV (Open Source Computer Vision) Example, who has the ability to detect multiple cat faces at the same time 🐱
Stars: ✭ 24 (-76%)
Mutual labels:  picture
wbp4j
Simple Java Api for 微博图床,使用简单的api即可完成上传图片
Stars: ✭ 48 (-52%)
Mutual labels:  picture
Python36
These are python sample projects which are written in python
Stars: ✭ 28 (-72%)
Mutual labels:  picture
weiboUploader-Watermark
新浪微博图床批量传图工具 上传-缩放-水印-生成链接一键搞定
Stars: ✭ 41 (-59%)
Mutual labels:  picture
LocalVideoImage-selector
A simple-used local video and image selector for Android, also support single-selection and multi-selection.
Stars: ✭ 25 (-75%)
Mutual labels:  picture
ImageOnMap
Repo for ImageOnMap, a bukkit plugin created to display any image using a map
Stars: ✭ 162 (+62%)
Mutual labels:  picture
xice7-imageKit
基于java语言实现的简单的图片处理
Stars: ✭ 23 (-77%)
Mutual labels:  image-compression

Introduction

js-image-compressor is a javascript library that implements lightweight image compression. After compression, it is only 5kb, and the image can be compressed on the front-end page. While providing basic image compression functions, it also exposes related public methods of image processing, as well as border case processing:

  • A certain threshold can be set for the size of the converted image, so that the image converted to png format will not be too large under undesirable conditions, and at the same time larger than this threshold, it can be automatically converted to jpeg format for better compression;
  • You can limit the width and height of the output image to prevent accidents, such as excessive compression operations that cause the browser to crash;
  • By default, a transparent background color is added to the output image of png, and other formats are set to white to avoid "black screen";
  • Read the EXIF information of jpeg format pictures, and correct the picture orientation;
  • Provide some common tool functions for image processing (image2Canvas, canvas2Blob and canvas2DataUrl, etc.), and users can also customize the style features of image output (for example, grayscale processing, watermarking).

Document language:

Use

Installation and introduction

You can install dependencies through npm:

npm install js-image-compressor --save-dev

You can also find the file image-compress.min.js in the dist directory after downloading and import it through script on the page:

<script src="../dist/image-compressor.js"></script>

Simple to use

You can only pass in the image object to be compressed, other parameters are optional, and the plug-in automatically completes the image compression processing according to the default parameters. However, the compressed image output in this way meets the following characteristics:

  • The default configuration is based on 0.8 compression ratio;
  • Output picture width/height maintains the source picture width/height;
  • Generally, the output image format keeps the original image format;
  • When the size of the png image is greater than 2m, it will be converted into a jpeg format image by default;
  • Fill the png picture with a transparent color;
  • When the output picture size is larger than the source picture, the source picture will be returned as the output picture;
  • jpeg format picture, correct the flip/rotation direction;

If these default configurations cannot meet your needs, other parameter configurations may be required. The following is a simple configuration:

var options = {
  file: file,

  // Callback before compression
  beforeCompress: function (result) {
    console.log('Image size before compression:', result.size);
    console.log('mime type:', result.type);
  },

  // Compression success callback
  success: function (result) {
    console.log('result:', result)
    console.log('Image size after compression:', result.size);
    console.log('mime type:', result.type);
    console.log('Actual compression ratio:', ((file.size-result.size) / file.size * 100).toFixed(2) +'%');
  }
};

new ImageCompressor(options);

Among them, the hook function beforeCompress occurs after the image is read and before the canvas is created; the hook function success function occurs after the compression is completed to generate the image. Their callback parameter result is a blob object that integrates relevant information such as size, picture type and size.

Standard use

In standard use, we can customize the compression ratio (quality), output image type (mimeType), width (width), height (height), and maximum width (maxWidth) according to our own needs. ), maximum height (maxHeight), minimum width (minWidth), maximum height (minHeight), png to jpeg threshold (convertSize), whether to correct the jpeg direction (redressOrientation) and whether the loose mode ( loose).

  • Whether to correct the jpeg orientation (redressOrientation), the jpeg format image will be presented according to its orientation in some iOS browsers, this option can control the restoration of the initial orientation, the default is true;
  • Whether it is loose mode (loose), which means to control when the compressed image size is larger than the source image, output the source image, otherwise output the compressed image, the default is true.

The following is the standard configuration:

var options = {
  file: file,
  quality: 0.6,
  mimeType:'image/jpeg',
  maxWidth: 2000,
  maxHeight: 2000,
  width: 1000,
  height: 1000,
  minWidth: 500,
  minHeight: 500,
  convertSize: Infinity,
  loose: true,
  redressOrientation: true,

  // Callback before compression
  beforeCompress: function (result) {
    console.log('Image size before compression:', result.size);
    console.log('mime type:', result.type);
  },

  // Compression success callback
  success: function (result) {
    console.log('Image size after compression:', result.size);
    console.log('mime type:', result.type);
    console.log('Actual compression ratio:', ((file.size-result.size) / file.size * 100).toFixed(2) +'%');
  },

  // An error occurred
  error: function (msg) {
    console.error(msg);
  }
};

new ImageCompressor(options);

The error hook function is an error callback during the image compression process. Without this callback error, it will be thrown in the form of throw new Error(msg) in the plugin.

Other hook functions

Before compressing the output image, we can also customize the canvas to incorporate elements.

The following is the grayscale and watermark processing of the picture:

var options = {
  file: file,

  // Before picture painting
  beforeDraw: function (ctx) {
    vm.btnText ='Ready to draw...';
    console.log('Ready to draw...');
    ctx.filter ='grayscale(100%)';
  },

  // After the picture is painted
  afterDraw: function (ctx, canvas) {
    ctx.restore();
    vm.btnText ='Drawing completed...';
    console.log('Drawing completed...');
    ctx.fillStyle ='#fff';
    ctx.font = (canvas.width * 0.1) +'px microsoft yahei';
    ctx.fillText(vm.watermarkText, 10, canvas.height-20);
  },
};

new ImageCompressor(options);

beforeDraw is the hook function before the picture is drawn after the canvas is created, and afterDraw is the hook function after the picture is drawn.

Tool functions

The following figure summarizes the detailed process of the js-image-compressor plug-in from uploading user pictures locally through the file of input to image compression, and at the same time exposes these tools and methods for users to use.

js-image-compressor

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].