7.2.1 • Published 4 months ago

ngx-image-cropper v7.2.1

Weekly downloads
58,936
License
MIT
Repository
github
Last release
4 months ago

Image cropper for Angular

example

StackBlitz

Installation

npm install ngx-image-cropper --save

Upgrade instructions

For a list of breaking changes and update instructions, go to releases. Only Angular 13+ is supported since image cropper version 6.0.0.

Example usage

Add the ImageCropperModule to the imports of the module which will be using the Image Cropper.

import { NgModule } from '@angular/core';
import { ImageCropperModule } from 'ngx-image-cropper';

@NgModule({
    imports: [
        ...
        ImageCropperModule
    ],
    declarations: [
        ...
    ],
    exports: [
        ...
    ],
    providers: [
        ...
    ]
})
export class YourModule {
}

Add the element to your HTML:

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

<image-cropper
    [imageChangedEvent]="imageChangedEvent"
    [maintainAspectRatio]="true"
    [aspectRatio]="4 / 3"
    format="png"
    (imageCropped)="imageCropped($event)"
    (imageLoaded)="imageLoaded($event)"
    (cropperReady)="cropperReady()"
    (loadImageFailed)="loadImageFailed()"
></image-cropper>

<img [src]="croppedImage" />

And add this to your ts file:

import { ImageCroppedEvent, LoadedImage } from 'ngx-image-cropper';
import { DomSanitizer } from '@angular/platform-browser';

export class YourComponent {
    imageChangedEvent: any = '';
    croppedImage: any = '';
    
    constructor(
      private sanitizer: DomSanitizer
    ) {
    }

    fileChangeEvent(event: any): void {
        this.imageChangedEvent = event;
    }
    imageCropped(event: ImageCroppedEvent) {
      this.croppedImage = this.sanitizer.bypassSecurityTrustUrl(event.objectUrl);
      // event.blob can be used to upload the cropped image
    }
    imageLoaded(image: LoadedImage) {
        // show cropper
    }
    cropperReady() {
        // cropper ready
    }
    loadImageFailed() {
        // show message
    }
}

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

API

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

Inputs

NameTypeDefaultDescription
imageChangedEventFileEventThe change event from your file input (set to null to reset the cropper)
imageFileBlob(File)The file you want to change (set to null to reset the cropper)
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.
imageAltTextstringAlternative text for uploaded image for accessibility compliance.
cropperFrameAriaLabelstring'Crop photo'Aria-label text for the focusable cropper frame element.
formatstringpngOutput format (png, jpeg, webp, bmp, ico) (not all browsers support all types, png is always supported, others are optional)
outputstringblobOutput type (blob or base64) (blob is the most performant)
aspectRationumber1 / 1The width / height ratio (e.g. 1 / 1 for a square, 4 / 3, 16 / 9 ...)
maintainAspectRatiobooleantrueKeep width and height of cropped image equal according to the aspectRatio
containWithinAspectRatiobooleanfalseWhen set to true, padding will be added around the image to make it fit to the aspect ratio
resizeToWidthnumber0 (disabled)Cropped image will be resized to at most this width (in px)
resizeToHeightnumber0 (disabled)Cropped image will be resized to at most this height (in px)
cropperStaticWidthnumber0 (disabled)Set Cropper Width and disable resize (in px)
cropperStaticHeightnumber0 (disabled)Set Cropper Height and disable resize (in px)
cropperMinWidthnumber0 (disabled)The cropper cannot be made smaller than this number of pixels in width (relative to original image's size) (in px)
cropperMinHeightnumber0 (disabled)The cropper cannot be made smaller than this number of pixels in height (relative to original image's size) (in px) (will be ignored if maintainAspectRatio is set)
cropperMaxWidthnumber0 (disabled)The cropper cannot be made bigger than this number of pixels in width (in px)
cropperMaxHeightnumber0 (disabled)The cropper cannot be made bigger than this number of pixels in height (in px)
initialStepSizenumber3 (px)The initial step size in pixels when moving the cropper using the keyboard. Step size can then be changed by using the numpad when the cropper is focused
onlyScaleDownbooleanfalseWhen the resizeToWidth or resizeToHeight is set, enabling this option will make sure smaller images are not scaled up
cropperCropperPositionTo be able to overwrite the cropper coordinates, you can use this input. Create a new object of type CropperPosition and assign it to this input. Make sure to create a new object each time you wish to overwrite the cropper's position and wait for the cropperReady event to have fired.
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.
autoCropbooleantrueWhen set to true, the cropper will emit an image each time the position or size of the cropper is changed. When set to false, you can call the crop method yourself (use @ViewChild to get access to the croppers methods).
alignImage'left' or 'center''center'Use this to align the image in the cropper either to the left or center.
backgroundColorstringUse this to set a backgroundColor, this is useful if you upload an image of a format with transparent colors and convert it to 'jpeg' or 'bmp'. The transparent pixels will then become the set color or the default value. Enter any string representing a CSS color (https://developer.mozilla.org/en-US/docs/Web/CSS/color_value).
hideResizeSquaresbooleanfalseDisables the resize-squares at the border of the cropper. This is mostly useful for Touch Devices where you can change the Size of the Cropper via Pinch-To-Zoom
disabledbooleanfalseDisables the component and prevents changing the cropper position
canvasRotationnumber0Rotate the canvas (1 = 90deg, 2 = 180deg...)
transformImageTransform{}Flip, rotate and scale image. (if allowMoveImage is enabled, make sure to use 2-way data binding [(transform)]="transform".
allowMoveImagebooleanfalseAllow background image to be moved. When enabled and using the transform input, make sure to use 2-way data binding for the transform input ([(transform)]="transform").
hiddenbooleanfalseSet to true to hide image cropper

CSS Variables

NameTypeDefaultDescription
--cropper-outline-colorstringrgba(255,255,255,0.3)The background color you see around the cropper
--cropper-overlay-colorstringrgba(255,255,255)The background color you see around the image

Outputs

NameTypeDescription
imageCroppedImageCroppedEventEmits an ImageCroppedEvent each time the image is cropped
imageLoadedLoadedImageEmits the LoadedImage when it was loaded into the cropper
cropperReadyDimensionsEmits when the cropper is ready to be interacted. The Dimensions object that is returned contains the displayed image size
startCropImagevoidEmits when the component started cropping the image
loadImageFailedvoidEmits when a wrong file type was selected (only png, gif and jpg are allowed)

Methods

To gain access to the image cropper's methods use @ViewChild(ImageCropperComponent) imageCropper: ImageCropperComponent;

NameReturnsDescription
crop(output?: 'blob' | 'base64')Promise or ImageCroppedEventCrops the source image to the current cropper position. Be sure to set autoCrop to false if you only wish to use this function directly. When output is set to blob, a Promise will be returned.

Interfaces

CropperPosition

PropertyTypeDescription
x1numberX position of first coordinate (in px)
y1numberY position of first coordinate (in px)
x2numberX position of second coordinate (in px)
y2numberY position of second coordinate (in px)

ImageTransform

PropertyTypeDescription
scalenumberScale image (1=normal, 2=2x zoom...)
rotatenumberRotation in degrees
flipHbooleanFlipped horizontally
flipVbooleanFlipped vertically
translateHnumberMove horizontally (in %)
translateVnumberMoved vertically (in %)
translateUnitnumberThe unit used for the translate (% or px) (default = %)

ImageCroppedEvent

PropertyTypeDescription
blobBlobBlob of the cropped image (only if output="blob")
objectUrlstringObject url pointing to the generated blob (only if output="blob")
base64stringBase64 string of the cropped image (only if output="base64")
widthnumberWidth of the cropped image
heightnumberHeight of the cropped image
cropperPositionCropperPositionPosition of the cropper when it was cropped relative to the displayed image size
imagePositionCropperPositionPosition of the cropper when it was cropped relative to the original image size
offsetImagePositionCropperPositionPosition of the cropper when it was cropped relative to the original image size without padding when containWithinAspectRatio is true

LoadedImage

PropertyTypeDescription
original.objectUrlstringObject url pointing to the original image
original.imageHTMLImageElementHTMLImageElement of the original image
original.sizeDimensionWidth and height of the original image
transformed.objectUrlstringObject url pointing to the transformed image
transformed.imageHTMLImageElementHTMLImageElement of the transformed image
transformed.sizeDimensionWidth and height of the transformed image
exifTransformExifTransformExif transformations read from original image
spm-acr-spa-uibrael-solarenginestongdai-bggangular-mat-uploaderabp-zero-templates-gabp-zero-template-gmanualabp-zero-templatabp-zero-templat-gabp-zero-template-gng-packagr-librarysportcrmngx-upload-img-to-firebase-storagengx-upload-img-to-firestorecointervu@everything-registry/sub-chunk-2284egy-sorobandtihystgame-chat@senaiplay/senaiplay-cropperzen-angularplmt-core-libraryxuihike-appp365-controlsperforma365-ctrlzm-zzzft-smart-libfwh-ui-component-angular-library@ran-ng/assetssaukaccountancysaukaccountantsabp-zero-jpacs11abp-zero-jpacs2abp-zero-laoxieabp-zero-manualabp-zero-manual-publishabp-zero-manual-templateabp-zero-sprinthubabp-zero-sprinthubtestingabp-zero-templat-cardsabp-zero-template-aabp-zero-template-ababp-zero-template-abcabp-zero-template-asdabp-zero-template-babp-zero-template-bcabp-zero-template-bcdabp-zero-template-bnmabp-zero-template-cabp-zero-template-cardingabp-zero-template-cardsabp-zero-template-ccmabp-zero-template-ccmcabp-zero-template-cdabp-zero-template-cdeabp-zero-template-chayabp-zero-template-chay1abp-zero-template-cmcabp-zero-template-cpabp-zero-template-dabp-zero-template-deabp-zero-template-defabp-zero-template-eabp-zero-template-efabp-zero-template-efgabp-zero-template-efraiabp-zero-template-fghabp-zero-template-ghabp-zero-template-ghiabp-zero-template-habp-zero-template-hiabp-zero-template-hijabp-zero-template-hjkabp-zero-template-hubabp-zero-template-iabp-zero-template-ijabp-zero-template-ijbabp-zero-template-ijkabp-zero-template-jabp-zero-template-jkabp-zero-template-jklabp-zero-template-kabp-zero-template-klabp-zero-template-klmabp-zero-template-komabp-antech-jpacsabp-jpacs-214abp-jpacs394abp-jpacs395abp-jpacs477abp-jpacs478abp-jpacs520abp-zero-carabp-zero-cardsabp-zero-jpacs04abp-zero-jpacs10abp-zero-sprinthubtesting2abp-zero-template-cmabp-zero-template-fabp-zero-template-fg
7.2.1

4 months ago

7.2.0

4 months ago

7.0.3

7 months ago

7.1.2

6 months ago

7.1.1

6 months ago

7.1.0

6 months ago

7.0.0

11 months ago

7.0.2

9 months ago

7.0.1

11 months ago

6.3.4

11 months ago

6.3.3

1 year ago

6.3.0

1 year ago

6.3.2

1 year ago

6.3.1

1 year ago

6.2.1

2 years ago

6.2.0

2 years ago

6.2.2

2 years ago

6.1.0

2 years ago

6.0.1

2 years ago

6.0.0

2 years ago

6.0.3

2 years ago

6.0.2

2 years ago

5.0.1

3 years ago

5.0.0

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

3.3.5

3 years ago

3.3.4

3 years ago

3.3.3

3 years ago

3.3.2

3 years ago

3.3.1

3 years ago

3.3.0

3 years ago

3.2.1

4 years ago

3.2.0

4 years ago

3.1.9

4 years ago

3.1.8

4 years ago

3.1.7

4 years ago

3.1.6

4 years ago

3.1.5

4 years ago

3.1.4

4 years ago

3.1.3

4 years ago

3.1.2

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.1

5 years ago

1.3.11

5 years ago

1.3.10

5 years ago

1.3.9

5 years ago

1.3.8

5 years ago

1.3.7

5 years ago

1.3.6

5 years ago

1.3.5

5 years ago

1.3.4

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.2.9

6 years ago

0.2.8

6 years ago

0.2.7

6 years ago

0.2.6

6 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.1.30

6 years ago

0.1.29

6 years ago

0.1.28

6 years ago

0.1.27

6 years ago

0.1.26

6 years ago

0.1.25

6 years ago

0.1.24

6 years ago

0.1.23

6 years ago

0.1.22

6 years ago

0.1.21

6 years ago

0.1.20

6 years ago

0.1.19

6 years ago

0.1.18

6 years ago

0.1.17

6 years ago

0.1.16

6 years ago

0.1.15

6 years ago

0.1.14

6 years ago

0.1.13

6 years ago

0.1.12

6 years ago

0.1.11

6 years ago

0.1.10

6 years ago

0.1.9

6 years ago

0.1.8

7 years ago

0.1.7

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago