angular2-img-cropper icon indicating copy to clipboard operation
angular2-img-cropper copied to clipboard

SetImage Undefined

Open limpep opened this issue 9 years ago • 15 comments

Hi,

I am trying to load the image that I get from the server in my loadImage method but I keep getting the following error Cannot read property 'setImage' of undefined. Not sure why, any help would be much appreciated.

Here is my code below

  data: any;

  cropperSettings: CropperSettings;

  croppedWidth:number;
  croppedHeight:number;

  @ViewChild('cropper', undefined)
  cropper:ImageCropperComponent;

  constructor() {
    this.data = {};
  }

  ngOnInit() {

    this.cropperSettings = new CropperSettings();
    this.cropperSettings.fileType = "image/jpeg";

    this.cropperSettings.width = 400;
    this.cropperSettings.height = 400;

    this.cropperSettings.croppedWidth = 400;
    this.cropperSettings.croppedHeight = 400;

    this.cropperSettings.canvasWidth = 150;
    this.cropperSettings.canvasHeight = 150;

    this.cropperSettings.rounded = false;
    this.cropperSettings.keepAspect = true;
    // this.cropperSettings.dynamicSizing = true;

    this.cropperSettings.noFileInput = true;
    this.cropperSettings.cropperDrawSettings.strokeColor = 'rgba(0,0,0,0.5)';
    this.cropperSettings.cropperDrawSettings.strokeWidth = 1;
  }

  cropped(bounds:Bounds) {
    this.croppedHeight = bounds.bottom-bounds.top;
    this.croppedWidth = bounds.right-bounds.left;
  }


  private loadImage(){
    let imgObj = new Image();
    imgObj.src = 'data:image/jpeg;base64,' + this.user.avatar;
    this.cropper.setImage(imgObj);
  }

  fileChangeListener($event) {
    let image:any = new Image();;

    let file = $event.target.files[0];
    let myReader:FileReader = new FileReader();
    let that = this;

    myReader.onloadend = function (loadEvent:any) {
      that.cropper.reset();
      image.src = loadEvent.target.result;
      that.cropper.setImage(image);
    };

    myReader.readAsDataURL(file);
  }

limpep avatar Mar 31 '17 16:03 limpep

Same issue here... my component is placed in modal...

markovicboban avatar Apr 06 '17 18:04 markovicboban

Same issue here. I'm using this component inside ng2-bootstrap-modal. The image data is obtained from a file upload and then I try to init the cropper inside the modal without success. I've tried to put setImage function inside ngAfterViewInit() or ngOnInit() but the result is the same.

gioymartin avatar Apr 17 '17 17:04 gioymartin

Check if you have added 'ImageCropperComponent' in your ngModule declarations.

ramk18 avatar May 02 '17 11:05 ramk18

How did you guys resolve this?

For me it works only if I do not set this.cropperSettings.noFileInput = true; But then I cant style the Input field which is not very desirable

anik657 avatar May 11 '17 22:05 anik657

In my case it worked adding the settings to the cropper object at the end of ngOnInit()

this.cropper.settings = this.cropperSettings;

gioymartin avatar Jun 02 '17 13:06 gioymartin

Hello,

I am working in something similar, working for me with the following code:

let image = new Image();  
image.src = this.model[this.key];
image.crossOrigin = 'Anonymous'; 

image.onload = () => { 
   this.cropper.setImage(image); 
};  

you need to be sure that the image is loaded, before to set it.

Xepe avatar Jun 05 '17 22:06 Xepe

I ended up using the base64 provided to me by my backend system.

limpep avatar Jun 06 '17 09:06 limpep

Same problem here, trying to upload an image from a component and passing it to a modal (bsx) and then add it to the image cropper.

defra91 avatar Aug 30 '17 13:08 defra91

Just crossed this frustrating issue and spent ~4 frustrating hours trying to find a solution. The problem is that cropper is inside the modal and is never initiated with viewchild:

  @ViewChild('cropper', undefined)
  cropper:ImageCropperComponent;

This describes the problem https://stackoverflow.com/questions/34947154/angular-2-viewchild-annotation-returns-undefined , tried all the suggestions but nothing works for this particular case.

After a coffe this came to my mind and works for me:

<div class="file-upload">
    <span class="text">upload</span>
    <input id="custom-input" type="file" (change)="fileChangeListener($event, cropper)">
</div>  
fileChangeListener($event, cropperComp: ImageCropperComponent) {

  this.cropper = cropperComp;

  let image = new Image();
  var file:File = $event.target.files[0];
  var myReader:FileReader = new FileReader();
  var that = this;

  myReader.onloadend = function (loadEvent: any) {
      image.src = loadEvent.target.result;
      that.cropper.setImage(image);
  };
  myReader.readAsDataURL(file);
}

analuisamartins avatar Oct 18 '17 10:10 analuisamartins

@analuisamartins thank you!

odaikurd avatar Jan 26 '18 18:01 odaikurd

@analuisamartins working for me, thank you!

ghost avatar Jan 29 '18 15:01 ghost

I had the same issue when using this.cropperSettings.noFileInput = true;. A simple omission from the example was the issue for me: #cropper selector in the view. It's right there in the Customizing Image cropper example. Added that and it works as expected.

scottbullard avatar Apr 10 '18 05:04 scottbullard

In my case, it was just an error importing Image, I was sending a wrong image model.

sophiecmusical avatar Apr 26 '18 12:04 sophiecmusical

@analuisamartins thank you !!!! it's working very very fine...............................

ChirantanPatel avatar May 11 '18 18:05 ChirantanPatel

it happened to me when I removed the this.data = {}; of the constructor, just add it again.

ghost avatar Jun 26 '18 23:06 ghost