What is ngx-image-cropper?
The ngx-image-cropper is an Angular library that provides a highly customizable image cropping tool. It allows users to crop images in various shapes and sizes, and offers a range of features to enhance the image cropping experience.
What are ngx-image-cropper's main functionalities?
Basic Image Cropping
This feature allows users to crop images while maintaining a specific aspect ratio. The cropped image is emitted in the specified format (e.g., PNG).
<image-cropper [imageChangedEvent]="imageChangedEvent" [maintainAspectRatio]="true" [aspectRatio]="4 / 3" format="png" (imageCropped)="imageCropped($event)"></image-cropper>
Free Aspect Ratio Cropping
This feature allows users to crop images without maintaining a fixed aspect ratio, providing more flexibility in selecting the crop area.
<image-cropper [imageChangedEvent]="imageChangedEvent" [maintainAspectRatio]="false" format="jpeg" (imageCropped)="imageCropped($event)"></image-cropper>
Round Cropping
This feature allows users to crop images in a circular shape, which is useful for profile pictures and other round image requirements.
<image-cropper [imageChangedEvent]="imageChangedEvent" [maintainAspectRatio]="true" [aspectRatio]="1 / 1" [roundCropper]="true" format="png" (imageCropped)="imageCropped($event)"></image-cropper>
Image Rotation
This feature allows users to rotate the image before cropping, ensuring the image is properly oriented.
<image-cropper [imageChangedEvent]="imageChangedEvent" [maintainAspectRatio]="true" [aspectRatio]="4 / 3" format="png" (imageCropped)="imageCropped($event)" (imageLoaded)="imageLoaded()" (cropperReady)="cropperReady()" (loadImageFailed)="loadImageFailed()"></image-cropper>
Other packages similar to ngx-image-cropper
cropperjs
Cropper.js is a JavaScript image cropping library that provides a rich set of features for cropping images. It is highly customizable and can be used with various frameworks, including Angular. Compared to ngx-image-cropper, Cropper.js offers more advanced features and greater flexibility but requires more setup and integration effort.
react-image-crop
React Image Crop is a library specifically designed for React applications. It provides a simple and easy-to-use interface for cropping images. While it offers similar functionalities to ngx-image-cropper, it is tailored for React rather than Angular, making it a better choice for React developers.
vue-cropper
Vue Cropper is a Vue.js component for image cropping. It offers a range of features similar to ngx-image-cropper but is designed for use with Vue.js. It provides a seamless integration with Vue applications and is a good alternative for developers working with the Vue framework.
Image cropper for Angular
Plunker
Installation
npm install ngx-image-cropper --save
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"
[resizeToWidth]="128"
format="png"
(imageCropped)="imageCropped($event)"
></image-cropper>
<img [src]="croppedImage" />
And add this to your ts file:
imageChangedEvent: any = '';
croppedImage: any = '';
fileChangeEvent(event: any): void {
this.imageChangedEvent = event;
}
imageCropped(image: string) {
this.croppedImage = image;
}
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 triggerd with the cropped image as a Base64 string in its payload.
API
Inputs
imageChangedEvent
- The change event from your file inputformat
- Output format (png, jpeg, webp, bmp, ico) (default = png) (not all browsers support all types, png is always supported, others are optional)maintainAspectRatio
- Keep width and height of cropped image equal according to the aspectRatio (default = true)aspectRatio
- The width / height ratio (e.g. 1 / 1 for a square, 4 / 3, 16 / 9 ...) (default = 1)resizeToWidth
- Cropped image will be resized to this width (in px) (default = 0 (no resizing))imageBase64
- 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
Outputs
imageCropped
- Emits a Base64 string of the cropped image each time it is croppedimageLoaded
- Emits when the image was loaded into the cropperloadImageFailed
- Emits when a wrong file type was selected (only png, gif and jpg are allowed)