File

projects/i-components/src/lib/directives/imagedatatobase64.directive.ts

Description

Angular Pipe for the ImageDataToBase64

Metadata

Name imagedatatobase64

Methods

Public transform
transform(value: any)

Does the require transformation for the pipe

Parameters :
Name Type Optional Description
value any No

Value to transform

Returns : any

The transformed value

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { WriteableBitmap } from '@mobilize/wms-framework';

/**
 * Angular Pipe for the ImageDataToBase64
 *
 * @export
 * @class ClickActionParameterDirective
 */
@Pipe({
  name: 'imagedatatobase64',
})
export class ImageDataToBase64 implements PipeTransform {
  /**
   * Creates an instance of ImageDataToBase64
   *
   * @param {DomSanitizer} sanitizer The dom sanitizer service instance
   * @memberof ImageDataToBase64
   */
  constructor(private sanitizer: DomSanitizer) {}

  /**
   * Does the require transformation for the pipe
   *
   * @param {any} value Value to transform
   * @memberof ImageDataToBase64
   * @returns {any} The transformed value
   */
  public transform(value: any): any {
    let data = [];
    if (value instanceof WriteableBitmap) {
      data = value.data;
    } else {
      /* istanbul ignore else */
      if (value instanceof Array) {
        data = value;
      }
    }
    /// WARNING btoa may not be available in all browsers
    const base64Text =
      'data:image/gif;base64,' +
      btoa(data.map((d) => String.fromCharCode(d)).join(''));
    return this.sanitizer.bypassSecurityTrustUrl(base64Text);
  }
}

result-matching ""

    No results matching ""