[英]Image Width Height Via AngularJS (ng-file-upload)
我需要根據1:3的比例來驗證圖像的寬度-高度。 我正在使用ng-file-upload上傳圖片。 驗證需要先完成,然后再發送到服務器。
我對如何從所選圖像中獲取圖像寬度/高度一無所知。 有人可以幫我嗎?
我正在從此網站上關注教程: http : //odetocode.com/blogs/scott/archive/2013/07/03/building-a-filereader-service-for-angularjs-the-service.aspx
這將是該插件的功能請求。 作為解決方法,您可以執行以下操作:
<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/*" >
上傳文件后,是否要為DOM元素分配ID? 如果是這樣,純JS可以像這樣工作:
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();
希望對您有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.