Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Angular 14 Image Upload With Live Preview to Crop,Scale & Resize Image Using ngx-image-cropper in TS

Posted on January 17, 2023

 

 

Welcome folks today in this blog post we will be uploading images in angular 14 with live preview and we will be resizing ,cropping and scaling images in browser using ngx-image-cropper library in typescript. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to make a new angular app using the below command as shown below

 

 

ng new sampleapp

 

 

And after that you need to install the below libraries using the below command as shown below

 

 

npm i ngx-image-cropper

 

 

And after that you will see the below directory structure of the angular app as shown below

 

 

 

 

 

Now first of all you need to go to app.module.ts file and copy paste the below code

 

 

app.module.ts

 

 

TypeScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
 
import { ImageCropperModule } from 'ngx-image-cropper';
 
import { AppComponent } from './app.component';
 
 
 
@NgModule({
  imports:      [ BrowserModule, FormsModule, ImageCropperModule ],
  declarations: [ AppComponent],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

 

 

And now you need to go to app.component.html file and copy paste the below html code

 

 

app.component.html

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<input type="file" (change)="fileChangeEvent($event)" />
 
<image-cropper
    [imageChangedEvent]="imageChangedEvent"
    [maintainAspectRatio]="true"
    [aspectRatio]="4 / 3"
    format="png"
    (imageCropped)="imageCropped($event)"
    (imageLoaded)="imageLoaded()"
    (cropperReady)="cropperReady()"
    (loadImageFailed)="loadImageFailed()"
></image-cropper>
 
<img [src]="croppedImage" />

 

 

As you can see in the above html code we have the input field where we allow the user to select the image and then we have attached the onChange event listener when the file is changed. And then we have the actual image-cropper widget to actually show the live preview of the selected image and inside it we have different attributes to resize,crop and scale image at the same time and live preview will be seen by the user instantly and you can download the image.

 

And now we need to go to app.component.ts file and copy paste the following code

 

 

app.component.ts

 

 

TypeScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { Component } from '@angular/core';
import { ImageCroppedEvent } from 'ngx-image-cropper';
 
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';
 
  imageChangedEvent: any = '';
  croppedImage: any = '';
    
  fileChangeEvent(event: any): void {
      this.imageChangedEvent = event;
  }
  imageCropped(event: ImageCroppedEvent) {
      this.croppedImage = event.base64;
  }
  imageLoaded() {
      // show cropper
  }
  cropperReady() {
      // cropper ready
  }
  loadImageFailed() {
      // show message
  }
}

 

 

As you can see we have different methods for various operations that are listed above. And then we are also getting the base64 code of the selected image.

 

 

 

 

Recent Posts

  • Angular 14/15 JWT Login & Registration Auth System in Node.js & Express Using MongoDB in Browser
  • Build a JWT Login & Registration Auth System in Node.js & Express Using MongoDB in Browser
  • React-Admin Example to Create CRUD REST API Using JSON-Server Library in Browser Using Javascript
  • Javascript Papaparse Example to Parse CSV Files and Export to JSON File and Download it as Attachment
  • Javascript Select2.js Example to Display Single & Multi-Select Dropdown & Fetch Remote Data Using Ajax in Dropdown
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme