Angular crop image, resize, zoom, flip and rotate feature before image upload with example

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.

Angular crop image, zoom, resize, flip and rotate before image upload

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.

With the rise of digital media, the need for efficient and effective ways to manage images is greater than ever. Fortunately, Angular developers now have access to a range of powerful image processing tools that make it easier than ever to manipulate images with these photo cutting tools in their applications. 

In this article we will look at Ngx-image-cropper example that provides a comprehensive guide to using the NGX Image Cropper library to create powerful image editing features such as crop, resize, zoom, flip and rotate before image upload. It includes detailed examples and step-by-step instructions to

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

With the Angular CLI, developers can quickly and easily set up an Angular environment. The following instructions will help you set up an Angular CLI in no time. 


 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

The NGX Image Cropper library enables developers to create a great image crop experience for users. It can be easily installed in any Angular application with minimal effort. With this library, developers have access to all the necessary tools necessary to create an effective and efficient interface to cut out image.

The installation of the NGX Image Cropper library is simple and straightforward. To install the image cropper plugin, you will need to add the following command to your NPM package file:
 
  npm install ngx-image-cropper --save  

Register NGX Image Cropper module - ImageCropperModule 

After installation, you will also need to import the module into your app's root modules so that it can be used in other components throughout your application. 

Additionally, you will need to configure the component within your parent component's template, so that it is visible when the page loads up in the browser window.


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 {}

By registering the NGX Image Cropper module in an angular application, developers can provide their users with a feature-rich image cropping experience. 

This module allows users to easily select and crop images within their applications, providing them with the capability to control the exact size and shape of an image before it is deployed. 

Integerate the code to crop the image


Integrating an image cropper into any Angular project can be a time-consuming task. NGX Image Cropper is here to make it easy for developers. This open source library provides the flexibility and options like image cutter and crop a gif,  those needed to quickly add an intuitive crop image experience into any web application. 

Complete ngx-image-cropper example:
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');
    }
}  
NGX Image Cropper is fully customizable, allowing developers to customize the look and feel of the interface while maintaining all core features. Developers are able to tweak many aspects of the UI such as background color, border widths, and more with ease. 

Once configured, users are able to easily crop images using simple drag and drop gestures. Additionally, this library supports photo cutting for multiple file formats including PNG, JPEG and you can even crop a gif with it and make it useful for a variety of projects requiring image editing capabilities. 

add the following code in the app.component.html


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

This library is also fully responsive and can be used on both mobile devices as well as desktop computers without any issues.

Zoom in and out image with angular

Ngx-image-cropper includes support for zoom in and out for any image in angular. what we need to do is to add the transform property to image-cropper.

  zoomOut() {
    this.scale -= .1;
    this.imageTransform = {
      ...this.imageTransform,
      scale: this.scale
    };
  }

  zoomIn() {
    this.scale += .1;
    this.imageTransform = {
      ...this.imageTransform,
      scale: this.scale
  };

Once the area has been selected, users can then use the mouse wheel or pinch gestures on touchscreens to zoom in and out of their selection area until it is just right. After that, they can apply various filters like rotation adjustment before saving the final result.

The library also supports undo/redo actions which makes it easy to correct mistakes while editing images.

<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

NGX Image Cropper has the ability to perform basic image edits including photo cutting, flipping, rotating and scaling. You can easiliy flip image using angular.

You can flip and rotate image with ngx-image-croper using angular is an effective way to modify images within a web application. With the help of the open source library, NGX Image Cropper, developers can easily implement this feature into their code.

The code inside app.component.html looks something like this:

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

And in the app.component.ts file , add the following code:

  
  rotateLeft() {
    this.canvasRotation--;
  }

  rotateRight() {
    this.canvasRotation++;
  }

  flipHorizontal() {
    this.imageTransform = {
      ...this.imageTransform,
      flipH: !this.imageTransform.flipH
    };
  }

  flipVertical() {
    this.imageTransform = {
      ...this.imageTransform,
      flipV: !this.imageTransform.flipV
    };
  }

When using this tool, users can access a variety of options such as horizontal or vertical flip, clockwise or counterclockwise rotation angles of 90 degrees or 180 degrees and scaling options that range from 50% to 400%. 

Using NGX Image Cropper, developers can create an intuitive user experience as they have full control over how users interact with the image cropping system. It also offers multiple options for manipulating images, including scaling, resizing, rotating, and flipping them in any direction.
By using these features, it's simple for users to adjust their images for any project.

Checkout the output


As th result of the code that we written leads to this output.
Angular crop image, resize, zoom, flip and rotate feature

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.

Summary

In conclusion, we have implemented the Angular crop image, resize, zoom, flip and rotate features that can be used to improve the overall user experience on an application. It helps users to modify their images effortlessly before uploading them on the application. In addition, this feature is simple to use and fast in terms of implementation. 

The example provided in this article was an easy to follow guide of how to implement the feature on your site. With a few lines of code you can make sure that your users can have all of the control they need when it comes to their images.

Therefore, any developer looking for a way to give users an easy way of customizing their images should consider implementing this feature.

Related Articles

Post a Comment

0 Comments