Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the image dimension with angular-file-upload

Tags:

angularjs

I'm currently using the angular-file-upload directive, and I'm pretty much using the exact codes from the demo.

I need to add a step in there to test for the dimension of the image, and I am only currently able to do with via jQuery/javascript.

Just wondering if there's a "angular" way to check for the dimension of the image before it's being uploaded?

$scope.uploadImage = function($files) {
    $scope.selectedFiles = [];
    $scope.progress = [];

    if ($scope.upload && $scope.upload.length > 0) {
        for (var i = 0; i < $scope.upload.length; i++) {
            if ($scope.upload[i] !== null) {
                $scope.upload[i].abort();
            }
        }
    }
    $scope.upload = [];
    $scope.uploadResult = [];
    $scope.selectedFiles = $files;
    $scope.dataUrls = [];

    for ( var j = 0; j < $files.length; j++) {
        var $file = $files[j];
        if (/*$scope.fileReaderSupported && */$file.type.indexOf('image') > -1) {
            var fileReader = new FileReader();
            fileReader.readAsDataURL($files[j]);
            var loadFile = function(fileReader, index) {
                fileReader.onload = function(e) {
                    $timeout(function() {
                        $scope.dataUrls[index] = e.target.result;

                        //------Suggestions?-------//
                        $('#image-upload-landscape').on('load', function(){
                            console.log($(this).width());
                        });
                        //-------------------------//
                    });
                };
            }(fileReader, j);
        }
        $scope.progress[j] = -1;
        if ($scope.uploadRightAway) {
            $scope.start(j);
        }

    }
};
like image 485
muudless Avatar asked Aug 26 '14 01:08

muudless


1 Answers

I think you can do it by:

var reader = new FileReader();
reader.onload = onLoadFile;
reader.readAsDataURL(filtItem._file);

function onLoadFile(event) {
    var img = new Image();
    img.src = event.target.result;
    console.log(img.width, img.height)
}

This is the code snippet copied from https://github.com/nervgh/angular-file-upload/blob/master/examples/image-preview/directives.js.

I think this is more angularjs. However, you may need to modify it to fit your requirement.

like image 140
maxSonic Avatar answered Oct 23 '22 06:10

maxSonic