 | | Offres d'emplois |  | Développeurs PHP/MYSQL H/F Owlient est une société en forte croissance concevant des jeux en ligne pour des communautés de passionnés.
Sur le sujet de l'équitation, le prem... | |
 | | Avez vous lu ? |  | | |
|
 Créer une galerie photo - galerie.inc.php | |  |

<?php
/*
//<--- Le code complet de la galerie photos - galerie.inc.php
*/
define("IMG_PATH", "images/");
define("IMG_MINI_PATH", IMG_PATH . "miniatures/");
define("IMG_PAGE", 6);
define("NB_IMG_ROW", 3);
define("TABLE_WIDTH", '100%');
define("TD_WIDTH", '33%');
define("REDUC_COEFF", 0.5);
$action = (isset($_REQUEST['action']) ?
$_REQUEST['action'] : '');
function menu() {
$txt = '<center>';
$txt .= '<a href="' . $_SERVER['PHP_SELF'] .
'?action=voir">Voir la galerie</a>
<a href="' . $_SERVER['PHP_SELF'] .
'?action=ajout_form">Ajouter une image</a>';
$txt .= '</center>';
return $txt;
}
function error_message($msg) {
echo "<SCRIPT>alert(\"Erreur : $msg\");
history.go(-1)</SCRIPT>";
exit;
}
function file_taille($valeur) {
$txt = '';
if ($valeur>=1000000) {
$txt = $valeur/1000000;
$txt = number_format($txt, 2)." Mo";
}
elseif ($valeur>=1000) {
$txt = $valeur/1000;
$txt = number_format($txt, 2)." Ko";
}
elseif ($valeur>=0) {
$txt= $valeur." octect";
if ($valeur>0) {
$txt .= "s";
}
else {
$txt = $valeur;
}
}
else {
$txt = $valeur;
}
return $txt;
}
function download_error($error) {
switch($error) {
case"1":
error_message("Le fichier que vous souhaitez
téléchargé dépasse le poids autorisé sur le serveur !");
break;
case"3":
error_message("Le fichier n'a été que partiellement
téléchargé sur le serveur !");
break;
case"4":
error_message("Aucun fichier n'a été téléchargé
sur le serveur !");
break;
}
}
function voir() {
make_vignette(IMG_PATH);
$tab_contenu = array();
if (strlen(IMG_MINI_PATH) > 0 && is_dir(IMG_MINI_PATH)) {
if ($pointeur = opendir(IMG_MINI_PATH)) {
while (false !== ($file = readdir($pointeur))) {
if ($file != "." && $file != "..") {
(is_file(IMG_MINI_PATH . $file) ? $tab_contenu[] .= "$file" : '');
}
}
closedir($pointeur);
}
}
if (count($tab_contenu) > 0) {
$page_cour = (isset($_GET['page_cour']) ? $_GET['page_cour'] : 0);
$nombre_total = count($tab_contenu);
$numero_page = $page_cour + 1;
$nombre_total_pages =
$numero_derniere_page = ceil($nombre_total/IMG_PAGE);
if ($nombre_total_pages > 1) {
echo '<br><center>';
echo 'Page ' . $numero_page .
' de ' . $nombre_total_pages . '<br>';
if($numero_page > 1) {
$page_precedente = $page_cour - 1;
echo '<A HREF="' . $_SERVER['PHP_SELF'] .
'?action=voir&page_cour=' .
$page_precedente . '">[Précédent]</A> ';
}
for($i=0;$i<$nombre_total_pages;$i++) {
echo ' [<a href="' . $_SERVER['PHP_SELF'] .
'?action=voir&page_cour=' .
$i . '">' . ($i+1) . '</a>] ';
}
if($numero_page < $nombre_total_pages) {
$page_suivante = $page_cour + 1;
$derniere_page = $nombre_total_pages - 1;
echo '<A HREF="' . $_SERVER['PHP_SELF'] .
'?action=voir&page_cour=' .
$page_suivante . '">[Suivant]</A> ';
}
echo '</CENTER><br>';
}
$tab_contenu =
array_slice($tab_contenu, $page_cour * IMG_PAGE, IMG_PAGE);
echo '<table align="center"
width="' . TABLE_WIDTH . '" border="0"
cellspacing="5" cellpadding="0">';
for($i=0;$i<count($tab_contenu);$i++) {
$tb_size =
getimagesize(IMG_MINI_PATH.$tab_contenu[$i]);
echo (($i%NB_IMG_ROW) == 0 ? '<tr>' : '');
echo '<td align="center" width="' . TD_WIDTH . '">
<a href="' . $_SERVER['PHP_SELF'] .
'?action=view&img=' . $tab_contenu[$i] . '">
<img src="' . IMG_MINI_PATH.$tab_contenu[$i] . '" border="0"
width="' . $tb_size[0] . '" height="' . $tb_size[1] . '"></a></td>';
echo ((($i+1)%NB_IMG_ROW) == 0 ? '</tr>' : '');
}
echo '</table>';
}
echo '<center>actuellement dans la galerie : ' .
$nombre_total .
($nombre_total <= 1 ? ' image ' : ' images ') .
'</center>';
}
function view() {
$fichier = str_replace("mini_", "", $_GET['img']);
$path = IMG_PATH . $fichier;
$date_modif = date("d/m/Y H:i:s.", filectime($path));
list($width, $height, $type, $attribut) = getimagesize($path);
$poids = file_taille(filesize($path));
switch($type) {
case"1":
$chaine_type = 'GIF';
break;
case"2":
$chaine_type = 'JPG';
break;
case"3":
$chaine_type = 'PNG';
break;
case"4":
$chaine_type = 'SWF';
break;
case"5":
$chaine_type = 'PSD';
break;
case"6":
$chaine_type = 'BMP';
break;
case"7":
$chaine_type = 'TIFF';
break;
}
$txt = '<table align="center" border="0"
cellspacing="5" cellpadding="0">';
$txt .= '<tr><td align="center">' . $fichier .
' (' . $poids . ')</td></tr>';
$txt .= '<tr><td align="center">
<img src="' . $path . '" border="1"
bordercolor="#FF0000" ' . $attribut . '></td></tr>';
$txt .= '<tr><td align="center">';
$txt .= '<table align="center" border="0"
cellspacing="5" cellpadding="0">';
$txt .= '<tr><td align="center">Date de dernière
modification : ' . $date_modif . '</td></tr>';
$txt .= '<tr><td align="center">
Type d\'image : ' . $chaine_type . '</td></tr>';
$txt .= '<tr><td align="center">
Largeur de l\'image : ' . $width . ' px.</td></tr>';
$txt .= '<tr><td align="center">
Hauteur de l\'image : ' . $height . ' px.</td></tr>';
$txt .= '</table>';
$txt .= '</td></tr>';
$txt .= '</table>';
echo $txt;
}
function ajout_form() {
?>
<center><h2>Ajouter une image à
la galerie</h2></center>
<table align="center" width="50%" border="0"
cellspacing="5" cellpadding="0">
<form action="index.php" method="post"
enctype="multipart/form-data">
<input type="hidden" name="action" value="ajout">
<tr>
<td width="50%" align="right">Ajoutez une
image à la gallerie :</td>
<td width="50%"><input type="file" name="fichier"></td>
</tr>
<tr><td colspan="2"><hr></td></tr>
<tr><td colspan="2" align="center">
<input type="submit" name="Submit" value="Ajouter">
</td></tr>
</form>
</table>
<?
}
function ajout() {
download_error($_FILES["fichier"]["error"]);
if (move_uploaded_file($_FILES["fichier"]["tmp_name"],
IMG_PATH.$_FILES["fichier"]["name"])) {
$path = IMG_PATH.$_FILES["fichier"]["name"];
chmod($path, 0777);
}
else {
echo '<SCRIPT>
alert("Le fichier n\'a pas été téléchargé sur le serveur !");
location.href="' . $PHP_SELF . '";</SCRIPT>';
}
}
function make_vignette($dir) {
$tab_contenu = array();
if (strlen($dir) > 0 && is_dir($dir)) {
if ($pointeur = opendir($dir)) {
while (false !== ($file = readdir($pointeur))) {
if ($file != "." && $file != "..") {
(is_file($dir . $file) ? $tab_contenu[] .= "$file" : '');
}
}
closedir($pointeur);
}
if (count($tab_contenu) > 0) {
for($i=0;$i<count($tab_contenu);$i++) {
$path = IMG_PATH.$tab_contenu[$i];
if ($size = @getImageSize($path)) {
$new_file = "mini_" . $tab_contenu[$i];
$dest_path = IMG_MINI_PATH.$new_file;
$dest_width = $size[0] * REDUC_COEFF;
$dest_eight = $size[1] * REDUC_COEFF;
switch ($size[2]) {
case 1:
$src_img = @imageCreateFromGif($path);
break;
case 2:
$src_img = @imageCreateFromJpeg($path);
break;
}
$dst_img = imagecreatetruecolor($dest_width,$dest_eight);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dest_width,
$dest_eight, $size[0], $size[1]);
imagejpeg($dst_img, $dest_path, 100);
imagedestroy($dst_img);
}
}
}
}
}
?>
|
|
|
 |
|