Skip to content

@digitalascetic/ngx-pica is an Angular(LTS) module to resize images files in browse

Notifications You must be signed in to change notification settings

digitalascetic/ngx-pica

Folders and files

NameName
Last commit message
Last commit date

Latest commit

515b960 · Dec 16, 2024
Dec 16, 2024
Mar 9, 2018
Nov 8, 2022
Mar 9, 2018
Dec 16, 2024
Sep 13, 2024
Sep 17, 2024
Sep 13, 2024
Jul 6, 2020

Repository files navigation

@digitalascetic/ngx-pica

@digitalascetic/ngx-pica is an Angular (LTS) module to resize images files in browser using pica - high quality image resize in browser.

latest

Important

@digitalascetic/ngx-pica Angular 5 compatibility is under version 1.1.8

$ npm install @digitalascetic/[email protected] --save

Install

  1. Add ngx-pica module as dependency to your project.
$ npm install @digitalascetic/ngx-pica --save
  1. Include NgxPicaModule into your main AppModule or in module where you will use it.
// app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { NgxPicaModule } from '@digitalascetic/ngx-pica';

@NgModule({
  imports: [
    BrowserModule,
    NgxPicaModule
  ],
  declarations: [ AppComponent ],
  exports: [ AppComponent ]
})
export class AppModule {}

Services

NgxPicaService Methods

.resizeImages(files: File[], width: number, height: number, useWebWorker?: boolean): Observable<File>

This method resize an array of images doing it sequentially to optimize CPU and memory use.

  • files:[] - Array of images to resize
  • width - Width to be resized (px)
  • height - Height to be resized (px)
  • useWebWorker - optional, use multi-thread web worker, fallback to run in main-thread (default: true)

The Observable receives a next on every file that has been resized. If something goes wrong the Observable receive an error.

All errors are wrapped by NgxPicaErrorInterface.

.resizeImage(file: File, width: number, height: number, useWebWorker: boolean = true): Observable<File>

Same as above but only takes one file instead of an array of files.

.compressImages(files: File[], sizeInMB: number, useWebWorker?: boolean): Observable<File>

This method compress an array of images doing it sequentially to optimize CPU and memory use.

  • files:[] - Array of images to resize
  • sizeInMB - File size in MegaBytes
  • useWebWorker - optional, use multi-thread web worker, fallback to run in main-thread (default: true)

The Observable receives a next on every file that has been resized. If something goes wrong the Observable receive an error.

All errors are wrapped by NgxPicaErrorInterface.

.compressImage(file: File, sizeInMB: number, useWebWorker: boolean = true): Observable<File>

Same as above but only takes one file instead of an array of files.

NgxPicaImageService Methods

.isImage(file: File): boolean

This method check if a file is an image or not

Data Structures

export enum NgxPicaErrorType {
    NO_FILES_RECEIVED = 'NO_FILES_RECEIVED',
    CANVAS_CONTEXT_IDENTIFIER_NOT_SUPPORTED = 'CANVAS_CONTEXT_IDENTIFIER_NOT_SUPPORTED',
    NOT_BE_ABLE_TO_COMPRESS_ENOUGH = 'NOT_BE_ABLE_TO_COMPRESS_ENOUGH'
}

export interface NgxPicaErrorInterface {
    err: NgxPicaErrorType;
    file?: File;
}

Example

import { Component, EventEmitter } from '@angular/core';
import { NgxPicaService } from '@digitalascetic/ngx-pica';

@Component({
  selector: 'app-home',
  template: `
      <img *ngFor="let image of images" [src]="image" />
  
      <input type="file" [attr.accept]="image/*" multiple
             (change)="handleFiles($event)">
  `
})
export class AppHomeComponent {
    images: File[] = [];
    
    constructor(private _ngxPicaService: NgxPicaService) {
    
    }
    
    public handleFiles(event: any) {
        const files: File[] = event.target.files;
        
        this._ngxPicaService.resizeImages(files, 1200, 880)
            .subscribe((imageResized: File) => {
                let reader: FileReader = new FileReader();
                
                reader.addEventListener('load', (event: any) => {
                    this.images.push(event.target.result);
                }, false);
                
                reader.readAsDataURL(imageResized);
                
            }, (err: NgxPicaErrorInterface) => {
                throw err.err;
            });
    }

About

@digitalascetic/ngx-pica is an Angular(LTS) module to resize images files in browse

Resources

Stars

Watchers

Forks

Packages

No packages published