如何在AngularJS中获取图片尺寸? [英] How to get image dimensions in AngularJS?

查看:748
本文介绍了如何在AngularJS中获取图片尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户选择要用< input type =file>

$ b上传文件时检查图片的尺寸
$ b

到目前为止,我已经完成了这段代码:

HTML $ b

 < input type =fileaccept =。png,.jpeg,.jpgng-files =getTheFiles($ files)ng-model = 'company.logo' > 

AngularJS

  var reader = new FileReader(); 
reader.readAsDataURL($ files [0]);
reader.onload = function(e){
$ scope.imgpath = new Image();
$ scope.imgpath = e.target.result;
console.log($ scope.imgpath.width,$ scope.imgpath.height);



$ b $ p
$ b

在控制台中,所选图像的高度和宽度显示为0,0。



我从这个问题



感谢您的帮助。 好的,我试过了代码和它的工作:

  var fileReader = new FileReader(); 
$ scope.imgpath = new Image();
fileReader.onload = function(event){
$ scope.imgpath.src = event.target.result;
$ scope.imgpath.onload = function(){
console.log(this.width,this.height);
};
};

来源: CodePen的这段代码


I want to check the dimensions of the image when the user selects the file to upload with <input type = "file">

So far I've done this code:

HTML

<input type="file" accept=".png,.jpeg,.jpg"  ng-files="getTheFiles($files)" ng-model='company.logo'>

AngularJS

  var reader = new FileReader();
  reader.readAsDataURL($files[0]);      
  reader.onload = function(e) {         
      $scope.imgpath = new Image();
      $scope.imgpath = e.target.result;
      console.log($scope.imgpath.width, $scope.imgpath.height);  
  }

In the console, the height and width of the selected image is being shown as 0,0.

I've referred the answer(which is not accepted but upvoted 5 times) from this question.

Thank you for your help.

解决方案

Okay I tried this code and it worked:

                var fileReader = new FileReader();
                $scope.imgpath = new Image();
                fileReader.onload = function (event) {
                    $scope.imgpath.src = event.target.result;
                    $scope.imgpath.onload = function(){
                        console.log(this.width, this.height);
                    };
                };

Source : This code from CodePen

这篇关于如何在AngularJS中获取图片尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆