Learn MVC Using Angular Image Crop

Introduction

This article demonstrates learning MVC using angular image crop using bootstrap file style in the visual studio 2017.

Angular Image Crop

When you upload the image in Angular JS, this image crop directive helps to reduce the image size by using different area cutters.

Features

  • Fix width / height in crop area
  • Fix crop area square /circle
  • Fix type of image format
  • Fix image quality

Following the steps you can use angular image crop in angular JS in MVC

  • Create MVC Project
  • Configure Angular image crop & Bootstrap filestyle
  • Work in Angular image crop

Create MVC Project

Open the visual studio 2017

Angular

Go to New menu >Click New & project. Now it will open New Project Window

Angular

You can select ASP.NET Web Application on Framework 4.5. Enter the name of project in “Solution name” textbox then click ok button.

Angular

One more window should be appearing. Select MVC Template in this popup & Click ok button. Now start cropping image.

Configure Angular image crop & Bootstrap filestyle

You can download the plug in from

Open the _Layout.cshtml and must refer the .js file from downloaded folder to this view page

  1. <script src="~/Plugin/angular/angular.min.js"></script>  
  2.   <script src="~/Plugin/angular-ui-router/release/angular-ui-router.min.js"></script>  
  3.   <script src="~/Plugin/ng-img-crop/compile/unminified/ng-img-crop.js"></script>  
  4.   <link href="~/Plugin/ng-img-crop/compile/unminified/ng-img-crop.css" rel="stylesheet" />  
  5.   <script src="~/Plugin/bootstrap-filestyle/src/bootstrap-filestyle.js"></script>    

Link your angular configurable file, whatever you given name

  1. <script src="~/App/App.module.js"></script>  
  2. <script src="~/App/App.config.js"></script>  
  3. <script src="~/App/CIController.js"></script>   

Angular Module

You will need to include the module as a dependency on your application. 

  1. var uiroute = angular  
  2. .module('uiroute', ['ui.router','ngImgCrop']);   

If you have any doubt in configuration, visit the following articles -

Work in Angular image crop

This tree control will work when you use the < img-crop > directive as html attributes. 

  1. <img-crop image="myImage"   
  2.                                   result-image="myCroppedImage"   
  3.                                   area-type="{{imgcropType}}">  
  4.                         </img-crop>   

Using bootstrap file style directive 

  1. <input id="fileInput" filestyle=""   
  2. type="file" data-class-button="btn btn-default"   
  3. data-class-input="form-control"   
  4. data-button-text="Upload"  class="form-control" />   

Html Code 

  1. <div ng-controller="CIController " class="container-fluid">  
  2.     <div class="row">  
  3.         <div class="col-md-3">  
  4.             <div class="panel">  
  5.                 <div class="panel-heading no-shadow">  
  6.                     <a href="" ng-click="reset()" class="pull-right">  
  7.                         <small class="fa fa-refresh text-muted"></small>  
  8.                     </a>Select an image file  
  9.                 </div>  
  10.                 <div class="panel-body">  
  11.                     <div class="form-group">  
  12.                         <input id="fileInput" filestyle="" type="file" data-class-button="btn btn-default" data-class-input="form-control" data-button-text="Upload"  class="form-control" />  
  13.                     </div>  
  14.                     <p class="pv">Crop type:</p>  
  15.                     <div class="btn-group btn-group-justified mb">  
  16.                         <label ng-click="imgcropTypes('square')"  class="btn btn-default">Square</label>  
  17.                         <label ng-click="imgcropTypes('circle')"  class="btn btn-default">Circle</label>  
  18.                     </div>  
  19.                     <br />  
  20.                     <div data-text="Cropped Image" class="imgcrop-preview">  
  21.                         <img ng-src="{{myCroppedImage}}" />  
  22.                     </div>  
  23.                 </div>  
  24.             </div>  
  25.         </div>  
  26.         <div class="col-md-9">  
  27.             <div class="panel">  
  28.                 <div class="panel-body">  
  29.                     <div class="imgcrop-area">  
  30.                         <img-crop image="myImage"   
  31.                                   result-image="myCroppedImage"   
  32.                                   area-type="{{imgcropType}}">  
  33.                         </img-crop>  
  34.                     </div>  
  35.                 </div>  
  36.             </div>  
  37.         </div>  
  38.     </div>  
  39. </div>    

CSS Code

Set the style in the image crop area design 

  1. .imgcrop-area {  
  2.     width: 100%;  
  3.     height: 410px;  
  4.     overflow: hidden;  
  5.     background: #e6e9ee;  
  6. }  
  7.   
  8. .imgcrop-preview {  
  9.     position: relative;  
  10.     width: 100%;  
  11.     height: 200px;  
  12.     margin: 0 auto;  
  13.     background: #e6e9ee;  
  14.     text-align: center;  
  15. }  
  16.   
  17.     .imgcrop-preview:after {  
  18.         content: attr(data-text);  
  19.         display: block;  
  20.         position: absolute;  
  21.         height: 50%;  
  22.         text-align: center;  
  23.         margin: auto;  
  24.         top: 0;  
  25.         left: 0;  
  26.         bottom: 0;  
  27.         right: 0;  
  28.         z-index: 0;  
  29.         color: #8394a9;  
  30.     }  
  31.   
  32.     .imgcrop-preview > img {  
  33.         position: relative;  
  34.         z-index: 1;  
  35.         max-width: 100%;  
  36.     }   

Angular Controller

Initiate the crop area as “circle” 

  1. $scope.reset = function () {  
  2.             $scope.myImage = '';  
  3.             $scope.myCroppedImage = '';  
  4.             $scope.imgcropType = 'circle';  
  5.         };  
  6.         $scope.imgcropTypes = function (mode)  
  7.         {  
  8.             $scope.imgcropType = mode;  
  9.         }   

Angular Directive

you can use bootstrap filestyle as angular directive .when you will select the image using filestyle, set the element to the <img-crop> directive 

  1. .directive('filestyle', filestyle);  
  2.   
  3.     function filestyle() {  
  4.   
  5.         controller.$inject = ['$scope''$element'];  
  6.         return {  
  7.             restrict: 'A',  
  8.             controller: controller  
  9.         };  
  10.   
  11.         function controller($scope, $element) {  
  12.             var options = $element.data();  
  13.             options.classInput = $element.data('classinput') || options.classInput;  
  14.   
  15.             $element.filestyle(options);  
  16.         }  
  17.     }   

Angular Controller

Write the attribute id on change read the data. 

  1. var handleFileSelect = function (evt) {  
  2.             var file = evt.currentTarget.files[0];  
  3.             var reader = new FileReader();  
  4.             reader.onload = function (evt) {  
  5.                 $scope.$apply(function ($scope) {  
  6.                     $scope.myImage = evt.target.result;  
  7.                 });  
  8.             };  
  9.             if (file)  
  10.                 reader.readAsDataURL(file);  
  11.         };  
  12.   
  13.         angular.element(document.querySelector('#fileInput')).on('change', handleFileSelect);   

Click F5 button and Run the Application. Now it will appear in browser and you can see the result.

Output 1

Angular

If you click upload button, it will open file selectable windows & select any image in there.

Output 2

Angular

Once open the image it will be load in the image preview with initiate the crop area as “Square”

Output 3

If you click circle button crop area change as “circle”

Angular

Conclusion

In this article, we have learned mvc using angular image crop using bootstrap file style. If you have any queries, please tell me through the comments section. Your comments are very valuable.

Happy Coding!...