2011-07-09 23 views
5

posibles duplicados:
get image height and width in file tag javascript
Determining image file size + dimensions via Javascript?
How to upload preview image before upload through JavaScriptobtener la altura y la anchura de identificación de ficheros usando javascript

¿Cómo puedo obtener la altura y la anchura de la imagen sin recargar la página en la etiqueta de archivo?

<HTML> 
<HEAD> 
    <TITLE></TITLE> 
    <script language="javascript"> 
    function getW() 
    { 
     var theImg = document.getElementById('testimg'); 
     alert(theImg.width); 
    } 

    function getH() 
    { 
     var theImg = document.getElementById('testimg'); 
     alert(theImg.height); 
    } 
    </script> 
</HEAD> 

<BODY>   
<input type="file" id="testimg"/> 
    <input type="button" value="get Width" onclick="getW()"/> 
    <input type="button" value="get Height" onclick="getH()"/> 
</BODY> 
</HTML> 

Me da la altura de la imagen y el ancho de la imagen utilizando el código php, pero esa página tiempo se regenerará, sin recargar la página me sale tamaño de la imagen, pero no una altura y anchura ....

+0

lolwut? 2 preguntas como esta una tras otra? http://stackoverflow.com/questions/6633190/get-image-height-and-width-in-file-tag-javascript – Pwnna

+0

http://stackoverflow.com/questions/4094012/how-to-upload-preview- image-before-upload-through-javascript http://stackoverflow.com/questions/1310378/determining-image-file-size-dimensions-via-javascript – AmdY

Respuesta

0

Uso onload el evento:

document.getElementById('testimg').onload = function() { 
    alert(document.getElementById('testimg').offsetHeight); 
} 

usted puede cambiar la acción de la función, por ejemplo, haciendo que los botones visibles, o no-discapacitados, depende de usted.

No estoy seguro Entiendo su pregunta, pero Javascript tendrá que cargar la imagen antes de poder obtener sus dimensiones.

0

Esto es imposible de hacer de forma cruzada.

La única forma de navegador cruzado que se me viene a la mente (pero no es simple) es subir el archivo ajax, renderizar esa imagen (podría ocultarse) y obtener su tamaño con img.width y img.height.

Espero que esto ayude. Saludos

Cuestiones relacionadas