Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery image and height display

<input type="file" class="test" name="vend_logo" id="vend_logo">

$('.test').bind('change', function() {
var file = files[0]
var img = new Image();
var width = img.width;alert(width);
});

I wanted to alert the image width and height been uploaded. how can i do this ? I have used clientwidth and also naturalwidth but it is not displaying the correct value.

like image 777
anumol Avatar asked Oct 31 '22 16:10

anumol


1 Answers

You can do something like this

$('.test').bind('change', function() {
  if (file = this.files[0]) {
    img = new Image();
    img.onload = function() {
      var width = this.width;
      alert(width);
    };
    img.src = window.URL.createObjectURL(file);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="file" class="test" name="vend_logo" id="vend_logo">

Reference : Check image width and height before upload with Javascript

like image 59
Pranav C Balan Avatar answered Nov 08 '22 05:11

Pranav C Balan