Gathering detailed insights and metrics for ngx-image-compress
Gathering detailed insights and metrics for ngx-image-compress
Gathering detailed insights and metrics for ngx-image-compress
Gathering detailed insights and metrics for ngx-image-compress
npm install ngx-image-compress
Typescript
Module System
Node Version
NPM Version
74.5
Supply Chain
97.4
Quality
80.3
Maintenance
100
Vulnerability
98.9
License
TypeScript (85.75%)
HTML (12.07%)
JavaScript (1.3%)
SCSS (0.88%)
Total Downloads
3,043,523
Last Day
882
Last Week
15,128
Last Month
91,872
Last Year
1,012,285
87 Stars
284 Commits
36 Forks
3 Watching
5 Branches
11 Contributors
Latest Version
18.1.5
Package Id
ngx-image-compress@18.1.5
Unpacked Size
169.16 kB
Size
41.60 kB
File Count
23
NPM Version
10.7.0
Node Version
20.15.1
Publised On
26 Jul 2024
Cumulative downloads
Total Downloads
Last day
-79.9%
882
Compared to previous day
Last week
-31.9%
15,128
Compared to previous week
Last month
-0.7%
91,872
Compared to previous month
Last year
23.2%
1,012,285
Compared to previous year
1
2
Angular utility for compressing images to a satisfying size, that you can choose
1npm i ngx-image-compress
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:
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 {}
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 constructor(private imageCompress: NgxImageCompressService) {} 14 15 imgResultBeforeCompression: string = ''; 16 imgResultAfterCompression: string = ''; 17 18 compressFile() { 19 this.imageCompress.uploadFile().then(({image, orientation}) => { 20 this.imgResultBeforeCompression = image; 21 console.log('Size in bytes of the uploaded image was:', this.imageCompress.byteCount(image)); 22 23 this.imageCompress 24 .compressFile(image, orientation, 50, 50) // 50% ratio, 50% quality 25 .then(compressedImage => { 26 this.imgResultAfterCompression = compressedImage; 27 console.log('Size in bytes after compression is now:', this.imageCompress.byteCount(compressedImage)); 28 }); 29 }); 30 } 31}
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 constructor(private imageCompress: NgxImageCompressService) {} 13 14 imgResult: string = ''; 15 16 compressFile() { 17 const MAX_MEGABYTE = 2; 18 this.imageCompress 19 .uploadAndGetImageWithMaxSize(MAX_MEGABYTE) // this function can provide debug information using (MAX_MEGABYTE,true) parameters 20 .then( 21 (result: string) => { 22 this.imgResult = result; 23 }, 24 (result: string) => { 25 console.error( 26 "The compression algorithm didn't succed! The best size we can do is", 27 this.imageCompress.byteCount(result), 28 'bytes' 29 ); 30 this.imgResult = result; 31 } 32 ); 33 } 34}
1this.imageCompress.getImageWithMaxSizeAndMetas({image: 'base64ValueFromYourUpload'},MAX_MEGABYTE).then
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 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).
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 dangerous workflow patterns detected
Reason
no binaries found in the repo
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
no effort to earn an OpenSSF best practices badge detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
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-23
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