我的目标:
- 要在我的浏览器中上传图像文件,请对其进行编辑(裁剪和旋转)并下载/上传。
到目前为止我所取得的成就:
- 我只能旋转图像,但只能旋转一次。
我面临什么问题:
- 正如官方文档中提到的,我无法使用
this.angularCropper.cropper.rotate(degreeInNumber);
它给出了这个错误:Cannot read property 'rotate' of undefined
。 - 因为它是一个流行的 JS 图像库CropperJS的包装器,所以我尝试了 CropperJS 的语法
this.cropper.rotate(degreeInNumber);
(内部rotateLeft()
函数),它只工作一次。当我rotateLeft()
第二次调用函数时它不起作用。 - 此外,尽管提到
crossorigin
,<input>
我得到Cannot read property 'checkCrossOrigin' of undefined
这是我的代码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>
而且,我的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]);
}
}
}
我对此很陌生,谁能指出我错过了什么/做错了什么?
另外,如何取回裁剪后的图像?