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
| function reDimImage(img_Src, W_max, H_max) {
// ----------------------------------------------------
// Lit les dimensions de l'image source
W_Src = img_Src.width; // largeur source
H_Src = img_Src.height; // hauteur source
// ----------------------------------------------------
if(!W_max) { W_max = 0; }
if(!H_max) { H_max = 0; }
// ----------------------------------------------------
// Teste les dimensions tenant dans la zone
var ratio = 0 ;
if(W_max < W_Src){
ratio = W_max / W_Src ;
//Redimensionnement selon la largeur
H = Math.round(H_Src * ratio);
W = Math.round(W_Src * (H / H_Src));
}
if(H_max < H_Src){
//Redimensionnement selon la hauteur
ratio = H_max / H_Src;
W = Math.round(W_Src * ratio);
H = Math.round(H_Src * (W / W_Src));
}
//Sinon on laisse les dimansions de base
if(ratio == 0){
W = W_Src;
H = H_Src;
}
//Redimensionne l'image source automatiquement ;-)
img_Src.style.width = W + "px";
img_Src.style.height = H + "px";
} |