Hello,
I've been trying to understand the code. It works well with one input file and cropper. I can't seem to do multiple files and croppers.
How does onFile work? If I have 2 input files, do I
a) call a different function, like onFile2, or
b) refer to the next file object in array, i.e., file[1]?
Confused.
How about the preview? Do I give the div a different class and pass it as an option, e.g $scope.option_2 = {preview:"some_other_class"}, then in the attribute, refer to it as ng-options="option_2".
Any help would be appreciated. Thanks in advance.
Here is my (obviously faulty) code:
JS:
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 -
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.
//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
Hello, I've been trying to understand the code. It works well with one input file and cropper. I can't seem to do multiple files and croppers.
How does onFile work? If I have 2 input files, do I a) call a different function, like onFile2, or b) refer to the next file object in array, i.e., file[1]?
Confused.
How about the preview? Do I give the div a different class and pass it as an option, e.g $scope.option_2 = {preview:"some_other_class"}, then in the attribute, refer to it as ng-options="option_2".
Any help would be appreciated. Thanks in advance.
Here is my (obviously faulty) code:
JS:
/**
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 }); }; $scope.onFile2 = function(blob) { Cropper.encode((file = blob)).then(function(dataUrl2) { $scope.dataUrl2 = dataUrl2; $timeout(showCropper); // wait for $digest to set image's src }); };
/**
use as a source for an image element. */
$scope.options_2 = { preview:"preview-thumbnail", maximize: true, aspectRatio: 3/1, done: function(dataNew) { data = dataNew; } }; $scope.preview = function() { if (!file || !data) return; Cropper.crop(file, data).then(Cropper.encode).then(function(dataUrl) { ($scope.preview || ($scope.preview = {})).dataUrl = dataUrl; }); };
$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(file2, data) .then(function(blob) { return Cropper.scale(blob, {width: width}); }) .then(Cropper.encode).then(function(dataUrl) { ($scope.preview || ($scope.preview = {})).dataUrl = dataUrl; }); }
/**
More on options - https://github.com/fengyuanchen/cropper#options */ $scope.options = { maximize: true, aspectRatio: 3/1, done: function(dataNew) { data = dataNew; } };
/**
ng-cropper
directive is derived from the scope ofng-cropper
directive adds two handlersng-show
&ng-hide
attributes.To show or hide a cropper
$broadcast
a proper event. */ $scope.showEvent = 'show'; $scope.hideEvent = 'hide';function showCropper() { $scope.$broadcast($scope.showEvent); } function hideCropper() { $scope.$broadcast($scope.hideEvent); }
HTML:
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.
//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.
// When there is a cropped image to show encode it to base64 string and // use as a source for an image element.
//defined a second preview2 for the second input file. so, ng-click here will be preview2().
//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
//for the second preview, give it a different name. See HTML code in next post.
ng-cropper
directive is derived from the scope ofng-cropper
directive adds two handlersng-show
&ng-hide
attributes.$broadcast
a proper event. */ $scope.showEvent = 'show'; $scope.hideEvent = 'hide';//showEvent2 and hideEvent2 is for the second input file
//showCropper2 and hideCropper2 is for 2nd input file
Here's my HTML5
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.
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.
@benlooi helped me out was tearing my hair out thanks buddy.