Gathering detailed insights and metrics for fd-ngx-image-compress
Gathering detailed insights and metrics for fd-ngx-image-compress
npm install fd-ngx-image-compress
Typescript
Module System
Node Version
NPM Version
60.8
Supply Chain
96.5
Quality
75.4
Maintenance
100
Vulnerability
98.9
License
TypeScript (85.75%)
HTML (12.07%)
JavaScript (1.3%)
SCSS (0.88%)
Total Downloads
1,747
Last Day
5
Last Week
26
Last Month
54
Last Year
859
87 Stars
284 Commits
36 Forks
3 Watching
5 Branches
11 Contributors
Minified
Minified + Gzipped
Latest Version
13.1.21
Package Id
fd-ngx-image-compress@13.1.21
Unpacked Size
191.34 kB
Size
39.48 kB
File Count
21
NPM Version
8.19.3
Node Version
16.19.0
Publised On
09 Feb 2023
Cumulative downloads
Total Downloads
Last day
-75%
5
Compared to previous day
Last week
30%
26
Compared to previous week
Last month
-72.2%
54
Compared to previous month
Last year
-3.3%
859
Compared to previous year
1
2
Angular utility for compressing images to a satisfying size, that you can choose
1npm i ngx-image-compress
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:
1import {NgxImageCompressService} from "ngx-image-compress"; 2 3@NgModule({ 4 declarations: [AppComponent], 5 imports: [BrowserModule], 6 providers: [NgxImageCompressService], 7 bootstrap: [AppComponent], 8}) 9export class AppModule { 10}
Here how to use the service in your component.
This option is giving control over the compression process.
compressFile() signature is detailed here
1import {Component} from "@angular/core"; 2import {NgxImageCompressService} from "ngx-image-compress"; 3 4@Component({ 5 selector: "app-root", 6 template: ` 7 <button (click)="compressFile()">Upload and compress Image</button> 8 <img [src]="imgResultBeforeCompression" *ngIf="imgResultBeforeCompression" /> 9 <img [src]="imgResultAfterCompression" *ngIf="imgResultAfterCompression" /> 10 `, 11}) 12export class AppComponent { 13 14 constructor(private imageCompress: NgxImageCompressService) { 15 } 16 17 imgResultBeforeCompression: string = ""; 18 imgResultAfterCompression: string = ""; 19 20 compressFile() { 21 this.imageCompress.uploadFile().then( 22 ({image, orientation}) => { 23 24 this.imgResultBeforeCompression = image; 25 console.log("Size in bytes of the uploaded image was:", this.imageCompress.byteCount(image)); 26 27 this.imageCompress 28 .compressFile(image, orientation, 50, 50) // 50% ratio, 50% quality 29 .then( 30 (compressedImage) => { 31 this.imgResultAfterCompression = compressedImage; 32 console.log("Size in bytes after compression is now:", this.imageCompress.byteCount(compressedImage)); 33 } 34 ); 35 } 36 ); 37 } 38}
Quicker and effortless method.
Getting directly an image at a maximum of "X" MegaBytes, using a built-in algorithm:
1import {Component} from "@angular/core"; 2import {NgxImageCompressService} from "ngx-image-compress"; 3 4@Component({ 5 selector: "app-root", 6 template: ` 7 <button (click)="compressFile()">Upload and compress Image</button> 8 <img [src]="imgResult" *ngIf="imgResult" /> 9 `, 10}) 11export class AppComponent { 12 13 constructor(private imageCompress: NgxImageCompressService) { 14 } 15 16 imgResult: string = ""; 17 18 compressFile() { 19 const MAX_MEGABYTE = 2; 20 this.imageCompress 21 .uploadAndGetImageWithMaxSize(MAX_MEGABYTE) // this function can provide debug information using (MAX_MEGABYTE,true) parameters 22 .then( 23 (result: string) => { 24 this.imgResult = result; 25 }, 26 (result: string) => { 27 console.error('The compression algorithm didn\'t succed! The best size we can do is', this.imageCompress.byteCount(result), 'bytes') 28 this.imgResult = result; 29 }); 30 } 31}
For uploading multiple files, instead of using
1this.imageCompress.uploadFile() 2 .then((singleFile: { image: string, fileName:string, orientation: number }) => //...
You can use
1this.imageCompress.uploadMultipleFiles() 2 .then((arrayOfFiles: { image: string, fileName:string, orientation: number }[]) => //...
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.
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
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 remplace 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).
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).
uploadFileOrReject
and uploadMultipleFileOrReject
)uploadFile
or uploadMultiple
. With these, the promise stays silent when the user cancel the upload selection.index.ts
before submitting it
again to npm
, in order to make it more handy.No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
packaging workflow detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 4
Details
Reason
Found 1/22 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
30 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-12-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More