@digitalascetic/ngx-pica

中文文档
20.1.3发布于 2024/12/16MITTypeScriptES Module近 30 天下载 1.2万

Angular (LTS) module to resize images files in browser

原始使用文档
文档目录10 节
  1. Important
  2. Install
  3. Services
  4. NgxPicaService Methods
  5. .resizeImage(file: File, width: number, height: number, opt…
  6. .compressImages(files: File[], sizeInMB: number): Observabl…
  7. .compressImage(file: File, sizeInMB: number): Observable<Fi…
  8. NgxPicaImageService Methods
  9. Data Structures
  10. Example

@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 pica exifr @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, options?: NgxPicaResizeOptionsInterface): 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)
  • options - Based on pica - resize options, we've also added aspect ratio options:
    • keepAspectRatio - Set true to ensure the aspect ratio of the image is maintained as it get resized
    • forceMinDimensions - Set true to ensure the dimensions of image resized will be greater than width and height values defined

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, options?: NgxPicaResizeOptionsInterface): Observable<File>

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

.compressImages(files: File[], sizeInMB: number): 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

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): 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 interface AspectRatioOptions {
    keepAspectRatio: boolean;
    forceDimensions?: boolean;
}
export interface NgxPicaResizeOptionsInterface {
    aspectRatio?: AspectRatioOptions;
    quality?: number;
    alpha?: boolean;
    unsharpAmount?: number;
    unsharpRadius?: number;
    unsharpThreshold?: number;
}
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;
            });
    }

版本与发布记录

@digitalascetic/ngx-pica 共发布 35 个版本,首次发布于 2018/03/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
20.1.3latest2024/12/161.9 年前
20.1.22024/09/172 年前
20.1.12024/09/172 年前
20.1.02024/09/172 年前
20.0.12024/09/172 年前
20.0.02024/09/162 年前

关于 @digitalascetic/ngx-pica 的常见问题

@digitalascetic/ngx-pica 是什么?

Angular (LTS) module to resize images files in browser

如何安装 @digitalascetic/ngx-pica?

在项目目录运行 npm install @digitalascetic/ngx-pica,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@angular/common(>=18.2.0)、@angular/core(>=18.2.0)、browser-image-compression(^2.0.2)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@digitalascetic/ngx-pica 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@digitalascetic/ngx-pica 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@digitalascetic/ngx-pica 可以商用吗?

@digitalascetic/ngx-pica 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@digitalascetic/ngx-pica 的下载量有多少?

根据 npm 官方下载统计,@digitalascetic/ngx-pica 在 2026/10/12 – 2026/11/10 这 30 天内被下载 11,918 次,日均约 397 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@digitalascetic/ngx-pica 还在维护吗?

最新版本 20.1.3 发布于 2024/12/16(1.9 年前);至今共发布 35 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/03/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。