Having the ability to crop image, resize, zoom, flip and rotate images before uploading them in angular can be extremely useful in certain applications. In this article, we will discuss how to use the angular crop image feature to edit images before they are uploaded to a website or application.
We will include step-by-step instructions on how to do so, as well as an example of the implementation. This article will be beneficial for developers who need a quick way to modify and upload images efficiently.
Learning Objectives
- Set Up Angular Environment with Angular CLI
- Install NGX Image Cropper library
- Register NGX Image Cropper module - ImageCropperModule
- Integerate the code to crop the image
- Zoom in and out image
- Flip and rotate image with angular
- Check the output
Set Up Angular Environment with Angular CLI
npm install -g @angular/cli
Angular 15 is released , you can install the latest angular version simply following these steps:
ng update @angular/core@15 @angular/cli@15
Create a brand new angular application by this command
ng new test-app
// and redirect to the app
cd test-app
Install NGX Image Cropper library
npm install ngx-image-cropper --save
Register NGX Image Cropper module - ImageCropperModule
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { ImageCropperModule } from 'ngx-image-cropper';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, ImageCropperModule],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}
Integerate the code to crop the image
import { Component } from '@angular/core';
import { Dimensions, ImageCroppedEvent, ImageTransform } from 'ngx-image-cropper';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
imageChangedEvnt: any = '';
croppedImage: any = '';
canvasRotation = 0;
rotation = 0;
scale = 1;
showCropper = false;
imageTransform: ImageTransform = {};
onFileChange(event: any): void {
this.imageChangedEvnt = event;
}
imageCropped(event: ImageCroppedEvent) {
this.croppedImage = event.base64;
}
imageLoaded() {
this.showCropper = true;
}
loadImageFailed() {
console.log('Load failed');
}
}
<input type="file" (change)="onFileChange($event)" />
<br />
<br />
<div>
<image-cropper
[resizeToWidth]="256"
[cropperMinWidth]="128"
[imageChangedEvent]="imageChangedEvnt"
[aspectRatio]="4 / 3"
format="png"
(imageCropped)="imageCropped($event)"
(imageLoaded)="imageLoaded()"
(loadImageFailed)="loadImageFailed()"
></image-cropper>
</div>
<img [src]="croppedImage" [style.border]="croppedImage ? '1px solid black' : 'none'" />
Zoom in and out image with angular
zoomOut() {
this.scale -= .1;
this.imageTransform = {
...this.imageTransform,
scale: this.scale
};
}
zoomIn() {
this.scale += .1;
this.imageTransform = {
...this.imageTransform,
scale: this.scale
};
<image-cropper
[resizeToWidth]="256"
[cropperMinWidth]="128"
[imageChangedEvent]="imageChangedEvnt"
[aspectRatio]="4 / 3"
[transform]="imageTransform"
format="png"
(imageCropped)="imageCropped($event)"
(imageLoaded)="imageLoaded()"
(loadImageFailed)="loadImageFailed()"
></image-cropper>
<button (click)="zoomOut()">Zoom -</button> <button (click)="zoomIn()">Zoom +</button>
Flip and rotate image with angular
<button (click)="rotateLeft()">Rotate left</button>
<button (click)="rotateRight()">Rotate right</button>
<button (click)="flipHorizontal()">Flip horizontal</button>
<button (click)="flipVertical()">Flip vertical</button>
<image-cropper
[resizeToWidth]="256"
[cropperMinWidth]="128"
[imageChangedEvent]="imageChangedEvnt"
[aspectRatio]="4 / 3"
[canvasRotation]="canvasRotation"
[transform]="imageTransform"
format="png"
(imageCropped)="imageCropped($event)"
(imageLoaded)="imageLoaded()"
(loadImageFailed)="loadImageFailed()"
></image-cropper>
rotateLeft() {
this.canvasRotation--;
}
rotateRight() {
this.canvasRotation++;
}
flipHorizontal() {
this.imageTransform = {
...this.imageTransform,
flipH: !this.imageTransform.flipH
};
}
flipVertical() {
this.imageTransform = {
...this.imageTransform,
flipV: !this.imageTransform.flipV
};
}
Checkout the output
The library supports almost every features related to images including crop a JPEG, photo resize, flip a photo and even crop a GIF files in angular so that users don’t have to worry about compatibility issues when manipulating images in Angular apps.
0 Comments