0

我需要根据 1:3 的比例来验证图像的宽度 - 高度。我正在使用 ng-file-upload 上传图片。验证需要在发送到服务器之前完成。

我对如何从所选图像中获取图像宽度/高度一无所知。有人可以帮帮我吗?

我正在关注本网站的教程:http: //odetocode.com/blogs/scott/archive/2013/07/03/building-a-filereader-service-for-angularjs-the-service.aspx

4

3 回答 3

1

那将是插件的功能请求。作为一种解决方法,您可以这样做:

<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;
}

或者

<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?

添加了编辑 功能您可以拥有

<div ngf-select ngf-model="image" ngf-ratio="1x3" ngf-pattern="'image/*'" accept="image/*" > 
于 2015-09-01T17:09:05.817 回答
0

上传文件后,是否为 DOM 元素分配了 ID?如果是这样,普通的 JS 可以像这样工作:

var img = document.getElementById('imageid'); 

var width = img.clientWidth;
var height = img.clientHeight;
于 2015-09-01T13:24:22.917 回答
0
    var image = angular.element('<CLASS_NAME or ID_NAME>');
    $scope.width = image.width();
    $scope.height = image.height();

希望这对你有帮助!

于 2016-05-23T13:28:51.567 回答