Git Product home page Git Product logo

Comments (4)

benlooi avatar benlooi commented on July 17, 2024

OK! Finally did it correctly! Here's how you do multiple croppers on the same page. I'll post the corresponding HTML5 in the next post.

.controller ('addAlbumCtrl', function ($scope,$timeout,Cropper){


    var file, file2, data, data2;

/**
 * Method is called every time file input's value changes.
 * Because of Angular has not ng-change for file inputs a hack is needed -
 * call `angular.element(this).scope().onFile(this.files[0])`
 * when input's event is fired.
 */
$scope.onFile = function(blob) {
    Cropper.encode((file = blob)).then(function(dataUrl) {
    $scope.dataUrl = dataUrl;
    $timeout(showCropper);  // wait for $digest to set image's src
  });
};

//defined a second scope variable for the second input file, which we will call in the HTML
//also assigned dataUrl2 to the 2nd file blob. Noticed I also changed the $timeout to showCropper2 as defined at the end of the controller.

      $scope.onFile2 = function(blob) {
     Cropper.encode((file = blob)).then(function(dataUrl) {
      $scope.dataUrl2 = dataUrl;
      $timeout(showCropper2);  // wait for $digest to set image's src
    });
  };

// When there is a cropped image to show encode it to base64 string and
// use as a source for an image element.

   $scope.preview = function() {
        if (!file || !data) return;
        Cropper.crop(file, data).then(Cropper.encode).then(function(dataUrl) {
         ($scope.preview || ($scope.preview = {})).dataUrl = dataUrl;
        });
  };

//defined a second preview2 for the second input file. so, ng-click here will be preview2().

 $scope.preview2 = function() {
      if (!file || !data) return;
      Cropper.crop(file, data).then(Cropper.encode).then(function(dataUrl) {
       ($scope.preview2 || ($scope.preview2 = {})).dataUrl = dataUrl;
    });
      };


  $scope.scale = function(width) {
    Cropper.crop(file, data)
      .then(function(blob) {
       return Cropper.scale(blob, {width: width});
      })
      .then(Cropper.encode).then(function(dataUrl) {
        ($scope.preview || ($scope.preview = {})).dataUrl = dataUrl;
      });
  }

  $scope.scale2 = function(width) {
    Cropper.crop(file, data)
      .then(function(blob) {
       return Cropper.scale(blob, {width: width});
      })
     .then(Cropper.encode).then(function(dataUrl) {
       ($scope.preview2 || ($scope.preview2 = {})).dataUrl = dataUrl;
      });
  }

//specified where to show the preview. Take note it is a JQuery selector, so if you are using a class, you need to include the '.' prefix. You can use id as well, and change it to a hashtag sign

     $scope.options = {
            maximize: true,
           preview:'.preview-container',
         aspectRatio: 3/1,
       done: function(dataNew) {
       data = dataNew;
    }
  };

//for the second preview, give it a different name. See HTML code in next post.

      $scope.options_2 = {
       maximize: true,
      aspectRatio: 3/1,
      preview:'.preview-thumbnail',
       done: function(dataNew) {
       data = dataNew;
      }
     };
  • Showing (initializing) and hiding (destroying) of a cropper are started by
  • events. The scope of the ng-cropper directive is derived from the scope of
  • the controller. When initializing the ng-cropper directive adds two handlers
  • listening to events passed by ng-show & ng-hide attributes.
  • To show or hide a cropper $broadcast a proper event.
    */
    $scope.showEvent = 'show';
    $scope.hideEvent = 'hide';

//showEvent2 and hideEvent2 is for the second input file

      $scope.showEvent2 = 'show';
      $scope.hideEvent2 = 'hide';

     function showCropper() { $scope.$broadcast($scope.showEvent); }
     function hideCropper() { $scope.$broadcast($scope.hideEvent); }

//showCropper2 and hideCropper2 is for 2nd input file

    function showCropper2() { $scope.$broadcast($scope.showEvent2); }
     function hideCropper2() { $scope.$broadcast($scope.hideEvent2); }

        })

from ngcropper.

benlooi avatar benlooi commented on July 17, 2024

Here's my HTML5

<section>
<div class="form-group">
<label>
    Album Art (900 x 300 px)
     <form>
     <input type="file" onchange="angular.element(this).scope().onFile(this.files[0])">
  <button ng-click="preview()">Show preview</button>
  <button ng-click="scale(200)">Scale to 200px width</button>
  <label>Disabled <input type="checkbox" ng-model="options.disabled"></label>

  <br />

  <div ng-if="dataUrl" class="img-container">
    <img ng-if="dataUrl" ng-src="{{dataUrl}}" width="900"
     ng-cropper ng-show="{{showEvent}}" ng-hide="{{hideEvent}}" ng-options="options">
  </div>

  <div class="preview-container">
    <img ng-if="preview.dataUrl" ng-src="{{preview.dataUrl}}">
  </div>



</div>

   </form>




<div class="form-group">
         <input type="file" onchange="angular.element(this).scope().onFile2(this.files[0])">
  <button ng-click="preview2()">Show preview</button>
  <button ng-click="scale(200)">Scale to 200px width</button>
  <label>Disabled <input type="checkbox" ng-model="options.disabled"></label>

  <br />

  <div ng-if="dataUrl2" class="img-thumb-container">
    <img ng-if="dataUrl2" ng-src="{{dataUrl2}}" width="900"
     ng-cropper ng-show="{{showEvent2}}" ng-hide="{{hideEvent2}}" ng-options="options_2">
  </div>

  <div class="preview-thumbnail">
    <img ng-if="preview2.dataUrl" ng-src="{{preview2.dataUrl}}">
  </div>


 <!--  <input type="file" ng-file-select="onFileSelect($files)" multiple> -->

</div>
</section>

And the CSS for the previews. DO note the previews have to be the same aspect ratio as what you specified in the options. I sent mine to 3 / 1.

.preview-container {
        overflow:hidden;
        width:900px;
        height:300px;
    }
    .preview-thumbnail {
        overflow:hidden;
        width:900px;
        height:300px;
    }

from ngcropper.

muhammadn avatar muhammadn commented on July 17, 2024

You did not use file2 or data2 so question: why did you declare those two variables?

EDIT: you don't need those two variables for it to work.

from ngcropper.

grantwhitaker06 avatar grantwhitaker06 commented on July 17, 2024

@benlooi helped me out was tearing my hair out thanks buddy.

from ngcropper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.