1

Is there any way to get Image Height and Width while uploading image...

I am trying the code below but always getting 0 0:

const uploadedImage = e.target.files[0];
var image = new Image();

image.src = uploadedImage;

console.log(image.naturalWidth,image.naturalHeight);

How Can I Solve this?

Youssouf Oumar
  • 29,373
  • 11
  • 46
  • 65

1 Answers1

2
var _URL = window.URL || window.webkitURL;
$("#file").change(function (e) {
    var file, img;
    if ((file = this.files[0])) {
        img = new Image();
        var objectUrl = _URL.createObjectURL(file);
        img.onload = function () {
            alert(this.width + " " + this.height);
            _URL.revokeObjectURL(objectUrl);
        };
        img.src = objectUrl;
    }
});

You may find details HERE

Tanjin Alam
  • 1,728
  • 13
  • 15