All files / lib ngx-image-compress.service.ts

37.5% Statements 6/16
33.33% Branches 4/12
23.07% Functions 3/13
37.5% Lines 6/16

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                  1x         7x     7x 7x               9x                                                                                                                             5x                                                                
import {Injectable, Renderer2, RendererFactory2} from '@angular/core';
import {ImageCompress} from './image-compress';
import {DataUrl} from './models/data-url';
import {DOC_ORIENTATION} from './models/DOC_ORIENTATION';
import {UploadResponse} from './models/upload-response';
 
@Injectable({
    providedIn: 'root',
})
export class NgxImageCompressService {
    private readonly render: Renderer2;
 
    private imageCompress: ImageCompress;
 
    public DOC_ORIENTATION = DOC_ORIENTATION;
 
    constructor(rendererFactory: RendererFactory2) {
        this.render = rendererFactory.createRenderer(null, null);
        this.imageCompress = new ImageCompress();
    }
 
    /**
     * helper to evaluate the compression rate
     * @param imgString the image in base64 string format
     */
    public byteCount(image: DataUrl) {
        return this.imageCompress.byteCount(image);
    }
 
    /**
     * Get the correct Orientation value from image tags
     */
    public getOrientation(file: File): Promise<DOC_ORIENTATION> {
        return this.imageCompress.getOrientation(file);
    }
 
    /**
     * return a promise with the new image data and image orientation
     * Nothing happen if no file have been selected
     */
    public uploadFile(): Promise<UploadResponse> {
        return this.imageCompress.uploadFile(this.render, false) as Promise<UploadResponse>;
    }
 
    /**
     * return a promise with an array of image data and image orientation
     * Nothing happen if no files have been selected
     */
    public uploadMultipleFiles(): Promise<UploadResponse[]> {
        return this.imageCompress.uploadFile(this.render, true) as Promise<UploadResponse[]>;
    }
 
    /**
     * return a promise with the new image data and image orientation
     * the promise will reject if no file have been selected
     */
    public uploadFileOrReject(): Promise<UploadResponse> {
        return this.imageCompress.uploadFile(this.render, false, true) as Promise<UploadResponse>;
    }
 
    /**
     * return a promise with an array of image data and image orientation
     * the promise will reject if no files have been selected
     */
    public uploadMultipleFilesOrReject(): Promise<UploadResponse[]> {
        return this.imageCompress.uploadFile(this.render, true, true) as Promise<UploadResponse[]>;
    }
 
    /**
   * perform a compression from the given DataUrl (string), provided by the uploadFile, or uploadMultipleFiles method
   *
   *
   | 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) <sup>[1](#fn1)</sup> |
   | quality     | number | JPEG quality factor as a percentage (optional, default: 50) <sup>[2](#fn2)</sup>  |
   | 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) |
   */
    public compressFile(
        image: DataUrl,
        orientation: DOC_ORIENTATION,
        ratio = 50,
        quality = 50,
        maxWidth = 0,
        maxHeight = 0
    ): Promise<DataUrl> {
        return this.imageCompress.compress(image, orientation, this.render, ratio, quality, maxWidth, maxHeight);
    }
 
    /**
     * Most simple function to use here.
     * Perform an upload and return an image dataUrl (string format) with a maximum size, given in *MegaBytes*
     * If the size can't be reached, the best that can be reached will be returned in promise *rejection*
     * Put debugMode to true if you have some trouble to print some help using console.debug
     */
    public uploadAndGetImageWithMaxSize(maxSizeMb = 1, debugMode = false, rejectOnCancel = false): Promise<DataUrl> {
        return this.imageCompress
            .uploadGetImageMaxSize(maxSizeMb, debugMode, this.render, rejectOnCancel)
            .then(uploadResponse => uploadResponse.image)
            .catch(e => {
                throw e.image;
            });
    }
 
    /**
     * Same as before, but return more informations (file name...)
     */
    public uploadAndGetImageWithMaxSizeAndMetas(maxSizeMb = 1, debugMode = false, rejectOnCancel = false): Promise<UploadResponse> {
        return this.imageCompress.uploadGetImageMaxSize(maxSizeMb, debugMode, this.render, rejectOnCancel);
    }
 
    /**
     * Not handling the upload, you need to provide the file and the orientation by yourself
     */
    public getImageWithMaxSizeAndMetas(file: UploadResponse, maxSizeMb = 1, debugMode = false): Promise<UploadResponse> {
        return this.imageCompress.getImageMaxSize(file, maxSizeMb, debugMode, this.render);
    }
}