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
|
<?php
header("Content-Type: text/plain"); // Utilisation d'un header pour spécifier le type de contenu de la page. Ici, il s'agit juste de texte brut (text/plain).
function fct_redim_image($Wmax, $Hmax, $rep_Dst, $img_Dst, $rep_Src, $img_Src) {
// ------------------------------------------------------------------
$condition = 0;
// Si certains paramètres ont pour valeur '' :
if ($rep_Dst == '') { $rep_Dst = $rep_Src; } // (meme repertoire)
if ($img_Dst == '') { $img_Dst = $img_Src; } // (meme nom)
if ($Wmax == '') { $Wmax = 0; }
if ($Hmax == '') { $Hmax = 0; }
// ------------------------------------------------------------------
// si le fichier existe dans le répertoire, on continue...
if (file_exists($rep_Src.$img_Src) && ($Wmax!=0 || $Hmax!=0)) {
// ----------------------------------------------------------------
// extensions acceptées :
$ExtfichierOK = '" jpg jpeg png"'; // (l espace avant jpg est important)
// extension
$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) != '') {
// -------------------------------------------------------------
// récupération des dimensions de l image Src
$size = getimagesize($rep_Src.$img_Src);
$W_Src = $size[0]; // largeur
$H_Src = $size[1]; // hauteur
// -------------------------------------------------------------
// condition de redimensionnement et dimensions de l image finale
// -------------------------------------------------------------
// A- LARGEUR ET HAUTEUR maxi fixes
if ($Wmax != 0 && $Hmax != 0) {
$ratiox = $W_Src / $Wmax; // ratio en largeur
$ratioy = $H_Src / $Hmax; // 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- LARGEUR maxi fixe
if ($Hmax != 0 && $Wmax == 0) {
$H = $Hmax;
$W = $H * ($W_Src / $H_Src);
$condition = $H_Src > $Hmax; // 1 si vrai (true)
}
// -------------------------------------------------------------
// C- HAUTEUR maxi fixe
if ($Wmax != 0 && $Hmax == 0) {
$W = $Wmax;
$H = $W * ($H_Src / $W_Src);
$condition = $W_Src > $Wmax; // 1 si vrai (true)
}
// -------------------------------------------------------------
// on REDIMENSIONNE si la condition est vraie
// -------------------------------------------------------------
if ($condition == 1) {
// création de la ressource-image"Src" en fonction de l extension
// et on crée une ressource-image"Dst" vide aux dimensions finales
switch($extension) {
case 'jpg':
case 'jpeg':
$Ress_Src = imagecreatefromjpeg($rep_Src.$img_Src);
$Ress_Dst = ImageCreateTrueColor($W,$H);
break;
case 'png':
$Ress_Src = imagecreatefrompng($rep_Src.$img_Src);
$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, ré-echantillonne)
ImageCopyResampled($Ress_Dst, $Ress_Src, 0, 0, 0, 0, $W, $H, $W_Src, $H_Src);
// ----------------------------------------------------------
// ENREGISTREMENT dans le répertoire (avec la fonction appropriée)
switch ($extension) {
case 'jpg':
case 'jpeg':
ImageJpeg ($Ress_Dst, $rep_Dst.$img_Dst);
break;
case 'png':
imagepng ($Ress_Dst, $rep_Dst.$img_Dst);
break;
}
// ----------------------------------------------------------
// libération des ressources-image
imagedestroy ($Ress_Src);
imagedestroy ($Ress_Dst);
}
// -------------------------------------------------------------
}
}
// --------------------------------------------------------------------------------------------------
// retourne : 1 (vrai) si le redimensionnement et l enregistrement ont bien eu lieu, sinon rien (false)
// si le fichier a bien été créé
if ($condition == 1 && file_exists($rep_Dst.$img_Dst)) { return true; }
else { return false; }
}
// --------------------------------------------------------------------------------------------------
$variable1 = (isset($_GET["variable1"])) ? $_GET["variable1"] : NULL;
// Les paramètres :
// - $Wmax : LARGEUR maxi finale ----> ou 0 : largeur libre
// - $Hmax : HAUTEUR maxi finale ----> ou 0 : hauteur libre
// - $rep_Dst : répertoire de l image de Destination (déprotégé) ----> ou '' : même répertoire
// il faut s'assurer que les droits en écriture ont été donnés au dossier (chmod ou via logiciel FTP)
// - $img_Dst : NOM de l image de Destination ----> ou '' : même nom que l image Src
// - $rep_Src : répertoire de l image Source (déprotégé)
// - $img_Src : NOM de l image Source
if ($variable1) {
// Faire quelque chose...
$variable2 = "tn_".$variable1;
$redimOK = fct_redim_image(180,180,'PhoV/',$variable2,'Photos/',$variable1);
//echo "OK";
} else {
echo "FAIL";
}
?>
exit;
?> |
Partager