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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| <?php
function redimentionnementimage($W_max, $H_max, $rep_Dst, $img_Dst, $rep_Src, $img_Src) {
// ------------------------------------------------------------------
$condition = 0;
if ($rep_Dst == '') { $rep_Dst = $rep_Src; } //cas où la source et la destination sont dans le meme repertoire
if ($img_Dst == '') { $img_Dst = $img_Src; } //cas où la source et la destination sont dans le meme nom
// ------------------------------------------------------------------
// si le fichier existe dans le répertoire, on continue...
if (file_exists($rep_Src.$img_Src) && ($W_max!=0 || $H_max!=0))
{
// ----------------------------------------------------------------
// extensions acceptees :
$ExtfichierOK = '" jpg jpeg png"'; // (l espace avant jpg est important)
// extension fichier Source
$tabimage = explode('.',$img_Src);
$extension = $tabimage[sizeof($tabimage)-1]; // ±tabimage[1] mais cas où il y est plusieurs "." dans le nom du fichier.
$extension = strtolower($extension); // on met en minuscule // securisation
// ----------------------------------------------------------------
//Vérification de c'est le bon format
if (strpos($ExtfichierOK,$extension) != '')
{
// -------------------------------------------------------------
// recuperation des dimensions de l image Src
$img_size = getimagesize($rep_Src.$img_Src);
$W_Src = $img_size[0]; // largeur
$H_Src = $img_size[1]; // hauteur
// -------------------------------------------------------------
// condition de redimensionnement et dimensions de l image finale
// -------------------------------------------------------------
// A- LARGEUR ET HAUTEUR maxi fixes
if ($W_max != 0 && $H_max != 0) {
$ratiox = $W_Src / $W_max; // ratio en largeur
$ratioy = $H_Src / $H_max; // ratio en hauteur
$ratio = max($ratiox,$ratioy); // le plus grand
$W = $W_Src/$ratio;
$H = $H_Src/$ratio;
$condition = ($W_Src>$W) || ($W_Src>$H); // 1 si vrai (true)
} // -------------------------------------------------------------
// B- HAUTEUR maxi fixe
if ($W_max == 0 && $H_max != 0) {
$H = $H_max;
$W = $H * ($W_Src / $H_Src);
$condition = $H_Src > $H_max; // 1 si vrai (true)
}
// -------------------------------------------------------------
// C- LARGEUR maxi fixe
if ($W_max != 0 && $H_max == 0) {
$W = $W_max;
$H = $W * ($H_Src / $W_Src);
$condition = $W_Src > $W_max; // 1 si vrai (true)
}
// -------------------------------------------------------------
// on REDIMENSIONNE si la condition est vraie
// -------------------------------------------------------------
if ($condition == 1) {
// ----------------------------------------------------------
// creation de la ressource-image "Src" en fonction de l extension
switch($extension) {
case 'jpg':
$Ress_Src = imagecreatefromjpeg($rep_Src.$img_Src);
break;
case 'jpeg':
$Ress_Src = imagecreatefromjpeg($rep_Src.$img_Src);
break;
case 'png':
$Ress_Src = imagecreatefrompng($rep_Src.$img_Src);
break;
}
// ----------------------------------------------------------
// creation d une ressource-image "Dst" aux dimensions finales
switch($extension) {
case 'jpg':
$Ress_Dst = imagecreatetruecolor($W,$H);
break;
case 'jpeg':
$Ress_Dst = imagecreatetruecolor($W,$H);
break;
case 'png':
$Ress_Dst = imagecreatetruecolor($W,$H);
// fond transparent (pour les png avec transparence)
imagesavealpha($Ress_Dst, true);
$trans_color = imagecolorallocatealpha($Ress_Dst, 0, 0, 0, 127);
imagefill($Ress_Dst, 0, 0, $trans_color);
break;
}
// ----------------------------------------------------------
// REDIMENSIONNEMENT (copie, redimensionne, re-echantillonne)
imagecopyresampled($Ress_Dst, $Ress_Src, 0, 0, 0, 0, $W, $H, $W_Src, $H_Src);
// ----------------------------------------------------------
// ENREGISTREMENT dans le repertoire (avec la fonction appropriee)
switch ($extension) {
case 'jpg':
imagejpeg ($Ress_Dst, $rep_Dst.$img_Dst);
break;
case 'jpeg':
imagejpeg ($Ress_Dst, $rep_Dst.$img_Dst);
break;
case 'png':
imagepng ($Ress_Dst, $rep_Dst.$img_Dst);
break;
}
// ----------------------------------------------------------
// liberation des ressources-image
imagedestroy ($Ress_Src);
imagedestroy ($Ress_Dst);
}
// -------------------------------------------------------------
}
}
// -----------------------------------------------------------------------------------------------------
// si le fichier a bien ete cree
if ($condition == 1 && file_exists($rep_Dst.$img_Dst)) { return true; }
else { return false; }
}
// retourne : 1 (vrai) si le redimensionnement et l enregistrement ont bien eu lieu, sinon rien (false)
// -----------------------------------------------------------------------------------------------------
?> |
Partager