angular-cropperjs icon indicating copy to clipboard operation
angular-cropperjs copied to clipboard

replace() didn't work

Open cihad opened this issue 8 years ago • 8 comments

When I run:

this.angularCropper.cropper.replace(imageUrl)

image has changed. But when I run:

this.angularCropper.cropper.getCroppedCanvas().toDataURL("image/jpeg");

I got this error:

AppComponent.html:7 ERROR TypeError: Cannot read property 'toDataURL' of null

It means getCroppedCanvas() returns null.

Thanks.

cihad avatar Sep 25 '17 19:09 cihad

I'll make a test, give me a moment.

matheusdavidson avatar Sep 25 '17 19:09 matheusdavidson

i have confirmed the problem with the replace method, will need more time to find a fix. What you could do in the meantime is destroy angular-cropper html with ngIf, change the image and place the element again. Thats the way i do in https://github.com/matheusdavidson/angular-upcrop

matheusdavidson avatar Sep 25 '17 20:09 matheusdavidson

Thank you for your interest. I will try.

cihad avatar Sep 25 '17 20:09 cihad

This is still bug? Exist some way to avoid this problem?

mariuszr2404 avatar Feb 27 '18 15:02 mariuszr2404

onFileAdded(event){

const angularCropper = this.angularCropper;

const input = event.target;

  if (input.files && input.files[0]) {
    const reader = new FileReader();

    reader.onload = function (e) {
      **angularCropper.cropper.destroy();
      angularCropper.imageUrl = e.target.result;**
    };
    reader.readAsDataURL(input.files[0]);
  }

}

This is my solution for now, hope it stays stable. When I do replace, it creates another instance below the existing one.

DanielEberl avatar Aug 09 '18 16:08 DanielEberl

This seems to work for me:

readFile(file: File) {
const myReader = new FileReader();

myReader.onloadend = (loadEvent: any) => {
    this.angularCropper.imageUrl = loadEvent.target.result;

    this.angularCropper.cropper.destroy();
};

myReader.readAsDataURL(file);
}

Am I doing this right? The call to .destroy() reloads the cropper.

If I look at the source code of https://fengyuanchen.github.io/cropperjs/ I see this:

if (/^image\/\w+/.test(file.type)) {
    uploadedImageType = file.type;
    uploadedImageName = file.name;

    if (uploadedImageURL) {
    URL.revokeObjectURL(uploadedImageURL);
    }

    image.src = uploadedImageURL = URL.createObjectURL(file);
    cropper.destroy();
    cropper = new Cropper(image, options);
    inputImage.value = null;
} else {
    window.alert('Please choose an image file.');
}

As you can see, a new Cropper gets created after the old one is destroyed.

So my question is: is calling .destroy() enough? It works perfectly BTW

m-ghaoui avatar Dec 06 '18 13:12 m-ghaoui

Found Solution :

All we need is to call destroy();

this.angularCropper.cropper.destroy();

Demo : https://stackblitz.com/edit/angular-cropperjs-replace-demo

vivekdoshi2 avatar Jan 09 '19 10:01 vivekdoshi2

This may not be an issue with this library but with cropperjs itself, see: https://github.com/fengyuanchen/cropperjs/issues/617

MaccabeeY avatar Jan 11 '20 12:01 MaccabeeY