- <?
- /**
- * @name miniature
- * Créer une miniature d'une image donnée et en retourne l'adresse.
- *
- * @param String sPathFrom : Répertoire d'origine
- * @param String sPathTo : Répertoire de destination
- * @param String integer iMaxDimension : Taille maximale de la miniature
- * @param boolean bOverwrite : Ecraser la miniature si elle existe
- *
- * @return String Adresse de la miniature nouvellement crée.
- */
-
- function miniature ($sPathFrom, $sPathTo, $iMaxDimension, $sNameImage, $bOverwrite=false)
- {
- //test des paramètres
- try {
- if (!is_string($sPathFrom) )
- throw new Exception ('erreur : miniature(). sPathFrom must be a string');
-
- if (!is_string($sPathTo) )
- throw new Exception ('erreur : miniature(). sPathTo must be a string');
-
- if (!is_int($iMaxDimension) )
- throw new Exception ('erreur : miniature(). iMaxDimension must be a numeric');
-
- if (!is_string($sNameImage) )
- throw new Exception ('erreur : miniature(). sNameImage must be a string');
-
- if (!is_bool($bOverwrite) )
- throw new Exception ('erreur : miniature(). bOverwrite must be a boolean');
- }
- catch (Exception $e) {
- echo $e->getMessage()."\n";
- return false;
- }
- // fin test des paramètres
-
- //répertoire de l'image
- $dir_img = substr($sPathFrom, 0, strrpos($sPathFrom, "/")+1);
-
- //test si le fichier existe et qu'on écrase OU que le fichier n'existe pas.
- if((file_exists($sPathTo.$sNameImage) && true === $bOverwrite) || !file_exists($sPathTo.$sNameImage)) {
- //Largeur et hauteur des miniatures
- $width = $iMaxDimension;
- $height = $iMaxDimension;
-
- // Calcul des nouvelles dimensions en gardant les proportions
- list($width_orig, $height_orig) = getimagesize($sPathFrom);
-
- if ($width && ($width_orig < $height_orig))
- $width = ($height / $height_orig) * $width_orig;
- else
- $height = ($width / $width_orig) * $height_orig;
-
- // création d'une image vierge
- $mini = @ImageCreateTrueColor($width, $height)
- or die ("Impossible de crée un flux d'image GD");
-
- //On ressample l'image initiale pour en créer une copie en miniature
- $imgSrc = imagecreatefromjpeg($sPathFrom);
- ImageCopyResampled($mini, $imgSrc, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
-
- //On enregistre l'image dans le répertoire des miniatures
- if (!file_exists($sPathTo) ) {
- mkdir($sPathTo);
- chmod ($sPathTo, 0764);
- }
- imageJpeg($mini, "".$sPathTo.$sNameImage.".jpg");
- }
-
- return $sPathTo.$sNameImage.".jpg";
- }
- ?>
<?
/**
* @name miniature
* Créer une miniature d'une image donnée et en retourne l'adresse.
*
* @param String sPathFrom : Répertoire d'origine
* @param String sPathTo : Répertoire de destination
* @param String integer iMaxDimension : Taille maximale de la miniature
* @param boolean bOverwrite : Ecraser la miniature si elle existe
*
* @return String Adresse de la miniature nouvellement crée.
*/
function miniature ($sPathFrom, $sPathTo, $iMaxDimension, $sNameImage, $bOverwrite=false)
{
//test des paramètres
try {
if (!is_string($sPathFrom) )
throw new Exception ('erreur : miniature(). sPathFrom must be a string');
if (!is_string($sPathTo) )
throw new Exception ('erreur : miniature(). sPathTo must be a string');
if (!is_int($iMaxDimension) )
throw new Exception ('erreur : miniature(). iMaxDimension must be a numeric');
if (!is_string($sNameImage) )
throw new Exception ('erreur : miniature(). sNameImage must be a string');
if (!is_bool($bOverwrite) )
throw new Exception ('erreur : miniature(). bOverwrite must be a boolean');
}
catch (Exception $e) {
echo $e->getMessage()."\n";
return false;
}
// fin test des paramètres
//répertoire de l'image
$dir_img = substr($sPathFrom, 0, strrpos($sPathFrom, "/")+1);
//test si le fichier existe et qu'on écrase OU que le fichier n'existe pas.
if((file_exists($sPathTo.$sNameImage) && true === $bOverwrite) || !file_exists($sPathTo.$sNameImage)) {
//Largeur et hauteur des miniatures
$width = $iMaxDimension;
$height = $iMaxDimension;
// Calcul des nouvelles dimensions en gardant les proportions
list($width_orig, $height_orig) = getimagesize($sPathFrom);
if ($width && ($width_orig < $height_orig))
$width = ($height / $height_orig) * $width_orig;
else
$height = ($width / $width_orig) * $height_orig;
// création d'une image vierge
$mini = @ImageCreateTrueColor($width, $height)
or die ("Impossible de crée un flux d'image GD");
//On ressample l'image initiale pour en créer une copie en miniature
$imgSrc = imagecreatefromjpeg($sPathFrom);
ImageCopyResampled($mini, $imgSrc, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
//On enregistre l'image dans le répertoire des miniatures
if (!file_exists($sPathTo) ) {
mkdir($sPathTo);
chmod ($sPathTo, 0764);
}
imageJpeg($mini, "".$sPathTo.$sNameImage.".jpg");
}
return $sPathTo.$sNameImage.".jpg";
}
?>