Location: PHPKode > scripts > Simple Gallery > simple-gallery/sgallery.class.php
<?
/*************************************************************************/
/*************************************************************************/
/**  Class written by: Mike Rosas ©2007                                 **/
/**  -----------------------------------------------------------------  **/
/**  This class can manage a gallery with out using MySQL.              **/
/**  It reads the images directly from the directories                  **/
/**  All you have to do is to initialize the class and point to         **/
/**  the directory containing the images subdirectory and the           **/
/**  thumbs subdirectory.                                               **/
/**                                                                     **/
/**  More info on readme.txt                                            **/
/**  -----------------------------------------------------------------  **/
/*************************************************************************/
/*************************************************************************/


Class sGallery{

	var $dirs;
	var $name;
	var $images = array();
	var $thumbs = array();
	var $nimg;
	var $nthu;
	
	var $error = array();
	var $hcolor;
	var $fcolor;
	var $bcolor;
	var $border;
	var $nav;

	function sGallery($name="", $d="."){
		$this->dirs = $d;
		$this->nimg = 0;
		$this->nthu = 0;
		$this->name = $name;
		$this->style();

		if(!($d = opendir($this->dirs."/images"))) array_push($this->error,"<b>ERROR:</b> Can't open image dir<br>");
		while ($f = readdir($d)) {
			if (eregi("\.jpg",$f) || eregi("\.jpeg",$f) || eregi("\.png",$f)){ 
				array_push($this->images,"$f");
				$this->nimg++;
			}
		}
		if(!$this->nimg) array_push($this->error,"<b>ERROR:</b> No files found in image dir<br>");

		if(!($d = opendir($this->dirs."/thumbs"))) array_push($this->error,"<b>ERROR:</b> Can't open thumb dir<br>");
		while ($f = readdir($d)) {
			if (eregi("\.jpg",$f) || eregi("\.jpeg",$f) || eregi("\.png",$f)){ 
				array_push($this->thumbs,"$f");
				$this->nthu++;
			}
		}
		if(!$this->nthu) array_push($this->error,"<b>ERROR:</b> No files found in thumb dir<br>");
		if($this->nimg != $this->nthu) array_push($this->error,"<b>ERROR:</b> images != thumbs<br>");
	}

	function style($hcolor = "NULL", $bcolor = "NULL", $fcolor = "NULL", $border=0, $nav="auto"){
		$this->hcolor = $hcolor;
		$this->fcolor = $fcolor;
		$this->bcolor = $bcolor;
		$this->border = $border;
		$this->nav    = $nav;
	}

	function show($p, $nc, $nr, $k){
		if(!$p)   $p = 0;
		if(!$nc) $nc = 0;
		if(!$nr) $nr = 0;
		
		if(count($this->error)){
			echo "<pre>Some errors were found.<br>The gallery wont run until they are solved:<br><br>";
			foreach($this->error as $er)
				echo $er;
			echo "<pre>";
		}else{
			if($k == "") 
				$this->showpage($p, $nc, $nr);
			else
				$this->showimage($p, $k);
		}
	}

	function showpage($p=0, $nc=5, $nr=5){
		if($p=="") $p=0;
		$nip = $nc * $nr;
		$np = floor($this->nthu/$nip);

		$fi = $p * $nip;
		$li = (($p + 1) * $nip) - 1;
		if($li >= $this->nthu) $li = $this->nthu-1;

		//echo "<pre>p:$p<br>nc:$nc<br>nr:$nr<br>nip:$nip<br>np:".($np+1)."<br>fi:$fi<br>li:$li</pre>";

		echo "<table border='$this->border' width='100%' cellspacing='0' cellpadding='0'><tr><td width='100%' ";
		if($this->hcolor != "NULL") echo "bgcolor='#$this->hcolor'";
		echo ">&nbsp;<b>&middot $this->name</b></td></tr><tr><td width='100%' ";
		if($this->bcolor != "NULL") echo "bgcolor='#$this->bcolor'";
		echo "><table border='0' width='100%' cellpadding='0' cellspacing='5'>";

		if(eregi("\?k=",$_SERVER['REQUEST_URI'])) {
			$aurl=split("?k=",$_SERVER['REQUEST_URI']);
		}else if(eregi("\&k=",$_SERVER['REQUEST_URI'])) {
			$aurl=split("&k=",$_SERVER['REQUEST_URI']);
		}else{
			$aurl[0]=$_SERVER['REQUEST_URI'];
		}

		$url = $aurl[0];
		if(eregi("\?", $url)) 
			$url .= "&k";
		else
			$url .= "?k";

		for($i=$fi;$i<=$li;$i++){
			if(!($i%$nc)) echo "</tr><tr>";
			echo "<td align='center'>";
			echo "<a href='$url=$i'><img src='".$this->dirs."/thumbs/".$this->thumbs[$i]."' border='0'></a>";
			echo "</td>";
		}

		echo "</tr>";

		$fp = 0;
		$lp = $np;
		$pp = $p-1; if(($p-1) < $fp) $pp = 0;
		$pn = $p+1; if(($p+1) > $np) $pn = $np;

		if(eregi("\?p=",$_SERVER['REQUEST_URI'])) {
			$aurl=split("?p=",$_SERVER['REQUEST_URI']);
		}else if(eregi("\&p=",$_SERVER['REQUEST_URI'])) {
			$aurl=split("&p=",$_SERVER['REQUEST_URI']);
		}else{
			$aurl[0]=$_SERVER['REQUEST_URI'];
		}

		$url = $aurl[0];
		if(eregi("\?", $url)) 
			$url .= "&p";
		else
			$url .= "?p";

		echo "</table></td></tr><tr><td width='100%' ";
		if($this->fcolor != "NULL") echo "bgcolor='#$this->fcolor'";
		echo "><p align='center'><b>";
		echo "<a href='$url=$fp'>&lt;&lt;</a>&nbsp;";
		echo "<a href='$url=$pp'>&lt;</a>&nbsp;|&nbsp;";

		for($i=0;$i<=$np;$i++)
			echo "<a href='$url=$i'>".($i+1)."</a>&nbsp;|&nbsp;";

		echo "<a href='$url=$pn'>&gt;</a>&nbsp;";
		echo "<a href='$url=$lp'>&gt;&gt;</a>";
		echo "</b></td></tr></table>";
	}

	function showimage($p, $k){

		$fk = 0;
		$lk = ($this->nimg-1);
		$pk = $k-1; if(($k-1) < 0) $pk = 0;
		$nk = $k+1; if(($k+1) >= $this->nimg) $nk = ($this->nimg-1);

		if(eregi("\?k=",$_SERVER['REQUEST_URI'])) {
			$aurl=split("?k=",$_SERVER['REQUEST_URI']);
		}else if(eregi("\&k=",$_SERVER['REQUEST_URI'])) {
			$aurl=split("&k=",$_SERVER['REQUEST_URI']);
		}else{
			$aurl[0]=$_SERVER['REQUEST_URI'];
		}

		$url = $aurl[0];
		if(eregi("\?", $url)) 
			$url .= "&k";
		else
			$url .= "?k";

		echo "<table border='$this->border' width='100%' cellspacing='0' cellpadding='0'>";
		
		if($this->nav == "auto" || $this->nav == "up"){
			echo "<tr><td width='100%' valign='top' align='center' ";
			if($this->hcolor != "NULL") echo "bgcolor='#$this->fcolor'";
			echo "><table border='0' width='100%' cellpadding='0'><tr>";
			echo "<td width='20%' valign='top' align='center'>[<a href='$url=$fk'>First</a>]</td>";
		    echo "<td width='20%' valign='top' align='center'>[<a href='$url=$pk'>Previous</a>]</td>";
			echo "<td width='20%' valign='top' align='center'>[<a href='$url='>Index</a>]</td>";
	        echo "<td width='20%' valign='top' align='center'>[<a href='$url=$nk'>Next</a>]</td>";
		    echo "<td width='20%' valign='top' align='center'>[<a href='$url=$lk'>Last</a>]</td>";
			echo "</tr></table></td></tr>";
		}
		
		echo "<tr><td width='100%' valign='top' align='center'>";

		echo "<img border='0' src='".$this->dirs."/images/".$this->thumbs[$k]."'>";

		echo "</td></tr>";
		
		if($this->nav == "auto" || $this->nav == "down"){
			echo "<tr><td width='100%' valign='top' align='center' ";
			if($this->hcolor != "NULL") echo "bgcolor='#$this->fcolor'";
			echo "><table border='0' width='100%' cellpadding='0'><tr>";
			echo "<td width='20%' valign='top' align='center'>[<a href='$url=$fk'>First</a>]</td>";
		    echo "<td width='20%' valign='top' align='center'>[<a href='$url=$pk'>Previous</a>]</td>";
			echo "<td width='20%' valign='top' align='center'>[<a href='$url='>Index</a>]</td>";
	        echo "<td width='20%' valign='top' align='center'>[<a href='$url=$nk'>Next</a>]</td>";
		    echo "<td width='20%' valign='top' align='center'>[<a href='$url=$lk'>Last</a>]</td>";
			echo "</tr></table></td></tr>";
		}
		
		echo "</table>";
	}

	function test(){
		echo "<pre>name: $this->name<br>nimg: $this->nimg<br>nthu: $this->nthu<br>dirs: $this->dirs<br>";
		echo "images:";
		print_r($this->images);
		echo "<br>thumbs:";
		print_r($this->thumbs);
		echo "</pre>";
	}

}

?>
Return current item: Simple Gallery