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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| <?php
// ---------------------------------------------------------------
// fonction de REDIMENSIONNEMENT physique "PROPORTIONNEL" et Enregistrement
// ---------------------------------------------------------------
// retourne : 1 (vrai) si le redimensionnement et l enregistrement ont bien eu lieu, sinon rien (false)
// ---------------------------------------------------------------
// La FONCTION : fctredimimage ($W_max, $H_max, $rep_Dst, $img_Dst, $rep_Src, $img_Src)
// Les parametres :
// - $W_max : LARGEUR maxi finale --> ou 0
// - $H_max : HAUTEUR maxi finale --> ou 0
// - $rep_Dst : repertoire de l image de Destination (deprotégé) --> ou '' (meme repertoire)
// - $img_Dst : NOM de l image de Destination --> ou '' (meme nom que l image Source)
// - $rep_Src : repertoire de l image Source (deprotégé)
// - $img_Src : NOM de l image Source
// ---------------------------------------------------------------
// 3 options :
// A- si $W_max != 0 et $H_max != 0 : a LARGEUR maxi ET HAUTEUR maxi fixes
// B- si $H_max != 0 et $W_max == 0 : image finale a HAUTEUR maxi fixe (largeur auto)
// C- si $W_max == 0 et $H_max != 0 : image finale a LARGEUR maxi fixe (hauteur auto)
// Si l'image Source est plus petite que les dimensions indiquees : PAS de redimensionnement.
// ---------------------------------------------------------------
// $rep_Dst : il faut s'assurer que les droits en écriture ont été donnés au dossier (chmod)
// - si $rep_Dst = '' : $rep_Dst = $rep_Src (meme repertoire que l image Source)
// - si $img_Dst = '' : $img_Dst = $img_Src (meme nom que l image Source)
// - si $rep_Dst='' ET $img_Dst='' : on ecrase (remplace) l image source !
// ---------------------------------------------------------------
// NB : $img_Dst et $img_Src doivent avoir la meme extension (meme type mime) !
// Extensions acceptees (traitees ici) : .jpg , .jpeg , .png
// Pour ajouter d autres extensions : voir la bibliotheque GD ou ImageMagick
// (GD) NE fonctionne PAS avec les GIF ANIMES ou a fond transparent !
// ---------------------------------------------------------------
// UTILISATION (exemple) :
// $redimOK = fctredimimage(120,80,'reppicto/','monpicto.jpg','repimage/','monimage.jpg');
// if ($redimOK == 1) { echo 'Redimensionnement OK !'; }
// ---------------------------------------------------------------
function fctredimimage($W_max, $H_max, $rep_Dst, $img_Dst, $rep_Src, $img_Src) {
// ------------------------------------------------------------------
$condition = 0;
// Si certains parametres ont pour valeur '' :
if ($rep_Dst == '') { $rep_Dst = $rep_Src; } // (meme repertoire)
if ($img_Dst == '') { $img_Dst = $img_Src; } // (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]; // dernier element
$extension = strtolower($extension); // on met en minuscule
// ----------------------------------------------------------------
// extension OK ? on continue ...
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
// -------------------------------------------------------------
// Par defaut :
// Si l'image Source est plus petite que les dimensions indiquees :
// PAS de redimensionnement.
// Mais on peut "forcer" le redimensionnement en ajoutant ici :
// $condition = 1;
if ($condition == 1) {
// ----------------------------------------------------------
// creation de la ressource-image "Src" en fonction de l extension
switch($extension) {
case 'jpg':
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
// fond noir (par defaut)
switch($extension) {
case 'jpg':
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':
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