Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

dimension d'une image chargée

2 réponses
Avatar
jfa
Bonjour
Je voudrais récupèrer les dimensions d'une image sans avoir à l'afficher.
J'ai essayé:

document.write('<img src="monimage.gif">');
imgHeight = document.images[0].height;

puis :

img = new Image();
img.src = "monimage.gif";
imgHeight = img.height;

le tout agrémenté de bouclage avec setTimeout() en testant img.complete.

Mais ça ne marche pas correctement, et en tous cas pas du premier coup.

Quelqu'un aurait-il un bout de code en état de marche ?
Merci d'avance.
JF

2 réponses

Avatar
ASM
Bonjour
Je voudrais récupèrer les dimensions d'une image sans avoir à l'afficher.
J'ai essayé:


function getImgTaille(img) {
var i = new Image();
i.onload = function() { alert('L = '+i.width+'nH = '+i.height); };
i.src = img;
}

<a href="truc.jpg" onclick="getImgTaille(this.href); return false;">
taille truc
</a>

Mais pas certain non plus que ce soit pour tous navigateurs

document.write('<img src="monimage.gif">');
imgHeight = document.images[0].height;


non pour cette méthode c'est

document.getElementById('info').innerHTML='Patienter';
document.images['visuel'].src = 'monimage.gif"

avec :

<p id="info"></p>
<img src="" alt="" name="visuel"
style="position:absolute;left:-2000px"
onload="alert('L = '+this.width+'nH = '+this.height);
document.getElementById('info').innerHTML='';" />




--
Stephane Moriaux et son [moins] vieux Mac

Avatar
jfaba31
Merci, ça marche très bien !