All files app.component.ts

59.01% Statements 36/61
0% Branches 0/4
51.85% Functions 14/27
59.01% Lines 36/61

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152                1x 8x 8x 8x 8x 8x 8x 8x 8x 8x   8x     1x 1x 1x   1x 1x 1x           1x     1x 1x 1x 1x               1x 1x 1x         1x 1x 1x   1x 1x 1x           2x   1x     1x         1x                                                                       8x                                                                            
import {Component} from '@angular/core';
import {DataUrl, DOC_ORIENTATION, NgxImageCompressService, UploadResponse} from 'ngx-image-compress';
 
@Component({
    selector: 'ngx-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss'],
})
export class AppComponent {
    imgResultBeforeCompress: DataUrl = '';
    imgResultAfterCompress: DataUrl = '';
    imgResultAfterResize: DataUrl = '';
    imgResultUpload: DataUrl = '';
    imgResultAfterResizeMax: DataUrl = '';
    imgResultAfterResizeMax2: DataUrl = '';
    imgResultAfterResizeMaxWithLoading: DataUrl = '';
    imgResultMultiple: UploadResponse[] = [];
    loadingCompression = false;
 
    constructor(private imageCompress: NgxImageCompressService) {}
 
    compressFile() {
        return this.imageCompress.uploadFile().then(({image, orientation}: UploadResponse) => {
            this.imgResultBeforeCompress = image;
            console.warn('Size in bytes was:', this.imageCompress.byteCount(image));
 
            this.imageCompress.compressFile(image, orientation, 50, 50).then((result: DataUrl) => {
                this.imgResultAfterCompress = result;
                console.warn('Size in bytes is now:', this.imageCompress.byteCount(result));
            });
        });
    }
 
    uploadFile() {
        this.imageCompress
            .uploadFileOrReject()
            .then(({image, orientation, fileName}) => {
                this.imgResultUpload = image;
                console.warn('File Name:', fileName);
                console.warn('Image Orientation', DOC_ORIENTATION[orientation]);
                console.warn(`Image encoded in ${image?.substring(0, 50)}... (${image?.length} characters)`);
            })
            .catch(error => {
                console.log('UploadRejection: ', error);
            });
    }
 
    uploadMultipleFiles() {
        return this.imageCompress.uploadMultipleFiles().then((multipleOrientedFiles: UploadResponse[]) => {
            this.imgResultMultiple = multipleOrientedFiles;
            console.warn(`${multipleOrientedFiles.length} files selected`);
        });
    }
 
    uploadAndResize() {
        return this.imageCompress.uploadFile().then(({image, orientation}: UploadResponse) => {
            console.warn('Size in bytes was:', this.imageCompress.byteCount(image));
            console.warn('Compressing and resizing to width 200px height 100px...');
 
            this.imageCompress.compressFile(image, orientation, 50, 50, 200, 100).then((result: DataUrl) => {
                this.imgResultAfterResize = result;
                console.warn('Size in bytes is now:', this.imageCompress.byteCount(result));
            });
        });
    }
 
    uploadAndReturnWithMaxSize() {
        return this.imageCompress.uploadAndGetImageWithMaxSize(1, true).then(
            (result: DataUrl) => {
                this.imgResultAfterResizeMax = result;
            },
            (result: string) => {
                console.error(
                    "The compression algorithm didn't succeed! The best size we can do is",
                    this.imageCompress.byteCount(result),
                    'bytes'
                );
                this.imgResultAfterResizeMax = result;
            }
        );
    }
 
    uploadAndReturnWithMaxSizeWithLoading() {
        this.loadingCompression = true;
        return this.imageCompress
            .uploadAndGetImageWithMaxSize(1, true, true)
            .then(
                (result: DataUrl) => {
                    this.imgResultAfterResizeMaxWithLoading = result;
                },
                (result: any) => {
                    if (result instanceof Error) {
                        if ((result as Error).message.includes('no file selected')) {
                            console.log('No file selected');
                        } else {
                            console.error('Unknown error:', result);
                        }
                    } else {
                        let strResult = result as string;
                        console.error(
                            "The compression algorithm didn't succeed! The best size we can do is",
                            this.imageCompress.byteCount(strResult),
                            'bytes'
                        );
                        this.imgResultAfterResizeMaxWithLoading = strResult;
                    }
                }
            )
            .finally(() => {
                this.loadingCompression = false;
            });
    }
 
    imageCapture: DataUrl = '';
 
    getImageFromCamera(newImage: DataUrl) {
        this.imageCapture = newImage;
    }
 
    getError(errorMessage: string) {
        console.error(errorMessage);
    }
 
    compressImageCapture() {
        console.warn('Size in bytes was:', this.imageCompress.byteCount(this.imageCapture));
 
        this.imageCompress.compressFile(this.imageCapture, 1, 50, 50).then((result: DataUrl) => {
            this.imageCapture = result;
            console.warn('Size in bytes is now:', this.imageCompress.byteCount(result));
        });
    }
 
    usingSeparateMethodForMaxSize() {
        this.imageCompress
            // This could me any method of your choice that is returning a DataUrl of an uploaded image
            .uploadFileOrReject()
            .then(({image, orientation, fileName}) => {
                console.warn('File Name:', fileName);
                console.warn('Image Orientation', DOC_ORIENTATION[orientation]);
                console.warn(`Image encoded in ${image?.substring(0, 50)}... (${image?.length} characters)`);
                // This is the method you need to use to compress the image to a maximum size
                return this.imageCompress.getImageWithMaxSizeAndMetas({image, orientation, fileName}, 1);
            })
            .then((result: UploadResponse) => {
                this.imgResultAfterResizeMax2 = result.image;
            })
            .catch(error => {
                console.log('UploadRejection: ', error);
            });
    }
}