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

ngx-image-compress

v18.1.5

Published

Angular utility for compressing images to a satisfying size, that you can choose

Downloads

95,560

Readme

ngx-image-compress

Angular utility for compressing images to a satisfying size, that you can choose

License Latest npm release NPM downloads Contributors

Build Status

Import

npm i ngx-image-compress
  • For visualizing code examples: https://stackblitz.com/edit/ngx-image-compress
  • For performance tests, in particular on your mobile, please do not use stackbliz, put this production-ready application: https://image-library.app
  • Compatible with any Angular version > 9
  • If you use Angular version < 9, you can use npm install ngx-image-compress@view-engine

Angular 13+ differ as there is no need to import the service in your module. You can inject the service in the constructor of your component directly.
For any angular version before 13, you should first import the service in your module, like this:

import {NgxImageCompressService} from 'ngx-image-compress';

@NgModule({
    declarations: [AppComponent],
    imports: [BrowserModule],
    providers: [NgxImageCompressService],
    bootstrap: [AppComponent],
})
export class AppModule {}

Usage

Here how to use the service in your component.

Using upload and compress function, independently

This option is giving control over the compression process.

compressFile() signature is detailed here

import {Component} from '@angular/core';
import {NgxImageCompressService} from 'ngx-image-compress';

@Component({
    selector: 'app-root',
    template: `
        <button (click)="compressFile()">Upload and compress Image</button>
        <img [src]="imgResultBeforeCompression" *ngIf="imgResultBeforeCompression" />
        <img [src]="imgResultAfterCompression" *ngIf="imgResultAfterCompression" />
    `,
})
export class AppComponent {
    constructor(private imageCompress: NgxImageCompressService) {}

    imgResultBeforeCompression: string = '';
    imgResultAfterCompression: string = '';

    compressFile() {
        this.imageCompress.uploadFile().then(({image, orientation}) => {
            this.imgResultBeforeCompression = image;
            console.log('Size in bytes of the uploaded image was:', this.imageCompress.byteCount(image));

            this.imageCompress
                .compressFile(image, orientation, 50, 50) // 50% ratio, 50% quality
                .then(compressedImage => {
                    this.imgResultAfterCompression = compressedImage;
                    console.log('Size in bytes after compression is now:', this.imageCompress.byteCount(compressedImage));
                });
        });
    }
}

Performing a single upload, and compressing automatically to a given max size

Quicker and effortless method.

Getting directly an image at a maximum of "X" MegaBytes, using a built-in algorithm:

import {Component} from '@angular/core';
import {NgxImageCompressService} from 'ngx-image-compress';

@Component({
    selector: 'app-root',
    template: `
        <button (click)="compressFile()">Upload and compress Image</button>
        <img [src]="imgResult" *ngIf="imgResult" />
    `,
})
export class AppComponent {
    constructor(private imageCompress: NgxImageCompressService) {}

    imgResult: string = '';

    compressFile() {
        const MAX_MEGABYTE = 2;
        this.imageCompress
            .uploadAndGetImageWithMaxSize(MAX_MEGABYTE) // this function can provide debug information using (MAX_MEGABYTE,true) parameters
            .then(
                (result: string) => {
                    this.imgResult = result;
                },
                (result: string) => {
                    console.error(
                        "The compression algorithm didn't succed! The best size we can do is",
                        this.imageCompress.byteCount(result),
                        'bytes'
                    );
                    this.imgResult = result;
                }
            );
    }
}

Same method but without the upload step

this.imageCompress.getImageWithMaxSizeAndMetas({image: 'base64ValueFromYourUpload'},MAX_MEGABYTE).then

Multiple files support

For uploading multiple files, instead of using

this.imageCompress.uploadFile()
  .then((singleFile: { image: string, fileName:string, orientation: number }) => //...

You can use

this.imageCompress.uploadMultipleFiles()
  .then((arrayOfFiles: { image: string, fileName:string, orientation: number }[]) => //...

Behavior if no files have been selected

With uploadFile() and uploadMultipleFiles(), nothing will happen when the user is selecting nothing, close the file selection, and cancel the upload.

If you want the upload promise to reject in such case, please use: uploadFileOrReject() or uploadMultipleFilesOrReject() instead.

compressFile() signature

The signature of compressFile() is:

compressFile(image, orientation, ratio, quality, maxWidth, maxHeight)

| Parameter | Type | Description | | ----------- | ------ | --------------------------------------------------------------------------------- | | image | string | DataUrl (string) representing the image | | orientation | number | EXIF Orientation value using the DOC_ORIENTATION enum value | | ratio | number | Maximum scale factor as a percentage (optional, default: 50) 1 | | quality | number | JPEG quality factor as a percentage (optional, default: 50) 2 | | maxWidth | number | Maximum width in pixels if you need to resize (optional, default: 0 - no resize) | | maxHeight | number | Maximum height in pixels if you need to resize (optional, default: 0 - no resize) |

[1] Ratio: "50" will decrease the resolution of each dimension by 2, i.e.: image of 2000 X 1500 pixels will become 1000 X 750 pixels, while the whole resolution will be reduced by 4.

[2] Quality: For more info about this parameter, read this guide

How it works under the hood?

We will use Renderer2, and transform the image using HTML canvas encrustation. In fact you can use the static version in the library and import renderer by yourself, or replace it with another DOM abstraction, using RendererFactory2.

There are mainly two advantage for using Renderer2 abstraction over direct DOM manipulation (by using ElementRef or window.document directly).

  • Angular keeps the component and the view in sync using templates, data binding, and change detection. All of them are bypassed when we update the DOM Directly.
  • DOM Manipulation works only in a browser. In the future we will not be able to use other platforms like web worker, in-server (for Server-Side Rendering), in a mobile or a desktop application, etc... where there is no browser.
  • The DOM APIs do not sanitize the data. Hence, it is possible to inject a script, thereby, opening our app to XSS injection attacks.

That's being said, please note that because of some iOS limitations/bugs when using Renderer2, we still are using window.document API, for the upload part only (not the canvas itself).

Change log

2023/02/14

  • Update transpilation builder to angular 15 (Every angular version from 9+ are still supported, for version bellow 9 please check https://www.npmjs.com/package/ngx-image-compress-legacy)
  • iOS version 13+ devices are now well recognized, and the upload method using native DOM api is used in accordance to it. This should help a lot of Ionic developers.

2022/05/24

  • Every angular version from 9+ are now supported.
  • No need to update the library after each angular release, by using now semantic versionning.
  • Every version before 9 are dropped because angular 13 only compile library ivy-compatible.
  • Anyway, if you really need to use this library in you old angular app we created here a npm transpiled version in compatibily mode with View Engine: https://www.npmjs.com/package/ngx-image-compress-legacy

2022/05/10

  • Adding new API to reject promise if the user close the upload windows and no files are selected (uploadFileOrReject and uploadMultipleFileOrReject)
    New functions avoid any breaking change in existing code, no changes are necessary, you can still use uploadFile or uploadMultiple. With these, the promise stays silent when the user cancel the upload selection.
  • Adding the file name in the upload result

2022/02/22

  • Fix Exif rotation for new version of Chrome 79+
  • Native upload for Safari browser

2022/01/19

  • Implementing a built-in algorithm, looping several times, to reach a desired max size in Megabytes
  • Readme updates and docs in method signature directly

2022/01/04

  • Readme update
  • CI/CD with github action

2021/12/21

  • Update to Angular 13
  • Upload multiple file at once
  • Add support for resizing image size (compressFile() is now accepting maxWidth and maxHeight paramaters)
  • Cleanup types
  • Invalid image rejection
  • General refactoring

2021/11/14

  • Added support for max size

2020/11/18

  • Update to Angular 11
  • Fix upload for iOS
  • Expose getOrientation api publically

2019/07/01

  • Update to Angular 8 (angular 7 is enough)
  • Fix DOC_ORIENTATION import (not a required import)

2019/01/09

  • Since Angular 6 include its own packaging system, I no longer need my webpack config to build it.
  • Everything is working in angular 7 without complaint now (test app is on github)

2018/10/04

  • Adding Live example.
  • Everything is now working and tested but I will make some arrangement to the code in index.ts before submitting it again to npm, in order to make it more handy.

2017/12/06

  • Upload to Github
  • Need some fixes and tests to be use as a static library