My objective:
- To upload image file in my browser, edit(crop & rotate) it and download/upload it.
What I have achieved so far:
- I am just able to rotate the image but just for once.
What issues I'm facing:
- As mentioned in the official docs, I'm not able to use
this.angularCropper.cropper.rotate(degreeInNumber);
it gives this error:Cannot read property 'rotate' of undefined
. - As it is a wrapper around CropperJS a popular JS image library, so I tried CropperJS's syntax
this.cropper.rotate(degreeInNumber);
(insiderotateLeft()
function) and it works but just for once. When I callrotateLeft()
function for the second time it does not work. - Also, despite mentioning
crossorigin
in<input>
, I'm gettingCannot read property 'checkCrossOrigin' of undefined
Here's my code for app.component.html
:
<input crossorigin type='file' (change)="readUrl($event)">
<img crossorigin id="img" [src]="url">
<div id="target"></div>
<div [hidden]="!(url != null)">
<angular-cropper crossorigin [cropperOptions]="cropper" [imageUrl]="url" #angularCropper></angular-cropper>
</div>
<br><br>
<button (click)="rotateLeft()">Left</button>
And, my app.component.ts
:
import { Component } from '@angular/core';
import { AngularCropperjsComponent } from 'angular-cropperjs';
import { ViewChild } from '@angular/core';
import * as Cropper from 'cropperjs/dist/cropper';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
@ViewChild('angularCropper') public angularCropper: AngularCropperjsComponent;
title = 'app';
tilt = 0;
url: string = null;
cropper: Cropper;
rotateLeft() {
let image = document.querySelector('#img');
let canvas = document.querySelector('#canvas');
let target = document.querySelector('#target');
this.tilt -= 90;
let that = this;
this.cropper = new Cropper(image, {
minContainerWidth: 400,
minContainerHeight: 400,
minCanvasWidth: 250,
minCanvasHeight: 250,
ready: function() {
this.cropper.rotate(that.tilt); <--- This works, but only for ONCE
}
});
this.angularCropper.cropper.rotate(66); <--- This does NOT work
console.log(this.cropper)
}
// This is for showing the uploaded image to <img crossorigin id="img" [src]="url">
readUrl(event: any) {
if (event.target.files && event.target.files[0]) {
const reader = new FileReader();
reader.onload = (e: any) => {
this.url = e.target.result;
};
reader.readAsDataURL(event.target.files[0]);
}
}
}
I'm pretty new to this can anyone please point out what am I missing/ doing wrong?
Also, how do I get back the cropped image?