/*---------------------------------------------------------------*/
|
/*
|
Titre : Images Miniatures
|
|
URL : https://phpsources.net/code_s.php?id=989
|
Date édition : 15 Fev 2019
|
*/
|
/*---------------------------------------------------------------*/
|
|
|
function make_thumbs($dirSrc , $dirDest){
|
/* parametres supplementaires :
|
* $tnH : largeur a attribuer aux miniatures. (par defaut : 100 px)
|
* (la hauteur est determinee de facon a conserver le ratio)
|
* $format : format de sortie (par defaut le format de l'image originale)
|
* valeurs : "GIF", "JPG", "PNG", "WBMP"
|
*/
|
|
$tnH = 100;
|
if (func_num_args() >= 3)
|
{
|
$arg = func_get_arg(2);
|
if (is_int($arg))
|
$tnH = $arg;
|
if (is_string($arg))
|
{
|
if (!strcmp("GIF", strtoupper($arg)))
|
$format = 1;
|
else
|
if (!strcmp("JPG", strtoupper($arg)))
|
$format = 2;
|
else
|
if (!strcmp("PNG", strtoupper($arg)))
|
$format = 3;
|
else
|
if (!strcmp("WBMP", strtoupper($arg)))
|
$format = 4;
|
}
|
}
|
if (func_num_args() >= 4)
|
{
|
$arg = func_get_arg(3);
|
if (is_int($arg))
|
$tnH = $arg;
|
if (is_string($arg))
|
{
|
if (!strcmp("GIF", strtoupper($arg)))
|
$format = 1;
|
else
|
if (!strcmp("JPG", strtoupper($arg)))
|
$format = 2;
|
else
|
if (!strcmp("PNG", strtoupper($arg)))
|
$format = 3;
|
else
|
if (!strcmp("WBMP", strtoupper($arg)))
|
$format = 4;
|
}
|
}
|
|
if (!(preg_match("/\/$/i", $dirSrc)))
|
$dirSrc .= "/";
|
|
// test des permissions sur le repertoire de destination
|
$perms = fileperms($dirDest);
|
$perms = $perms & 07;
|
if (($perms & 01) && ($perms & 02) && ($perms & 04))
|
{
|
if (!(preg_match("/\/$/i", $dirDest)))
|
$dirDest .= "/";
|
}
|
else
|
return "Le repertoire cible doit etre lisible, inscriptible et executable";
|
$fp = opendir($dirSrc);
|
if (!$fp)
|
return "Repertoire source illisible";
|
$pics = array();
|
while($file = readdir($fp))
|
{
|
if (!is_dir($file) && (preg_match("/\.(gif|jpe|jpg|jpeg|png|wbmp)$/i", $file)
|
))
|
{
|
array_push($pics, "$dirSrc$file");
|
}
|
}
|
closedir($fp);
|
$taille = sizeof($pics);
|
$thumbs = array();
|
|
for ($i=0;$i<$taille;$i++)
|
{
|
$size = getimagesize($pics[$i]); // sauvegarde des attributs de l'image
|
switch($size[2])
|
{
|
case 1 :
|
if (imagetypes() & IMG_GIF)
|
$src = imagecreatefromgif($pics[$i]); // l'image est au format gif
|
break;
|
case 2 :
|
if (imagetypes() & IMG_JPG)
|
$src = imagecreatefromjpeg($pics[$i]); // l'image est au format jpeg
|
break;
|
case 3 :
|
if (imagetypes() & IMG_PNG)
|
$src = imagecreatefrompng($pics[$i]); // l'image est au format png
|
break;
|
default :
|
if (preg_match("/\.wbmp$/", $pics[$i]) && (imagetypes() & IMG_WBMP))
|
{
|
$src = imagecreatefromwbmp($pics[$i]);
|
$size[0] = imagesx($src);
|
$size[1] = imagesy($src);
|
if (!isset($format))
|
$format = 4;
|
}
|
|
}
|
if ($src == '')
|
{
|
$thumbs[$pics[$i]] = "format non supporte";
|
}
|
else
|
{
|
$destW = $size[0]*$tnH/$size[1];
|
$destH = $tnH;
|
$dest = imagecreate($destW, $destH); // creation de l'image de destination
|
imagecopyresized($dest, $src, 0, 0, 0, 0, $destW, $destH, $size[0], $size[1]);
|
|
$tn_name = $pics[$i];
|
/* renommage du fichicer de destination (ajout de "_tn") */
|
$tn_name = preg_replace("/\.(gif|jpe|jpg|jpeg|png|wbmp)$/i", "_tn", $tn_name);
|
/* changement du chemin d'acces de $dirSrc en $dirDest */
|
$tn_name = preg_replace("/.*\/([^\/]+)$/i", "$dirDest\\1", $tn_name);
|
if (isset($format))
|
$type = $format;
|
else
|
$type = $size[2];
|
switch($type)
|
{
|
case 1 :
|
if (imagetypes() & IMG_GIF)
|
{
|
imagegif($dest, $tn_name.".gif");
|
$thumbs[$pics[$i]] = "$tn_name.gif";
|
}
|
break;
|
case 2 :
|
if (imagetypes() & IMG_JPG)
|
{
|
imagejpeg($dest, $tn_name.".jpg");
|
$thumbs[$pics[$i]] = "$tn_name.jpg";
|
}
|
break;
|
case 3 :
|
if (imagetypes() & IMG_PNG)
|
{
|
imagepng($dest, $tn_name.".png");
|
$thumbs[$pics[$i]] = "$tn_name.png";
|
}
|
break;
|
default :
|
if (imagetypes() & IMG_WBMP)
|
{
|
imagewbmp($dest, $tn_name.".wbmp");
|
$thumbs[$pics[$i]] = "$tn_name.wbmp";
|
}
|
}
|
if (!($thumbs[$pics[$i]]))
|
{
|
$thumbs[$pics[$i]] = "format non supporte";
|
}
|
}
|
}
|
return ($thumbs);
|
}
|
|
| ?> |