Comments (4)
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;
});
}
- Object is used to pass options to initalize a cropper.
- More on options - https://github.com/fengyuanchen/cropper#options
//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.
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.
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.
@benlooi helped me out was tearing my hair out thanks buddy.
from ngcropper.
Related Issues (20)
- ngCropper doesn't work in Angular Modal HOT 3
- Not working with jQuery 2.2.0 HOT 3
- Error when not specifying 'build' option HOT 1
- ngCropper and angular 1.5 HOT 6
- Can not move the crop area horizontally
- Get main image not from input field HOT 2
- ngCropper on load new image -> not working HOT 1
- dragStart event preview
- Change aspectRatio on click
- Start cropper from img src
- maximize:true does not work all image resolutions
- How can trigger zoom event? HOT 4
- ngCropper error element.cropper is not a function HOT 1
- Not working with Angular 1.5.8 and jQuery 2.2.4 HOT 2
- Not Working with Angular js 1.6.1
- cannot upload new images again and again HOT 2
- Accessing get data method?
- Problem with EXIF Image Orientation
- Is there support for circular area selection when cropping HOT 1
- Please add license file HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ngcropper.