question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I get the image size before uploading?

See original GitHub issue

How can I get the image size before uploading? I need to get the size to validate that the image is at least X pixel width and height.

I know I can do it like this in a directive that I apply to the <img> element itself.

        link: function(scope, element, attributes) {
            element.on('load', function () {
                scope.cmsImageSize.originalHeight = this.height;
                scope.cmsImageSize.originalHidth = this.width;
            });
        }

But how do I get it from within my files I’m watching? I’ve spent now some time inspecting the objects I have there but couldn’t figure out from where I could get the image dimensions.

$scope.$watch('data.files', function() {
                for (var i = 0; i < $scope.data.files.length; i++) {
                    var file = $scope.data.files[i];
                    $scope.upload = $upload.upload({
                        url: '/api/v1/image_files/upload.json',
                        method: 'POST',
                        data: {
                            'data[ImageFile][model]': 'CmsPageElementImage',
                            'data[ImageFile][foreign_key]': $scope.element.CmsPageElement.id
                        },
                        file: file,
                        fileFormDataName: 'data[ImageFile][file]'
                    }).progress(function (evt) {
                                            /* ... */
                    }).success(function (data, status, headers, config) {
                                            /* ... */
                    });
                }
            });

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
burzumcommented, Feb 16, 2015

OK, I’ve managed to get it to work with this code. Guess this is how it has to be done in JS, looks like an ugly work-around to me.

                    var fr = new FileReader;
                    fr.onload = function () { // file is loaded
                        var img = new Image;
                        img.onload = function () {
                            alert(img.width); // image is loaded; sizes are available
                        };
                        img.src = fr.result; // is the data URL because called with readAsDataURL
                    };
                    fr.readAsDataURL(file); // I'm using a <input type="file"> for demonstrating
1reaction
danialfaridcommented, Sep 8, 2015

If you have ngf-max-width or similar validation, the width and height of the image will be calculated automatically and you can get it by file.width or file.height. You can also use Upload,imageDimensions(file).then(success, error)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Check image width and height before upload with Javascript
I tried the code with an image file of 13 MB. It takes up to 2 seconds within the browser to process and...
Read more >
Check Image Width, Height and its Type before Uploading ...
Here in this post, I'll show how quickly and efficiently you can check the width, height and format of multiple images or files...
Read more >
How to reduce the size of your image file before uploading
On a Mac · Launch Preview, then File/Open an image · Duplicate the image (work on the copy to keep the original unchanged)...
Read more >
How to Validate image width and height before upload using ...
In this tutorial, I show how you can validate image width and height before uploading in the server using JavaScript and PHP.
Read more >
How Do I Resize Images Before Uploading Them? - MC Trade
How Do I Resize Images Before Uploading Them? · 1. Open Paint. · 2. Open the file that you need to edit. ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found