Como puedo saber el tamaño de una imagen con jquery



   AUTOR PREGUNTA

Publicado 02 abril 2014 - 20:45

Como puedo saber el anocho y alto original de una imagen mediante algun funcion en javascript o jquery, por ejemplo si tengo la imagen que ha sido redimensionada.

<img src="mifoto.jpg" width="300" height="250" id="mifoto" />



2 personas más tuvieron esta duda Yo también

 

Publicado 02 abril 2014 - 20:50

Con jQuery podes averiguar el ancho y alto de un elemento utilizando las funciones width() y height().
Supongamos tu imagen

<img src="mifoto.jpg" width="300" height="250" id="mifoto" />

Para averiguar el tamaño original de la imagen, primero eliminamos los atributos (width, heitght) actuales de la imagen con la función removeAttr(), y listo, ya podemos aplicar ahora las funciones width() y height() y obtendremos el tamaño real de la imagen:

$(window).load(function() {
  var imagen = $('#mifoto');
  imagen.removeAttr("width"); // quitamos el atributo width 
  imagen.removeAttr("height"); // quitamos el atributo height 
  alert( imagen.width() ); // ancho original. Ej. 800
  alert( imagen.height() ); // alto original. Ej: 600
});