

Angular 12 Crop Image Preview Before Upload using ngx-image-cropper
source link: https://www.laravelcode.com/post/angular-12-crop-image-preview-before-upload-using-ngx-image-cropper
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Angular 12 Crop Image Preview Before Upload using ngx-image-cropper
In this article, we will discuss the angular 10 crop image afore upload. This post will give you a simple example of an angular 11/10 cropper example. you'll learn image upload with crop in angular 10. This article goes into detail on angular 10 image upload with crop utilizing cropper.
If you require to utilize image upload with a crop then you can facilely use the ngx-image-cropper
npm package. it will provide you Cropping, Zooming, Scaling, and Preview functionality while uploading time. it's very facilely used with your angular 10 application.
In this example, i will give you step by step explication of how you can image upload in a crop in angular 10 application. you can withal optically discern bellow screenshot for demo.
Step 1: Create New App
You can easily create your angular app using bellow command:
ng new ngImageCrop
Step 2: Install npm Packages
In this step, we will install the ngx-image-cropper
npm package for uploading the image crop function in angular. so let's run both commands:
npm install ngx-image-cropper --save
Step 3: Import ImageCropperModule
Now, here we will import ImageCropperModule from ngx-image-cropper
and then we add on declarations part. so let's update the app.module.ts
file as like bellow:
src/app/app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { ImageCropperModule } from 'ngx-image-cropper';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
ImageCropperModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Step 4: Update Component ts File
Here, we will update app.component.ts file here, in this file we will write fileChangeEvent(), imageCropped(), imageLoaded(), cropperReady() and loadImageFailed() that provided by ngx-image-cropper.
You can update as bellow app.component.ts file.
src/app/app.component.ts
import { Component } from '@angular/core';
import { ImageCroppedEvent } from 'ngx-image-cropper';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'ngImageCrop';
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 */
}
}
Step 5: Update HTML File
I used bootstrap class on this form.
Here, we will update the HTML file as like below, so update it as like bellow:
src/app/app.component.html
<div class="container">
<div class="card">
<div class="card-header">
Anguler 11 Crop Image Example - HackTheStuff
</div>
<div class="card-body">
<input type="file" (change)="fileChangeEvent($event)" />
<div class="row" style="margin-top: 15px;">
<div class="text-center col-md-8">
<h5>Crop Image</h5>
<image-cropper
[imageChangedEvent]="imageChangedEvent"
[maintainAspectRatio]="true"
[aspectRatio]="4 / 4"
[resizeToWidth]="256"
format="png"
(imageCropped)="imageCropped($event)"
(imageLoaded)="imageLoaded()"
(cropperReady)="cropperReady()"
(loadImageFailed)="loadImageFailed()"></image-cropper>
</div>
<div class="text-center col-md-4">
<h5>Preview</h5>
<img [src]="croppedImage" />
</div>
</div>
</div>
</div>
</div>
i hope you like this article.
Author : Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
Recommend
-
15
-
41
November 27, 2020 Image Upload in Angular with Preview & Progress Angular
-
6
How to preview image before upload using jQuery - FileReader() Satinder Singh / February 06, 2021 /
-
20
PHP - How to Crop Image Before Uploading using Cropper Js? Ad byValueimpression In this tutorial, you will learn Php cropper js example. this...
-
7
Show Image Preview Before Upload in VueJs 1941 views 1 year ago VueJs If you optate to show image preview a...
-
7
react-native-instagram-like-image-cropper The component like instagram image cropper Preview Getting started First, install peer packages Second, install package npm install react-nat...
-
4
Laravel 8 - Image Resize Before Upload 1076 views 6 months ago Laravel I am able to explain one by one article...
-
9
HTML5 Resize image before upload without ajax Allows to resize a file input image before upload to the server. The resized image will be attached to original file input. Then you can submit your form without ajax or handl...
-
13
How to Crop Image before Upload using Cropper Js in Laravel 7.x 46109 views 2 years ago Laravel Hi Guys, In this t...
-
5
In this tutorial we'll be exploring how to build a robust Image Control Component in Angular that enables users to crop their photos to preferred dimensions right before they get uploaded to the server. If you've ever wondered how to do this, you'...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK