在我的 Angular 6 应用程序中,我正在制作一个文件上传选项,在预览中,上传的文件需要显示自动裁剪和自动调整大小。
我尝试过以下方法,
HTML:
<canvas id="canvas"></canvas>
<div style="display:none;">
<img id="source" [src]="url" width="300" height="227">
</div>
<input type='file' (change)="onSelectFile($event)">
文件选择功能:
onSelectFile(event) {
if (event.target.files && event.target.files[0]) {
var reader = new FileReader();
reader.readAsDataURL(event.target.files[0]); // read file as data url
reader.onload = (event) => { // called once readAsDataURL is completed
this.url = event.target.result;
}
const canvas : any = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const image = document.getElementById('source');
ctx.drawImage(image, 33, 71, 104, 124, 21, 20, 87, 104);
}
}
在上面我已经尝试了以下链接的参考https://jsfiddle.net/8jwq3cs7/ https://jsfiddle.net/8jwq3cs7/
const canvas : any = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const image = document.getElementById('source');
ctx.drawImage(image, 33, 71, 104, 124, 21, 20, 87, 104);
在使用画布之前,原始图像如下所示:https://mdn.mozillademos.org/files/5397/rhino.jpg https://mdn.mozillademos.org/files/5397/rhino.jpg
而使用canvas之后是这样的:https://jsfiddle.net/8jwq3cs7/ https://jsfiddle.net/8jwq3cs7/
但如果我选择图像choose file
那我就是看不到照片选择后...
工作示例: https://stackblitz.com/edit/angular-file-upload-preview-uwpf8f https://stackblitz.com/edit/angular-file-upload-preview-uwpf8f
如果不是以 Angular 的方式,即使是仅使用纯 JavaScript 的解决方案也将是可观的......
要求是,如果我选择一个文件,那么需要裁剪相同的文件并在预览中自动适应尺寸...
请帮助我实现结果没有 jQuery 或任何库...