1.0.1 • Published 3 years ago

ng-photo-editor v1.0.1

Weekly downloads
59
License
MIT
Repository
github
Last release
3 years ago

Awesome Photo editor for Angular

NPM version The MIT License

example

Demo

Getting started

Step 1: Install ng-bootstrap

   ng add @ng-bootstrap/ng-bootstrap

Step 2: Install ngx-photo-editor

    npm install ngx-photo-editor --save

Example usage:

Add the NgxPhotoEditorModule to the imports of the module.

      import {NgxPhotoEditorModule} from "ngx-photo-editor";

      @NgModule({
        imports: [
          NgxPhotoEditorModule
        ]
      })

Add the element to your HTML:

<div><input type="file" (change)="fileChangeEvent($event)"/></div>

<ngx-photo-editor
  [imageChanedEvent]="imageChangedEvent"
  (imageCropped)="imageCropped($event)"
  [aspectRatio]="4/3"
  [viewMode]="3"
  [resizeToWidth]="500"></ngx-photo-editor>

<img [src]="base64" alt="">

And add this to your ts file:

import {CroppedEvent} from 'ngx-photo-editor';

export class AppComponent {
  imageChangedEvent: any;
  base64: any;
  
  fileChangeEvent(event: any) {
    this.imageChangedEvent = event;
  }

  imageCropped(event: CroppedEvent) {
    this.base64 = event.base64;
  }
}

When you choose a file from the file input, it will trigger fileChangeEvent. That event is then passed to the ngxPhotoEditor through imageChangedEvent which will load the image into the cropper. Everytime you release the mouse, the imageCropped event will be triggerd with the cropped image as a Base64, Blob in its payload.

API

All inputs are optional. Either the imageChangedEvent, imageBase64 or imageFile or imageUrl should be set to load an image into the cropper.

Inputs

NameTypeDefaultDescription
imageChangedEventFileEventThe change event from your file input
imageFileBlob(File)The file you want to change
imageBase64stringIf you don't want to use a file input, you can set a base64 image directly and it will be loaded into the cropper
imageURLstringIf you don't want to use a file input or a base64 you can set an URL to get the image from. If requesting an image from a different domain make sure Cross-Origin Resource Sharing (CORS) is allowed or the image will fail to load.
formatstringpngOutput format (png, jpeg, webp, bmp, ico) (not all browsers support all types, png is always supported, others are optional)
aspectRationumber1 / 1The width / height ratio (e.g. 1 / 1 for a square, 4 / 3, 16 / 9 ...)
resizeToWidthnumber0 (disabled)Cropped image will be resized to this width (in px)
resizeToHeightnumber0 (disabled)Cropped image will be resized to this height (in px) (will be ignored if resizeToWidth is set)
roundCropperbooleanfalseSet this to true for a round cropper. Resulting image will still be square, use border-radius: 100% on resulting image to show it as round.
imageQualitynumber92This only applies when using jpeg or webp as output format. Entering a number between 0 and 100 will determine the quality of the output image.
autoCropbooleantrueEnable to crop the image automatically when initialized.
autoCropAreanumber1 (80% of the image)A number between 0 and 1. Define the automatic cropping area size (percentage).
viewModenumber0Define the ViewMode of the cropper.
maskbooleantrueShow the black modal above the image and under the crop box.
guidesbooleantrueShow the dashed lines above the crop box.
centerIndicatorbooleantrueShow the center indicator above the crop box.
canvasHeightnumber400Cropper canvas height.
scalablebooleantrueEnable to scale the image.
zoomablebooleantrueEnable to zoom the image.
cropBoxMovablebooleantrueEnable to move the crop box by dragging.
cropBoxResizablebooleantrueEnable to resize the crop box by dragging.
modalSizestringnullModal Size. ('sm' / 'lg' / 'xl')
modalCenteredbooleanfalseModal Position Centered.
darkThemebooleantrueEnable Dark Theme.
imageSmoothingEnabledbooleantrueSmooth image output.
imageSmoothingQualitystringlowquality of image smoothing, one of "low" or "medium", or "high".

Outputs

NameTypeDescription
imageCroppedCroppedEventEmits an ImageCroppedEvent each time the image is cropped
loadImageFailedvoidEmits when a wrong file type was selected

Interfaces

ImageCroppedEvent

PropertyTypeDescription
base64stringBase64 string of the cropped image
filefile(Blob)Blob(File) of the cropped image

viewMode

  • Type: Number
  • Default: 0
  • Options:
    • 0: no restrictions
    • 1: restrict the crop box to not exceed the size of the canvas.
    • 2: restrict the minimum canvas size to fit within the container. If the proportions of the canvas and the container differ, the minimum canvas will be surrounded by extra space in one of the dimensions.
    • 3: restrict the minimum canvas size to fill fit the container. If the proportions of the canvas and the container are different, the container will not be able to fit the whole canvas in one of the dimensions.

Define the view mode of the cropper. If you set viewMode to 0, the crop box can extend outside the canvas, while a value of 1, 2 or 3 will restrict the crop box to the size of the canvas. A viewMode of 2 or 3 will additionally restrict the canvas to the container. Note that if the proportions of the canvas and the container are the same, there is no difference between 2 and 3.