Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cropper is not working when i am trying to use in ng-template #84

Open
Vishwjeet9097 opened this issue Jun 24, 2023 · 1 comment
Open

Comments

@Vishwjeet9097
Copy link

Screenshot 2023-06-25 at 3 06 48 AM

<button class="btn btn-primary" (click)="openImageModal(imageModal)">Launch demo modal

<ng-template #imageModal let-modal>

Modal title
        </div>
        <div class="d-flex justify-content-between align-items-center flex-wrap">
          <div class="d-flex align-items-center flex-wrapp me-2 mt-3">
            <button class="btn btn-primary crop mb-2 mb-md-0" (click)="cropImage()">Crop me</button>
          </div>
          <div class="mb-4 mb-md-0 mt-3">
            <a href="" class="btn btn-outline-primary download" download="imageName.png">Download croped image</a>
          </div>
        </div>
      </div>
      <div class="col-md-12 ms-auto">
        <h6 class="text-muted mb-3">Cropped Image: </h6>
        <img class="w-100 cropped-img mt-2" src="{{resultImage}}" alt="">
      </div>
    </div>
  </div>
</div>
================================= typescript ================================= @ViewChild('angularCropper') public angularCropper: CropperComponent;

imageUrl: any = 'assets/images/others/placeholder.jpg';
resultImage: any;

// Plugin configuration
config = {
zoomable: false
};

handleFileInput(event: any) {
if (event.target.files.length) {
var fileTypes = ['jpg', 'jpeg', 'png']; //acceptable file types
var extension = event.target.files[0].name.split('.').pop().toLowerCase(), //file extension from input file
isSuccess = fileTypes.indexOf(extension) > -1; //is extension in acceptable types
if (isSuccess) { //yes
// start file reader
const reader = new FileReader();
const angularCropper = this.angularCropper;
reader.onload = (event) => {
if (event.target?.result) {
angularCropper.imageUrl = event.target.result;
// this.imageUrl = event.target.result;
}
};
reader.readAsDataURL(event.target.files[0]);
} else { //no
alert('Selected file is not an image. Please select an image file.')
}
}
}

cropImage() {
console.log('clicked', this.angularCropper.cropper.getCroppedCanvas().toDataURL());
const resultImage = this.angularCropper.cropper.getCroppedCanvas().toDataURL();
console.log('clicked', this.resultImage);
this.imageUrl = this.angularCropper.cropper.getCroppedCanvas().toDataURL();
let dwn: HTMLElement = document.querySelector('.download') as HTMLElement;
dwn.setAttribute('href', this.angularCropper.cropper.getCroppedCanvas().toDataURL());
}

@matheusdavidson
Copy link
Owner

Hi there, can you please post the entire html and ts file, properly formatted so I can debug.

It looks like you didn't load the ViewChild("angularCropper") the right way, did you add #angularCropper to the component in the view?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants