MCPcopy Index your code
hub / github.com/Mawi137/ngx-image-cropper

github.com/Mawi137/ngx-image-cropper @9.0.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release 9.0.0 ↗ · + Follow
132 symbols 268 edges 27 files 0 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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 17.3+ is supported since version 9.0.0.

Example usage

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" />

In your ts file, declare the usage of ImageCropperComponent and add the following methods:

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

@Component({
  standalone: true,
  imports: [ImageCropperComponent]
})
export class YourComponent {
    imageChangedEvent: Event | null = null;
    croppedImage: SafeUrl  = '';

    constructor(
      private sanitizer: DomSanitizer
    ) {
    }

    fileChangeEvent(event: Event): 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.

Alternatives

If you find ngx-image-cropper doesn't cover your needs we recommend taking a look at Pintura

Pintura features cropping, rotating, flipping, filtering, annotating, and lots of additional functionality to cover all your image and video editing needs on both mobile and desktop devices.

Pintura example

Learn more about Pintura Image Editor

API

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

Inputs

Name Type Default Description
imageChangedEvent FileEvent The change event from your file input (set to null to reset the cropper)
imageFile Blob(File) The file you want to change (set to null to reset the cropper)
imageBase64 string If you don't want to use a file input, you can set a base64 image directly and it will be loaded into the cropper
imageURL string If 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.
imageAltText string Alternative text for uploaded image for accessibility compliance.
cropperFrameAriaLabel string 'Crop photo' Aria-label text for the focusable cropper frame element.
format string png Output format (png, jpeg, webp, bmp, ico) (not all browsers support all types, png is always supported, others are optional)
output string blob Output type (blob or base64) (blob is the most performant)
aspectRatio number 1 / 1 The width / height ratio (e.g. 1 / 1 for a square, 4 / 3, 16 / 9 ...)
maintainAspectRatio boolean true Keep width and height of cropped image equal according to the aspectRatio
containWithinAspectRatio boolean false When set to true, padding will be added around the image to make it fit to the aspect ratio
resizeToWidth number 0 (disabled) Cropped image will be resized to at most this width (in px)
resizeToHeight number 0 (disabled) Cropped image will be resized to at most this height (in px)
cropperStaticWidth number 0 (disabled) Set Cropper Width and disable resize (in px)
cropperStaticHeight number 0 (disabled) Set Cropper Height and disable resize (in px)
cropperMinWidth number 0 (disabled) The cropper cannot be made smaller than this number of pixels in width (relative to original image's size) (in px)
cropperMinHeight number 0 (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)
cropperMaxWidth number 0 (disabled) The cropper cannot be made bigger than this number of pixels in width (in px)
cropperMaxHeight number 0 (disabled) The cropper cannot be made bigger than this number of pixels in height (in px)
initialStepSize number 3 (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
onlyScaleDown boolean false When the resizeToWidth or resizeToHeight is set, enabling this option will make sure smaller images are not scaled up
cropper CropperPosition To 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.
roundCropper boolean false Set 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.
imageQuality number 92 This 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.
autoCrop boolean true When 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.

Extension points exported contracts — how you extend this code

Core symbols most depended-on inside this repo

Shape

Method 94
Function 17
Class 10
Interface 10
Enum 1

Languages

TypeScript100%

Modules by API surface

projects/ngx-image-cropper/src/lib/component/image-cropper.component.ts38 symbols
projects/demo-app/src/app/app.component.ts26 symbols
projects/ngx-image-cropper/src/lib/component/cropper.state.ts16 symbols
projects/ngx-image-cropper/src/lib/services/load-image.service.ts15 symbols
projects/ngx-image-cropper/src/lib/services/crop.service.ts10 symbols
projects/ngx-image-cropper/src/lib/utils/cropper-position.utils.ts8 symbols
projects/ngx-image-cropper/src/lib/utils/keyboard.utils.ts3 symbols
projects/ngx-image-cropper/src/lib/utils/exif.utils.ts3 symbols
projects/ngx-image-cropper/src/lib/interfaces/move-start.interface.ts2 symbols
projects/ngx-image-cropper/src/lib/utils/resize.utils.ts1 symbols
projects/ngx-image-cropper/src/lib/utils/percentage.utils.ts1 symbols
projects/ngx-image-cropper/src/lib/utils/blob.utils.ts1 symbols

For agents

$ claude mcp add ngx-image-cropper \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page