Gallery Script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Gallery Script

Post by John Cartwright »

Code: Select all

<?php


include("inc/mysql.php");
include("inc/design1.php");
$desLink = new Design;

//Title for your gallery that will be used with the default header.
$title="";

//Thumbnail size. The minimum width and height in pixels, that you would like the thumbnails to be. Photos are scaled.
$twidth=100;
$theight=100;

//Photo size. The minimum width and height in pixels, that you would like you photos scaled to when displayed.
$pwidth = 640;
$pheight = 640;

//The thumbnail gallery is displayed in a table. Please choose the number of rows and columns you would like.
$rows=3;
$cols=3;

//If you would like the filename of the photo to be displayed under the thumbnail change this setting.
//0=off 1=on
$showfilename=0;

//If you would like photo to up a new window when clicked change this setting.
//The defualt is to open the image up in the same browser window.
//0=off 1=on
$window=0;

//If you would like to use a custom header or footer please add the file names here and they will be included in the script.
//Otherwise the plain jane default will be used. The files should be in the same folder as the script, but if you provide an
//alternate path it will work as well. Example: header.html, header.php. 
$header="NULL";
$footer="NULL";

//No need to mess with anything below here.
//Part of this function comes from http://www.php.net/manual/en/function.getimagesize.php
function thumb_maker($filename, $minwidth, $minheight) {
	if (file_exists($filename."thmb.jpg")) {
		$photosize = getimagesize($filename."thmb.jpg");
		if (max($minwidth,$minheight)!=max($photosize[0],$photosize[1])) {
		unlink($filename."thmb.jpg");
		}		
	}
	if (!file_exists($filename."thmb.jpg") && file_exists($filename)) {
		echo "<br/>One moment please....creating a thumbnail.";
		set_time_limit(60);
		$photosize = getimagesize($filename);
	    // Get image size and scale ratio
	    $scale = min($minwidth/$photosize[0], $minheight/$photosize[1]);
		if ($scale < 1) {
		   $width = floor($scale*$photosize[0]);
		   $height = floor($scale*$photosize[1]);
		}
		else {
		   $width = $minwidth;
		   $height = $minheight;
		}
		if ($photosize[mime]=="image/jpeg") {
			$resizedimage = imagecreatetruecolor($width, $height);
			$thumbimage = imagecreatefromjpeg($filename);
			imagecopyresampled($resizedimage, $thumbimage, 0, 0, 0, 0, $width, $height, $photosize[0], $photosize[1]);
			imagejpeg($resizedimage,$filename."thmb.jpg",50);
			imageDestroy($resizedimage); 
			imageDestroy($thumbimage); 
		}
	}
}

//Part of this function comes from http://www.php.net/manual/en/function.getimagesize.php
function gallery_sizer($photo,$minwidth,$minheight) {
	if (file_exists($photo)) {
		$photosize = getimagesize("$photo");
	    # Get image size and scale ratio
	    $scale = min($minwidth/$photosize[0], $minheight/$photosize[1]);
		if ($scale <= 1) {
		   $width = floor($scale*$photosize[0]);
		   $height = floor($scale*$photosize[1]);
		}
		return array($width,$height,$photosize[0],$photosize[1]);
	}
}

function getdirs($dir) {
	$dirs=array();
	chdir($dir);
	if ($handle = opendir($dir)) {
	    while (false !== ($file = readdir($handle))) {
	        if ($file != "." && $file != ".."){
				if (is_dir($file)) {
	        		$dirs[] = $file;
				}
	        }
	    }
	    closedir($handle);
	}
	sort($dirs);
	return $dirs;
}


function getphotos($photodir) {
	$photos=array();
	if ($handle = opendir($photodir)) {
	    while (false !== ($file = readdir($handle))) {
	        if ($file != "." && $file != ".." && !eregi(".jpgthmb.jpg$",$file) && eregi(".jpg$",$file)){
	                $photos[] = $file;
	        }
	    }
	    closedir($handle);
	}
	sort($photos);
	return $photos;
}
		
function thumb_gallery($photonum) {
	global $photos, $photourl, $photodir, $twidth, $theight, $rows, $cols, $showfilename, $album, $window;
		if (($photonum)>(count($photos)-($rows*$cols))) {
			$photonum=(count($photos)-($rows*$cols));
		}
		if (($photonum)<=0) {
			$photonum=0;
		}

		if(isset($album)) { $query="&album=".urlencode($album); }
		for ($tr = 1; $tr <= $rows; $tr++) {
			$photobody[]="<tr>";
			for ($td = 1; $td <= $cols; $td++) {
				$photobody[]="<td align="center">";
				$size=gallery_sizer($photodir."/".$photos[$photonum]."thmb.jpg",$twidth,$theight);
				$link = $photourl."/".$photos[$photonum]."thmb.jpg";
				if ($window==1) { $target="_blank"; } else { $target="_self"; }
				if ($photonum<=count($photos)-1) { $photobody[]="<a href="$_SERVER[PHP_SELF]?p=$photos[$photonum]$query" target="$target"><img src="$link" width="$size[2]" height="$size[3]" alt="$photos[$photonum]"></a><br />"; }
				if ($showfilename==1) { $photobody[]=$photos[$photonum]; }
				$photobody[]="</td>";
				$photonum++;
			}
			$photobody[]="</tr>";
		}
		unset($tr, $td);
	
		#this id down here for a reason don't move it and use array_unshift instead. Trust me.
		if (($photonum-($rows*$cols))>0) { $prev="<a href="$_SERVER[PHP_SELF]?pn=".($photonum-(($rows*$cols)*2)).$query.""><< Previous</a>"; }
		if (($photonum)<(count($photos))) { $next="<a href="$_SERVER[PHP_SELF]?pn=".$photonum.$query."">Next>></a>"; }
		
		$photopage=array();
		$photopage[]="Pages: ";
		for ($pagelink=0, $pagenum=1; $pagelink<=count($photos); $pagelink+=($rows*$cols), $pagenum++) {
			$photopage[]="<a href="$_SERVER[PHP_SELF]?pn=".($pagelink).$query."">$pagenum</a> | ";
		}
		unset($pagelink, $pagenum);
		$photopage=implode("", $photopage);	

		$photobody[]="<tr><td colspan=".$cols."><table width="100%" border="0"><tr><td width="33%"><div align="LEFT">".$prev."</div></td><td width="33%"><div align="CENTER" class="caption">Photos <strong>".($photonum-(($rows*$cols)-1))."</strong> to <strong>".$photonum."</strong> of <strong>".count($photos)."</strong><br />".$photopage."</div></td><td width="33%"><div align="RIGHT">".$next."</div></td></tr></table></td></tr>";
		array_unshift($photobody, "<tr><td colspan=".$cols."><table width="100%" border="0"><tr><td width="33%"><div align="LEFT">".$prev."</div></td><td width="33%"><div align="CENTER">Photos <strong>".($photonum-(($rows*$cols)-1))."</strong> to <strong>".$photonum."</strong> of <strong>".count($photos)."</strong><br />".$photopage."</div></td><td width="33%"><div align="RIGHT">".$next."</div></td></tr></table></td></tr>");

		#put the table tag at the top of the array.
		array_unshift($photobody, '<table width="100%" border="0" cellspacing="0" cellpadding="3">');
		$photobody[]='</table>';

	return $photobody;
}

function photo_gallery($photo, $pwidth, $pheight) {
	global $photodir, $photourl, $thumbsurl, $window;
	$photobody=array();
	
	if (isset($photo)) {
		$size=gallery_sizer("$photodir/$photo",640,640);
		$photobody[]='<table width="100%" border="0" cellspacing="0" cellpadding="3">';
		$photobody[]="<img src="$photourl/$photo">";
		$photobody[]='<tr><td colspan="3" align="center">To save this image to your computer right click on the picture and choose<br /> <strong>''Save Picture As...'' or ''Save Image As...''</strong>.</td></tr>';
		if ($window==1) { $target="<a href="javascript:window.close()">Close Window</a>"; } else { $target="<a href="".$_SERVER[HTTP_REFERER]."">Back</a>"; }
		$photobody[]="<tr class="imageborder"><td>".$target."</td><td align="center"><div align="CENTER" class="caption">Photo <strong>".($photo)."</strong></div></td><td align="right">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>";
		$photobody[]='<tr><td colspan="3" align="center">';
		$photobody[]="<img src="$photourl/$photo" width="$size[0]" height="$size[1]" border=0 alt="" class="imageborder">";
		$photobody[]='</td></tr>';
		$photobody[]="<tr class="imageborder"><td align="center" colspan="3"><div align="CENTER" class="caption">Full size photo size ".$size[2]." pixels wide by ".$size[3]." pixels high.</div></td></tr>";
		$photobody[]="<tr class="imageborder"><td>".$target."</td><td align="center"><div align="CENTER" class="caption">Photo <strong>".($photo)."</strong></div></td><td align="right">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>";
		$photobody[]='</table>';
	}
	return $photobody;
}

function display($photobody) {
	global $photodir;
	getheader();
	albums($photodir);
	foreach ($photobody as $value) {
	   echo $value."\n";
	}
	
	getfooter();
	exit;
}

function albums($dir) {
//for now only one level of albums works.
	global $album, $title;
	$dirs=getdirs($dir);
	if (isset($album)) { echo "<h1 align="center">".$album."</h1>"; } else { echo "<h1 align="center">".$title."</h1>"; }
	echo "<div align="center">";
	if (isset($album)) { echo "<a href="".$_SERVER[PHP_SELF]."">$title</a>\n"; } else {  }
	if (count($dirs)>0) {
		foreach ($dirs as $value) {
		   echo "<a href="".$_SERVER[PHP_SELF]."?album=".urlencode($value)."">".$value."</a>\n";
		}
	}
	echo "</div>";
}

//returns the header for the page. Checks for a user defined header.
function getheader() {
	global $header, $title;
	if (isset($header) && $header!="NULL" && is_file ($header)) {
		require("$header");
	}
	else {
		echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">';
		echo '<html>';
		echo '<head>';
		echo "<title>$title</title>";
		echo '</head>';
		echo '<body>';
	}
}

//returns the footer for the page. Checks for a user defined footer.
function getfooter() {
	global $footer;
	if (isset($footer) && $footer!="NULL" && is_file ($footer)) {
		require("$footer");
	}
	else {
		echo "</body>\n</html>";
	}
}

$photodir= dirname($_SERVER['PATH_TRANSLATED']);
$photourl=dirname($_SERVER[PHP_SELF]);
$thumbsurl=dirname($_SERVER[PHP_SELF]);
$photobody=array();

if (isset($_GET[album])) {
	$album = urldecode($_GET[album]);
	$photodir=dirname(realpath($_SERVER['PATH_TRANSLATED']))."/".$album;
	$photourl=dirname($_SERVER[PHP_SELF])."/".$album;
	$thumbsurl=dirname($_SERVER[PHP_SELF])."/".$album;
}

//To make sure this works both on Linux and Win
$photodir = str_replace("", "/", $photodir);

//Get the jpegs from the folder.
$photos=getphotos($photodir);

//check to see if thumbnails are made if not it will make. Adds time to the processing of the script.
for ($i=0; $i<=count($photos)-1; $i++) {
	thumb_maker($photodir."/".$photos[$i], $twidth, $theight);
}
if (count($photos)<=0) {
	echo "No JPEG photos in the folder to display";
	exit;
}
if (isset($_GET[pn])) {
	$photonum = $_GET[pn];
	$photobody=thumb_gallery($photonum);
}
elseif (isset($_GET[p])) {
	$photo=$_GET[p];
	$photobody=photo_gallery($photo,$pwidth, $pheight);
}
else {
	$photobody=thumb_gallery(0);
}
//output it all to the browser.
display($photobody);

exit;

include("inc/design2.php");



?>
This script basically checks all the directories and assumes they are all folders with pictures that you want to display.. and if there are pictures in the directory it is in it will thumbnail them etc...

I'm not very experience with galleries .. ie. directory manipulation etc etc so I was hoping someone could help me change this script so it would only check things in gallery/ and whatever folders in there instead of checking all the folders in the current directory
TY
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

up

edit: your cool
Last edited by John Cartwright on Tue Mar 09, 2004 9:06 pm, edited 1 time in total.
davy2001
Forum Commoner
Posts: 37
Joined: Tue Feb 24, 2004 5:59 pm

Post by davy2001 »

Phenom wrote:up?
the sky?
Post Reply