首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图片上传,获取指令中的图片尺寸

问图片上传,获取指令中的图片尺寸
EN

Stack Overflow用户
提问于 2016-08-02 17:40:48
回答 1查看 135关注 0票数 0

我有下面的angular-js指令来‘上传’本地图片:

代码语言:javascript
复制
app.directive('ngFileModel', ['$parse', function ($parse) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            var model = $parse(attrs.ngFileModel);
            var isMultiple = attrs.multiple;
            var modelSetter = model.assign;
            element.bind('change', function () {
                var values = [];
                angular.forEach(element[0].files, function (item) {
                    var value = {
                       // File Name 
                        name: item.name,
                        //File Size 
                        size: item.size,
                        //File URL to view 
                        url: (URL || webkitURL).createObjectURL(item),
                        // File Input Value 
                        _file: item
                    };
                    values.push(value);


                });
                scope.$apply(function () {
                    if (isMultiple) {
                        modelSetter(scope, values);
                    } else {
                        modelSetter(scope, values[0]);
                    }
                });
            });
        }
    };
}]);

我现在想要获取图像尺寸,但我不知道如何获取。我尝试了类似的方法(在values.push(Value)行下面):

代码语言:javascript
复制
var img = new Image();
img.src = value.url;

alert("Width: " + img.width);

但这并不管用。我也尝试了img.onload,但是我也没有得到结果。

如何获取图像尺寸?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2016-08-02 22:22:16

我自己解决了这个问题,image.onload是正确的选择:

代码语言:javascript
复制
app.directive('ngFileModel', ['$parse', function ($parse) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            var model = $parse(attrs.ngFileModel);
            var isMultiple = attrs.multiple;
            var modelSetter = model.assign;
            element.bind('change', function () {
                var values = [];
                angular.forEach(element[0].files, function (item) {

                    var img = new Image();  
                    var imgheight = 0;
                    var imgwidth = 0;
                    var imgurl = (URL || webkitURL).createObjectURL(item);

                    img.src = imgurl;
                    img.onload = function() {

                        imgheight = img.height;
                        imgwidth = img.width;
                        //alert("Width: " + imgheight + "  height: " + imgwidth);

                        var value = {
                       // File Name 
                        name: item.name,
                        //File Size 
                        size: item.size,
                        //File URL to view 
                        url: imgurl,
                        // File Input Value 
                        _file: item,

                        width: imgwidth,

                        height: imgheight,

                        mystyle: {}
                        };
                        scope.$apply(function () {
                            values.push(value);
                        });                     
                    };
                });
                scope.$apply(function () {
                    if (isMultiple) {
                        modelSetter(scope, values);
                    } else {
                        modelSetter(scope, values[0]);
                    }
                });
            });
        }
    };
}]);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38716912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档