1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| function redimImage(inImg,maxWidth,maxHeight,inAlt,inClass){
// inImg : Chemin relatif de l'image; inMW : Largeur maximale; inMH : Hauteur maximale ; alt : string alt; class: position relative
// Declaration d'un objet Image
var oImg = new Image();
// Affectation du chemin de l'image a l'objet
oImg.src = inImg;
var dW ;
var dH ;
var h = oImg.height;
var w = oImg.width;
// Si la largeur ou la hauteur depasse la taille maximale
if ((h >= maxHeight) || (w >= maxWidth)) {
// Si la largeur et la hauteur depasse la taille maximale
if ((h >= maxHeight) && (w >= maxWidth)) {
// On cherche la plus grande valeur
if (h > w) {
dH = maxHeight;
dW = parseInt((w * dH) / h, 10);
} else {
dW = maxWidth;
dH = parseInt((h * dW) / w, 10);
}
} else if ((h > maxHeight) && (w < maxWidth)) {
// Si la hauteur depasse la taille maximale
dH = maxHeight;
dW = parseInt((w * dH) / h, 10);
} else if ((h < maxHeight) && (w > maxWidth)) {
// Si la largeur depasse la taille maximale
dW = maxWidth;
dH = parseInt((h * dW) / w, 10);
}
}else {
dW = w;
dH = h;
}
document.write('<img src="' + inImg + '" width="' + dW + '" height="' + dH + '" border="1" alt="'+ inAlt + '" class=" ' + inClass + '" /> ');
} |