<?
/*********************************************************************************
Damn Simple Gallery (dsGallery) v0.1
Coded By: Prestige

Magus (XIG developer) asked me to create a simple gallery that would take a
directory loaded with images and automatically create thumbnails.  This is it.
As you add more images, the thumbnails are automagically created.

Notes:	If you want overwrite an image or delete an image, you must also delete
		the thumbnail.

		You must have Imagemagick installed to use this.  Most hosting providers
		already have it installed, but if not, get it here:
		http://www.imagemagick.org/
*********************************************************************************/

/*********************************************************************************
Edit these values to match your server settings
$convert refers to the path to the convert binary from ImageMagick
*********************************************************************************/

$imagedir = "./imagedir";
$imageurl = "/dev/xig/images/imagedir";

$thumbnaildir = "./tn";
$thumbnailurl = "/dev/xig/images/tn";

$convert = "/usr/bin/convert";







/*********************************************************************************
Don't edit anything below here unless you know what you are doing
*********************************************************************************/
if ($_REQUEST[tn]){
	$image_imagedir = array();
	# Get image dir files
	if ($handle = opendir("$imagedir")) {
	   while (false !== ($file = readdir($handle))) {
	       if ($file != "." && $file != ".." && preg_match("#.jpg#i",$file)) {
	           $image_imagedir[] = $file;
	       }
	   }
	   closedir($handle);
	}
	
	$image_thumbnaildir = array();
	# Get thumnail dir files
	if ($handle = opendir("$thumbnaildir")) {
	   while (false !== ($file = readdir($handle))) {
	       if ($file != "." && $file != ".." && preg_match("#.jpg#i",$file)) {
	           $image_thumbnaildir[] = $file;
	       }
	   }
	   closedir($handle);
	}
	
	
	# create thumbnails if a given thumbnail does not already exist
	for ($x=0; $x < sizeof($image_imagedir); $x++){
		$image = $image_imagedir[$x];
		if (!in_array("$image",$image_thumbnaildir)){
			# Create Thumbnail
			$command = "$convert -geometry '160x120' '$imagedir/$image' '$thumbnaildir/$image'";
			exec($command, $returnarray, $returnvalue);
			#echo $command;
			}
		}
	
	# Display thumbnails
	$text = "<center>\n";
	$text .= "<table width=\"160\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n";
	for ($x=0; $x < sizeof($image_imagedir); $x++){
		$image = $image_imagedir[$x];
		$text .= "<tr>\n";
		$text .= "	<td align=\"center\">";
		$text .= "<a href=\"$_SERVER[PHP_SELF]?image=$image\" target=\"showimage\"><img src=\"$thumbnailurl/$image\" border=\"0\"></a>\n";
		$text .= "</td>\n";
		$text .= "</tr>\n";
		$text .= "<tr><td>&nbsp;</td></tr>\n";
		}
	$text .= "</table>\n";
	$text .= "</center>\n";
	#Done
	}



if ($_REQUEST[image]){
	$_REQUEST[image] = addslashes($_REQUEST[image]);
	$text = "<table class=\"center\"><tr><td class=\"center\">\n";
	$text .= "<DIV align=\"center\"><img src=\"$imageurl/$_REQUEST[image]\"></DIV>\n";
	$text .= "</td></tr></table>\n";
	}


if (!$_REQUEST[image] && !$_REQUEST[tn]){
	$text .= "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n";
	$text .= "<tr>\n";
	$text .= "	<td width=\"160\"><IFRAME id=\"thumbs\" SRC=\"$_SERVER[PHP_SELF]?tn=1\" NAME=\"thumbs\" width=\"180\" onLoad=\"calcHeight('thumbs');\" height=\"450\" FRAMEBORDER=\"0\" MARGINEWIDTH=\"0\" MARGINE HEIGHT=\"0\" SCROLLING=\"AUTO\"></IFRAME></td>\n";
	$text .= "	<td width=\"100%\"><IFRAME id=\"showimage\" NAME=\"showimage\" width=\"100%\" height=\"450\" FRAMEBORDER=\"0\" onLoad=\"calcHeight('showimage');\" MARGINEWIDTH=\"0\" MARGINE HEIGHT=\"0\" SCROLLING=\"NO\"></IFRAME></td>\n";
	$text .= "</tr>\n";
	$text .= "</table>\n";
	}



?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<title>XIG Galelria</title>
<style>
body {	font-family: Verdana, Arial, Geneva, sans-serif; 
		font-size: 10pt; 
		background: #000000;
 		color: #FFFFFF;
		margin: 0;
 		scrollbar-face-color: #000000;
 		scrollbar-shadow-color: white;
 		scrollbar-highlight-color: white;
 		scrollbar-3dlight-color: #000000;
 		scrollbar-darkshadow-color: #000000; scrollbar-track-color: #000000;
 		scrollbar-arrow-color: white}

table.center {	width: 100%; height: 100%; }
td.center {	vertical-align: middle; align: center;}
</style>

<? if (!$_REQUEST[image] && !$_REQUEST[tn]){ ?>
<script language="JavaScript">
window.onresize= winResize;
function winResize() {
	calcHeight('thumbs');
	calcHeight('showimage');
	}
</script>

<script language="JavaScript">
function calcHeight(F){
		if (navigator.appName=="Netscape") {
			winW = window.innerWidth;
			winH = window.innerHeight;
			}
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			winW = document.body.offsetWidth;
			winH = document.body.offsetHeight;
			}
		document.getElementById(F).height=winH;
		}
</script>
<? } ?>

</head>
<BODY BGCOLOR="#000000" TEXT="#FFFFFF" LINK="#333399" VLINK="#333399" ALINK="#333399" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<? echo "$text"; ?>
</body>
</html>

