0

How to resize base64 encoded image in Ionic framework.

I want to resize image on client side before uploading to server.

What is the best strategy in this case ?

f0rza
  • 480
  • 3
  • 17
  • 1
    This answer could help you: https://stackoverflow.com/questions/43212240/set-size-of-a-base64-image-javascript-angularjs?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Ivar Reukers May 24 '18 at 13:31
  • @Ivaro18 Thanks, I'll check it – f0rza May 24 '18 at 13:36

1 Answers1

-1

I solved this with using ng2-img-max library:

uploadDesktopFile() {
    let file = this.documentEl.nativeElement.files[0];

    const maxHeight = 800;
    const maxWidth = 600;

    let self = this;
    this.ng2ImgMax.resizeImage(file, maxHeight, maxWidth).subscribe(
      result => {
        let reader = new FileReader();
        reader.readAsDataURL(result);
        reader.onloadend = function () {
          self.imageURI = reader.result;  // we've got resized base64 sequence at this stage
          //self.uploadFile();
        }
        reader.onerror = function (error) {
          console.error('Error: ', error);
        };
      },
      error => {
        console.error('Error: ', error);
      }
    );
  }
f0rza
  • 480
  • 3
  • 17