Welcome folks today in this blog post we will be using the ngx-image-viewer
library to build mini image editor
to drag zoom and rotate
images using typescript in browser. 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
project using the below command as shown below
ng new sampleproject
cd sampleproject
And after that you need to install the below libraries
using the below command as shown below
npm i ngx-image-viewer
And after that you will see the below directory
structure of the angular app as shown below
First of all you need to go to app.module.ts
file and copy paste the following code
app.module.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppComponent } from './app.component'; import { ImageViewerModule } from "ngx-image-viewer"; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, ImageViewerModule.forRoot() ], providers: [], bootstrap: [AppComponent] }) export class AppModule{} |
And now we need to go to app.component.html
file and copy paste the following code
app.component.html
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> <div class="container"> <div class="row"> <div class="col"> <div class="image-viewer-container" [class.fullscreen]="fullscreen"> <div class="image-viewer"> <ngx-image-viewer #imageViewer [src]="images" [(index)]="imageIndex"></ngx-image-viewer> </div> </div> </div> </div> </div> |
As you can see in the above html
code we are including the font-awesome
cdn for the icons and then we have the ngx-image-viewer
directive to embed the image-editor
with controls as well to zoom and rotate images. And here we are attaching the images
array which will hold the images
to be edited
in the image editor.
And now we need to go to app.component.ts
file and copy paste the below code
app.component.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import { Component, ViewChild } from '@angular/core'; import { ImageViewerComponent } from 'ngx-image-viewer'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], }) export class AppComponent { @ViewChild('imageViewer') viewer!: ImageViewerComponent; fullscreen: boolean = false; imageIndex: number = 0; images: Array<string> = [ 'https://procodestore.com/wp-content/uploads/2021/03/164508084_271381191136191_654097929788476286_n.jpg', 'https://freemediatools.com/img/profile.jpg' ]; } |
As you can see in the above typescript
code we have the images
array where you need to store the url's
of the images. And then we have also have other variables for fullscreen
inside the image editor. And then we are declaring the reference of the image editor using the ViewChild
decorator.