script PHP avec comportement bizarre
Posté : 06 juin 2008, 14:15
Bonjour, j'utilise le code suivant pour faire un album photo. Malheureusement je ne sais pas pourquoi sur certains répertoires, il m'affiche 10 à 15 photos puis un bout du code PHP lui-même... par exemple à la place de la 16° image, il me met "<img src=./toto" et après plus rien (ni les autres photos ni quoi que ce soit...) Je pense que ça vient d'un pb mémoire du serveur (hébergé chez Free, c'est un site perso) mais je voudrais avoir votre avis
<?
// ************************************************************************ //
function resize_photo($fichier_photo,$nom_mini,$larg_mini,$haut_mini,$quality,$nomrep)
{
//$nomrep = init_var($_GET["rep"]);
$photo = imagecreatefromjpeg("$nomrep/$fichier_photo");
$tab = getimagesize("$nomrep/$fichier_photo");
$larg_photo = $tab[0];
$haut_photo = $tab[1];
if ($larg_photo/$haut_photo > intval(4/3))
$haut_mini = $haut_photo*$larg_mini/$larg_photo;
elseif ($larg_photo/$haut_photo != intval(4/3))
$larg_mini = $larg_photo*$haut_mini/$haut_photo;
$im = ImageCreateTrueColor($larg_mini,$haut_mini);
ImageCopyResampled($im,$photo,"0","0","0","0",$larg_mini,$haut_mini,$larg_photo,$haut_photo);
Imagejpeg($im,$nomrep."/".$nom_mini, $quality);
imagedestroy($im);
imagedestroy($photo);
}
// ************************************************************************ //
function test_image($photo)
{
$nom_fichier = basename($photo);
if (strlen($nom_fichier) < 5) return false;
$debut = substr($nom_fichier,0,1);
if ($debut=="_") return false;
$extension = strtolower(substr($nom_fichier,-4));
if ($extension==".jpg") return true;
return false;
}
// ************************************************************************ //
function init_var(&$var)
{
if (!isset($var))
$var='';
return $var;
}
// ************************************************************************ //
function liste_rep($nomrep)
{ $liste_photos=array();
echo "<br><div align=center><table border=0 cellspacing=5>";
$rep_ouvert = opendir (init_var($_GET["rep"]));//opendir(".");
//$nomrep = init_var($_GET["rep"]);
$cpt_ligne = 0;
while ($lu=readdir($rep_ouvert))
if ((test_image($lu))) $liste_photos[] = $lu;
closedir($rep_ouvert);
sort($liste_photos, SORT_STRING);
for ($cpt=0;$cpt<sizeof($liste_photos);$cpt++)
{ $lu = $liste_photos[$cpt];
$annee=substr($lu,0,4);
$mois=substr($lu,5,2);
$jour=substr($lu,8,2);
$tab = getimagesize("$nomrep/$lu",$info);
$iptc = iptcparse($info["APP13"]);
$caption = $iptc["2#120"][0];
if (test_image("$lu"))
{
$cpt_ligne++;
if ($cpt_ligne%8==1) echo "<tr>\n";
echo "<td valign=center align=center colspan=2>";
echo "<a href=\"$_SERVER[PHP_SELF]?rep=$nomrep&photo=$lu\">";
$nom_mini = "_pre_".$lu;
if (!file_exists($nom_mini)){
resize_photo($lu,$nom_mini,120,90,50,$nomrep);
echo "<img src=\"$nomrep/$nom_mini\" alt=\"$jour/$mois/$annee : $caption\">"; }
if ($cpt_ligne%8==0) echo "</tr>";
}
}
//if ($cpt_ligne%3!=0) echo "<td colspan=2> </td></tr>";
echo "</table></div>";
}
// ************************************************************************ //
function aff_photo($photo,$reduc,$diaporama, $nomrep){
$tab_photos=array();
$rep_ouvert = opendir (init_var($_GET["rep"]));
while ($lu=readdir($rep_ouvert))
if ((test_image($lu))) $tab_photos[] = $lu;
closedir($rep_ouvert);
sort($tab_photos, SORT_STRING);
$indice=-1;
for ($cpt=0;$cpt<sizeof($tab_photos);$cpt++)
if ($tab_photos[$cpt] == $photo) $indice=$cpt;
/*********************************************************************/
/************** Liens ***********************/
/*********************************************************************/
echo "<div align=\"left\">";
if ($diaporama==0) echo "<a href=\"$_SERVER[PHP_SELF]?photo=$photo&diaporama=1&rep=$nomrep\">Débuter le Diaporama</a>";
else echo "<a href=\"$_SERVER[PHP_SELF]?photo=$photo&rep=$nomrep\">Arrêt du Diaporama</a>";
echo "<br><a href=\"listerep.php\">Menu principal</a>";
echo "<br><a href=\"$_SERVER[PHP_SELF]?rep=$nomrep\">Revenir aux imagettes</a>";
echo "</div>";
/*********************************************************************/
if ((!test_image($photo)) || (!file_exists($nomrep."/".$photo)) || ($indice== -1))
{
echo "<h2 align=center>Le fichier '$photo' '$indice' INVALIDE)</h2>";
return false;
}
$taille = sizeof($tab_photos);
echo "<br><div align=center><table border=0 cellspacing=5>";
echo "<tr><td valign=center align=center colspan=7>";
/*********************************************************************/
/************** Image ***********************/
/*********************************************************************/
$tab = getimagesize("$nomrep/$photo",$info);
$larg_photo = $tab[0];
$haut_photo = $tab[1];
if (($reduc) && ($larg_photo>490 || $haut_photo>370)){
echo "Pour voir la photo en résolution maximale cliquez sur l'image...</i><br>";
//$extension = strtolower(substr($nom_fich,-4));
$annee=substr($photo,0,4);
$mois=substr($photo,5,2);
$jour=substr($photo,8,2);
$nom_moy = _480_.$photo;
if (!file_exists($nomrep."/".$nom_moy))
resize_photo($photo,$nom_moy,480,360,90,$nomrep);
echo "<a target=\"_blank\" href=\"$nomrep/$photo\"><img src=\"$nomrep/$nom_moy\" alt=$fichier_photo></a>";
echo "</tr><tr><td valign=center align=center colspan=7>";
echo ($jour."/".$mois."/".$annee);
echo "</td></tr>";
echo "<tr><td valign=center align=center colspan=7>";
$iptc = iptcparse($info["APP13"]);
$caption = $iptc["2#120"][0];
echo ($caption);
echo "</td></tr>";
if ($diaporama==1){
echo "<script>";
if ($indice==$taille-1) $next=$tab_photos[0];
else $next=$tab_photos[$indice+1];
echo "window.setTimeout('location.replace(\"$_SERVER[PHP_SELF]?photo=$next&diaporama=1&rep=$nomrep\")',5000)";
echo "</script>";
echo "</BODY></HTML>";
}
}
else
{
echo "<div align=\"center\">$nomrep/$photo</div><br>
<div align=center>
<img src=\"$nomrep/$photo\" alt=\"$photo\"><br>";
echo "</div>";
}
/*********************************************************************/
/************** Bandeau imagettes ***********************/
/*********************************************************************/
echo "<tr>\n";
echo "<td valign=center align=center>";
if ($indice>0)
{
$next=$tab_photos[0];
echo "<a href=\"$_SERVER[PHP_SELF]?photo=$next&rep=$nomrep\">Premier <br></a>";
}
if ($indice>5)
{
$next=$tab_photos[$indice-5];
echo " <a href=\"$_SERVER[PHP_SELF]?photo=$next&rep=$nomrep\"> << <br></a>";
}
if ($indice>0)
{
$next=$tab_photos[$indice-1];
echo " <a href=\"$_SERVER[PHP_SELF]?photo=$next&rep=$nomrep\"> < </a>";
}
for($i=0;$i<5;$i++)
{
$j=intval(abs($indice/5))*5+$i;
$lu=$tab_photos[$j];
if ($j<$taille)
{
echo "<td valign=center align=center>";
echo "<a href=\"$_SERVER[PHP_SELF]?photo=$lu&rep=$nomrep\">";
$nom_mini = "_pre_".$lu;
$k=$j+1;
echo "<img src=\"$nomrep/$nom_mini\" border=0 alt=$lu></a>";
}
}
echo "<td valign=center align=center>";
if ($indice<$taille-1)
{
$next=$tab_photos[$indice+1];
echo " <a href=\"$_SERVER[PHP_SELF]?photo=$next&rep=$nomrep\"> > <br></a>";
}
if ($indice<$taille-5)
{
$next=$tab_photos[$indice+5];
echo " <a href=\"$_SERVER[PHP_SELF]?photo=$next&rep=$nomrep\"> >> <br></a>";
}
if ($indice<$taille-1)
{
$next=$tab_photos[$taille-1];
echo " <a href=\"$_SERVER[PHP_SELF]?photo=$next&rep=$nomrep\"> Dernier</a>";
}
echo "</tr>";
}
// ************************************************************************ //
$photo = init_var($_GET["photo"]);
$zoom = init_var($_GET["zoom"]);
$diaporama = init_var($_GET["diaporama"]);
$repertoire = opendir (init_var($_GET["rep"]));
$nomrep = init_var($_GET["rep"]);
if ($zoom>0) $reduc = false;
else $reduc = true;
$photo = str_replace('../','',$photo);
$photo = str_replace('http://','',$photo);
if (!empty($photo)) $rep = dirname($photo);
$nb_photos=0;
while ($lu=readdir($repertoire))
if ((test_image($lu))) $nb_photos++;
if ($nb_photos!=0)
{
echo "<b>($nb_photos photo</b>";
if ($nb_photos>1) echo "<b>s</b>";
echo "<b>)</b>";
}
if (empty($photo)) liste_rep($nomrep);
else
{//$photo=$nomrep."/".$photo;
aff_photo($photo,$reduc,$diaporama,$nomrep);
echo "</td></tr><tr></table>"; }
?>
</body>
</html>