How can I get the image size before uploading?
See original GitHub issueHow 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:
- Created 9 years ago
- Comments:14 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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
orfile.height
. You can also useUpload,imageDimensions(file).then(success, error)