I need to validate the image width - height on the basis of 1:3 ratio. I am using ng-file-upload to upload the image. The validation needs to be done before sending it to server.
I am clueless about how to get the image width/height from the selected image. Can somebody help me out ?
I am following the tutorial from this site : http://odetocode.com/blogs/scott/archive/2013/07/03/building-a-filereader-service-for-angularjs-the-service.aspx
That would be a feature request for the plugin. As a workaround you can do this:
<div ngf-select ngf-model="image" ngf-validate-fn-async="validateRatio(image)" ngf-pattern="'image/*'" accept="image/*" >
$scope.validateRatio = function(image) {
var defer = $q.defer();
Upload.imageDimensions(image).then(function(d) {
if (d.width / d.height === expectedRatio) {
defer.resolve()
} else {
defer.reject();
}
}, function() {defer.reject();});
return defer.promise;
}
or
<div ngf-select ngf-model="image" ngf-pattern="'image/*'" ngf-min-height="0" accept="image/*" >
<div ng-show="image.width && (image.width / image.height !== expectedRatio)">Invalid ratio?
EDIT Feature is added You can have
<div ngf-select ngf-model="image" ngf-ratio="1x3" ngf-pattern="'image/*'" accept="image/*" >
When you have uploaded the file, are you assigning the DOM element with an ID? If so, plain JS could work such as:
var img = document.getElementById('imageid');
var width = img.clientWidth;
var height = img.clientHeight;
var image = angular.element('<CLASS_NAME or ID_NAME>');
$scope.width = image.width();
$scope.height = image.height();
Hope this helps You!.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.