Class galerie photo

Contenu du snippet

Je poste cette source en tant que débutant des class.
Après des années à faire du php tjrs a la vieille mode limite du php3, j'ai donc entrepris de créer une class pour voir comment ça marché vraiment...

Ce script bien que fonctionnel est surement loin d être correct et optimisé... je le poste d ailleurs pour ça car j'aimerai que l'on m'explique ( et pas critique hein ) mes erreurs.

Alors cette class donc:
après récupération du nom du répertoire dans une table sql, vérification si le répertoire existe bien, affiche donc les photos du répertoire par page.

j'attend des commentaires constructif pour me permettre de comprendre ce que j'ai mal fait
Merci à vous :)

Source / Exemple :


<?php 

	   class MyException extends Exception { 
		    public function __construct($msg=NULL, $code=0) 
		    { 
		        parent::__construct($msg, $code); 
		    }  
		} 
  
  
	class Gallery {
		private $rw;
		private $rh;
		private $rtop;
		private $w;
		private $h;
		private $pic;
		private $nbpage; 
		
		public $nombre_pic;
		public $page;
		public $page_back;
		public $page_next;
		public $affiche_pic;
		public $realpath;
		public $webpath;
		public $nombre_ppage;
		public $name_gallery;
		public $type;

		
		   public function __construct() {
			}
		
			public function data($rep,$type,$w,$h,$nbpp) { 
			if(is_dir($this->realpath.$rep)) {
				$nbfiles=0;
				$Dir=new RecursiveDirectoryIterator($this->realpath.$this->name_gallery);
				foreach (new RecursiveIteratorIterator($Dir) as $filename=>$cur) {
					if($cur->getFilename()!="index.html") {
						$nbfiles++;
						$this->pic[]=$cur->getFilename();
					}
				}
				sort($this->pic);
				$this->nombre_pic = count($this->pic);
				$this->name_gallery = $rep; //repertoir des images
				$this->type = $type; //type d'affichage
				$this->w = $w; // largeur maxi de l'image
				$this->h = $h; // hauteur maxi de l'image
				$this->nombre_ppage = $nbpp;	// nombre par page
				if($this->w==0) { $this->w=300; }
				elseif($this->w>1000) { $this->w=1000; }   // test des dimention demander
				if($this->h==0) { $this->h=300; }
				elseif($this->h>1000) { $this->h=1000; }	
			} 
			else
			 {
		 		throw new MyException('Erreur de repertoire'); 
		 	 }
		}			
		  	
		// redimention d'image
		public function RedimImage($img) {
		if(GetImageSize("".$this->realpath."".$this->name_gallery."/".$img."")) { 
		   	$dst_w = $this->w;
		   	$dst_h = $this->h;
		   	$size = GetImageSize("".$this->realpath."".$this->name_gallery."/".$img.""); // taille reel de l'image 
		   	$src_w = $size[0]; 
		   	$src_h = $size[1];
		  		$test_h = round(($dst_w / $src_w) * $src_h);
		  		$test_w = round(($dst_h / $src_h) * $src_w);
		  		
			if(!$dst_h) { $dst_h = $test_h; }
		  		elseif(!$dst_w) { $dst_w = $test_w; }
		  		elseif($test_h>$dst_h) { $dst_w = $test_w; }
		  		else { $dst_h = $test_h; }
		  
		 	if($dst_h > 1 && $dst_h < $this->h){	// centre l'image
				$top = ceil(($this->h - $dst_h) / 2);
			} else { $top = 0; }  
			
			$this->rtop = $top;
			$this->rw = $dst_w;
			$this->rh = $dst_h;
		 }
		 else
		 {
		 	throw new MyException('Erreur de redimention'); 
		 }
		}
		
		
		public function ViewGallery($page=0) {
			$this->page = $page; // recup de la page en cour
			$this->nbpage = round($this->nombre_pic / $this->nombre_ppage);	// nombre de page
			
			if($this->page > $this->nbpage-1) { $this->page=0; } // si page en cour plus grand que le nombre de page on retourne au debut
			
			if($this->page!=0) { $this->page_back = $this->page-1; } else { $this->page_back = 0; }	// page retour
			if($this->page+1 < $this->nbpage) { $this->page_next = $this->page+1; } else { $this->page_next = 0; }	// page suivant
			
			$varpic_debut = $this->page*$this->nombre_ppage;
			$varpic_fin = ($this->page+1)*$this->nombre_ppage; 
			
							foreach ($this->pic as $key => $value) {
						  if($key>=$varpic_debut && $key<$varpic_fin) { 
						  $this->RedimImage($value);
						  $args = array($value,
						  				$this->rw,
										$this->rh,
										$this->rtop);
						  $this->affiche_pic[] = $args;
					  } 
				  }
		} 
}
?>

<?php
// contenue de la page 
try{ 
$gallery = new Gallery();
$gallery->realpath = "/home/website/public_html/files/images/";
$gallery->webpath = "../files/images/";
$gallery->data('MA_DIR_equipe2','1','300','300','10'); 
$gallery->ViewGallery($_GET['page']);
}
catch(MyException $e)
{
echo 'Oops un bug : ' .$e->getMessage() . ' à la ligne : ' . $e ->getLine();
}

?> 

<table>	
	<td><a href="?op=test&page=<?= $gallery->page_back ?>"> back </a></td>
	<td><?php for($i=0;$i<count($gallery->affiche_pic);$i++) { echo '<img src="'.$gallery->webpath.$gallery->name_gallery.'/'.$gallery->affiche_pic[$i][0].'" width="'.$gallery->affiche_pic[$i][1].'" height="'.$gallery->affiche_pic[$i][2].'" style="margin-bottom:'.$gallery->affiche_pic[$i][3].'px"/>'; }  ?></td>
	<td><a href="?op=test&page=<?= $gallery->page_next ?>"> next </a></td>
	</tr>
</table>

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.