import { IQueryParameter } from '@kentico/kontent-core' ; import { Parameters } from '../models' ; import { ImageCompression , ImageFitMode , ImageFormat } from './image.models' ; export class ImageUrlTransformationBuilder { private queryParams : IQueryParameter [ ] = [ ] ; private readonly automaticFormatValue : string = 'format' ; constructor ( /** Image url */ public imageUrl ) { } param param withCustomParam ( param ) { this . queryParams . push ( new Parameters . CustomParameter ( param ) ) ; return this ; } param dpr withDpr ( dpr ) { this . queryParams . push ( new Parameters . QueryParameter ( 'dpr' , dpr . toString ( ) ) ) ; return this ; } param x param y param width param height withRectangleCrop ( x , y , width , height ) { this . queryParams . push ( new Parameters . QueryParameter ( 'rect' , [ x , y , width , height ] . join ( ',' ) ) ) ; return this ; } param x param y param z withFocalPointCrop ( x , y , z ) { this . withFitMode ( 'crop' ) ; this . queryParams . push ( new Parameters . QueryParameter ( 'crop' , 'focalpoint' ) ) ; this . queryParams . push ( new Parameters . QueryParameter ( 'fp-x' , x . toString ( ) ) ) ; this . queryParams . push ( new Parameters . QueryParameter ( 'fp-y' , y . toString ( ) ) ) ; this . queryParams . push ( new Parameters . QueryParameter ( 'fp-z' , z . toString ( ) ) ) ; return this ; } param compression withAutomaticFormat ( backupFormat ? ) { this . queryParams . push ( new Parameters . QueryParameter ( 'auto' , this . automaticFormatValue ) ) ; if ( backupFormat ) { this . withFormat ( backupFormat ) ; } return this ; } param fitMode withFitMode ( fitMode ) { this . queryParams . push ( new Parameters . QueryParameter ( 'fit' , fitMode ) ) ; return this ; } param compression withCompression ( compression ) { this . queryParams . push ( new Parameters . QueryParameter ( 'lossless' , compression === 'lossless' ? 'true' : 'false' ) ) ; return this ; } param quality withQuality ( quality ) { this . queryParams . push ( new Parameters . QueryParameter ( 'q' , quality . toString ( ) ) ) ; return this ; } param format withFormat ( format ) { this . queryParams . push ( new Parameters . QueryParameter ( 'fm' , format ) ) ; return this ; } param height withHeight ( height ) { this . queryParams . push ( new Parameters . QueryParameter ( 'h' , height . toString ( ) ) ) ; return this ; } param width withWidth ( width ) { this . queryParams . push ( new Parameters . QueryParameter ( 'w' , width . toString ( ) ) ) ; return this ; } /**
     * Gets params applied to image
     */ getParams ( ) { return this . queryParams ; } /**
     * Gets query string part of the image URL
     */ getQueryString ( ) { let query = '' ; this . queryParams . forEach ( filter => { if ( query . indexOf ( '?' ) > - 1 ) { query = query + '&' + filter . getParam ( ) ; } else { query = query + '?' + filter . getParam ( ) ; } } ) ; return query ; } /**
     * Gets full transformed url to image with parameters
     */ getUrl ( ) { return this . imageUrl + this . getQueryString ( ) ; } }