S_S
S_S

Reputation: 1412

Input type file check image resolution using pure Javascript

I have created a type=file input element

<input type="file" id="input-id" accept="image/jpg" onchange="verifyFileUpload(event)">

I need to check that the file resolution is aXb using pure Javascript. How can I do that in the verifyFileUpload(event) function?

Upvotes: 3

Views: 4407

Answers (1)

Hary
Hary

Reputation: 5818

Try the below way

window.URL = window.URL || window.webkitURL;

function verifyFileUpload(e)
{
  var file = document.getElementById("input-id");
  
  if (file && file.files.length > 0) 
  {
        var img = new Image();

        img.src = window.URL.createObjectURL( file.files[0] );
        img.onload = function() 
        {
            var width = this.naturalWidth,
                height = this.naturalHeight;
                
          console.log ("Image Width: " + width);
          console.log ("Image Height: " +height);
        };
    }
}
<input type="file" id="input-id" accept="image/jpg" onchange="verifyFileUpload(event)">

Upvotes: 10

Related Questions