ng2-image-cropper的使用

本文介绍了一个基于Angular 2的图片裁剪组件的实现方式,该组件允许用户自定义裁剪框大小,并提供了调整裁剪区域宽度和高度的功能。通过设置不同的参数,如裁剪框尺寸、最小尺寸等,可以灵活地调整图片裁剪效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//our root app component
import {Component, NgModule, ViewChild} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import {ImageCropperComponent, CropperSettings, Bounds} from 'ng2-img-cropper';




@Component({
  selector: 'my-app',
  template: `
    <div>
     
     <img-cropper [image]="data1" [settings]="cropperSettings1" (onCrop)="cropped($event)"></img-cropper>
     <span class="result" *ngIf="data1.image" >
        <img [src]="data1.image" 
        [width]="croppedWidth" 
        [height]="croppedHeight">
        {{data1.image.length}}
    </span>
    


    </div>
  `,
})
export class App {
    name:string;
    data1:any;
    cropperSettings1:CropperSettings;
    croppedWidth:number;
    croppedHeight:number;
    
    @ViewChild('cropper', undefined) cropper:ImageCropperComponent;
    
    constructor() {
      this.name = 'Angular2'
      this.cropperSettings1 = new CropperSettings();
      this.cropperSettings1.width = 200;
      this.cropperSettings1.height = 200;


      this.cropperSettings1.croppedWidth = 200;
      this.cropperSettings1.croppedHeight = 200;


      this.cropperSettings1.canvasWidth = 500;
      this.cropperSettings1.canvasHeight = 300;


      this.cropperSettings1.minWidth = 10;
      this.cropperSettings1.minHeight = 10;


      this.cropperSettings1.rounded = false;
      this.cropperSettings1.keepAspect = false;


      this.cropperSettings1.cropperDrawSettings.strokeColor = 'rgba(255,255,255,1)';
      this.cropperSettings1.cropperDrawSettings.strokeWidth = 2;


      this.data1 = {};
  }
  
  cropped(bounds:Bounds) {
    this.croppedHeight =bounds.bottom-bounds.top;
    this.croppedWidth = bounds.right-bounds.left;
  }
  
  fileChangeListener($event) {
    var image:any = 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;
        this.croppedWidth = image.width;
        this.croppedHeight = image.height;
        that.cropper.setImage(image);


    };


    myReader.readAsDataURL(file);
    }
}


@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App, ImageCropperComponent ],
  bootstrap: [ App ]
})
export class AppModule {}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值