File

src/app/layout/picture-manager/picture-croper/picture-croper.component.ts

Implements

OnInit OnDestroy

Metadata

encapsulation ViewEncapsulation.None
selector app-picture-croper
styleUrls ./picture-croper.component.less
templateUrl ./picture-croper.component.html

Index

Properties
Methods
Inputs
Outputs

Constructor

constructor()

Inputs

aspectRatio
Type : number
cropperOptions
Type : any
Default value : {}
imageUrl
Type : string
loadImageErrorText
Type : string
loading
Default value : false

Outputs

cropperError
Type : EventEmitter
cropperReady
Type : EventEmitter

Methods

imageLoaded
imageLoaded(ev: Event)

Image loaded

Parameters :
Name Type Optional
ev Event No
Returns : void
imageLoadError
imageLoadError()

Image load error

Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void
ngOnInit
ngOnInit()
Returns : void

Properties

Public cropper
Type : Cropper
image
Type : ElementRef
Decorators :
@ViewChild('image')
Public imageElement
Type : HTMLImageElement
Public loadError
Default value : false
import { Component, OnInit, ViewEncapsulation, ElementRef, ViewChild, Input, EventEmitter, Output, OnDestroy } from '@angular/core';
import Cropper from 'cropperjs';

export interface ImageCropperSetting {
  width: number;
  height: number;
}

@Component({
  selector: 'app-picture-croper',
  templateUrl: './picture-croper.component.html',
  styleUrls: ['./picture-croper.component.less'],
  encapsulation: ViewEncapsulation.None
})
export class PictureCroperComponent implements OnInit, OnDestroy {

  @ViewChild('image') image: ElementRef;

  @Input() imageUrl: string;
  @Input() loadImageErrorText: string;
  @Input() cropperOptions: any = {};
  @Input() aspectRatio: number;
  @Input() loading = false;

  @Output() cropperReady = new EventEmitter();
  @Output() cropperError = new EventEmitter();

  public cropper: Cropper;
  public imageElement: HTMLImageElement;
  public loadError = false;

  constructor() {
    this.loadImageErrorText = '图片加载失败,请检查URL是否正确。';
  }

  ngOnInit() {
  }

  /**
   * Image loaded
   */
  imageLoaded(ev: Event) {

    // Unset load error state
    this.loadError = false;
    this.loading = true;

    // Setup image element
    const image = ev.target as HTMLImageElement;
    this.imageElement = image;

    // Add crossOrigin?
    image.crossOrigin = 'anonymous';

    // Set crop options
    // extend default with custom config
    this.cropperOptions = Object.assign(this.cropperOptions, {
      aspectRatio: this.aspectRatio,
      movable: false,
      scalable: false,
      zoomable: false,
      viewMode: 1,
      checkCrossOrigin: true,
      checkOrientation: false,
      ready: () => {
        // Emit ready
        this.cropperReady.emit(true);
        this.loading = false;
      }
    });

    // Set cropperjs
    if (this.cropper) {
      this.cropper.destroy();
      this.cropper = undefined;
    }
    this.cropper = new Cropper(image, this.cropperOptions);
  }

  /**
   * Image load error
   */
  imageLoadError() {

    // Set load error state
    this.loadError = true;

    // emit error
    this.cropperError.emit(true);
  }

  ngOnDestroy() {
    if (this.cropper) {
      this.cropper.destroy();
      this.cropper = undefined;
    }
  }

}
<div class="cropper-wrapper">
  <!-- loading -->
  <nz-empty *ngIf="!imageUrl && !cropper && !loading" nzNotFoundContent="无图片" class="image-cropperjs-empty"></nz-empty>
  <div class="image-cropperjs-spin" *ngIf="loading">
    <nz-spin nzSimple></nz-spin>
  </div>
  <!-- LOAD ERROR -->
  <div class="alert alert-warning" *ngIf="loadError"></div>
  <nz-alert id="image-cropperjs-error" nzType="error" [nzMessage]="loadImageErrorText" nzShowIcon
    *ngIf="loadError && imageUrl">
  </nz-alert>
  <!-- CROPPER -->
  <div class="cropper">
    <img #image id="image-cropperjs" alt="image" [src]="imageUrl" (load)="imageLoaded($event)"
      (error)="imageLoadError()" style="visibility: hidden;" *ngIf="imageUrl" />
  </div>
</div>

./picture-croper.component.less

@import url('cropperjs/dist/cropper.css');

#image-cropperjs {
  display: block;
  max-width: 100%;
}

#image-cropperjs-error {
  margin-bottom: 12px;
}

.image-cropperjs-spin {
  text-align: center;
  background: #f0f2f5;
  border-radius: 4px;
  margin-bottom: 20px;
  padding: 30px 50px;
  margin: 20px 0;
}

.image-cropperjs-empty {
  margin-top: 48px;
}
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""