<?
/*

  APA: Automatic Php Album (version 0.2, 2005-04-28)


  Copyright (c) 2005 Glenn ROLLAND.
  See the file COPYING for details on the GNU General Public License

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2, or (at your option)
  any later version.

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/


$config=array();
$comment=array();
$image=array();

$mydirectory= '.';          //directory in which images are fetched
$config["image_regexp"]= "/\.(gif|png|jpe?g)$/";
$config["config_regexp"]= "/^\s*(.+?)\s*=\s*(.+?)\s*$/";
$config["apa_file"]=basename(__FILE__);
$config["apa_version"]="0.2";

function loadConfig(){
	global $config;
	$configName=str_replace(".php",".conf",$config["apa_file"]);

	if (! file_exists($configName)){
		// create the configuration file
		$fh=fopen($configName,"w");
		fwrite($fh,"\n");
		fwrite($fh,"# album page title\n");
		fwrite($fh,"album_title = Album generated by Automatic Php Album\n\n");
		fwrite($fh,"# number of images per line (obsolete)\n");
		fwrite($fh,"album_columns = 4\n\n");
		fwrite($fh,"# thumbnail image quality (0: worst to 100:best)\n");
		fwrite($fh,"thumb_quality = 75\n\n");
		fwrite($fh,"# thumbnail image maximum size (width or lenght)\n");
		fwrite($fh,"thumb_size = 160\n\n");
		fwrite($fh,"# directory created to store thumbnail images\n");
		fwrite($fh,"thumb_dir = thumb\n\n");
		fwrite($fh,"# prefix for generated images\n");
		fwrite($fh,"thumb_prefix = thumb_\n\n");
		fwrite($fh,"# set apa_ready to '1' to be able to use album (obsolete)\n");
		fwrite($fh,"ready = 0\n\n");
		fclose($fh);
		loadConfig();
	} else {
		// read the configuration file
		$content="";
		$fh=fopen($configName,"r");
		while(!feof($fh)){ $content.=fread($fh,8192); }
		fclose($fh);
		$confLines=explode("\n",$content);
		foreach ($confLines as $line ) {
			if (preg_match("/^\s*#/",$line)) { // skip
			} elseif (preg_match("/^\s*$/",$line)) { // skip
			} elseif (preg_match_all($config["config_regexp"],$line,$matches)){
				$config[$matches[1][0]]=$matches[2][0];
			}
		}
	}
}

function loadComment($mydirectory){
	global $comment;
	global $config;
	$commentName=str_replace(".php",".info",$config["apa_file"]);

	if (! file_exists($commentName)){
		// create the configuration file
		$fh=fopen($commentName,"w");
		fwrite($fh,"\n");
		$dirh=opendir($mydirectory);
		while ($currfile = readdir($dirh)){
			if (preg_match($config["image_regexp"],strtolower($currfile))){
				fwrite($fh,"# comment of file $currfile\n");
				fwrite($fh,"$currfile = Image $currfile\n\n");
			}
		}
		closedir($dirh);
		fclose($fh);
		loadComment($mydirectory);
	} else {
		// read the configuration file
		$content="";
		$fh=fopen($commentName,"r");
		while(!feof($fh)){ $content.=fread($fh,8192); }
		fclose($fh);
		$commentLines=explode("\n",$content);
		foreach ($commentLines as $line ) {
			$matches=array();
			if (preg_match("/^\s*#/",$line)) { // skip
			} elseif (preg_match("/^\s*$/",$line)) { // skip
			} elseif (preg_match_all($config["config_regexp"],$line,$matches)){
				$imagename=$matches[1][0];
				$imagecomment=$matches[2][0];
				if (preg_match($config["image_regexp"],strtolower($imagename))){
					$comment[$matches[1][0]]=$matches[2][0];
				}
			}
		}
	}
}

function displayThumb($imageNumber){
	global $config;
	global $comment;
	global $image;
	$imageName=$image[$imageNumber];
	$size = GetImageSize($imageName);
	$width=$size[0];
	$height=$size[1];
	$img="";
	if ($width > $height){
		$thumb_width = $config["thumb_size"];
		$thumb_height = ($height * $config["thumb_size"]) / $width;
	} else {
		$thumb_width = ($width * $config["thumb_size"]) / $height;
		$thumb_height = $config["thumb_size"];
	}
	$imageFormat = $size[2]; //1 = GIF, 2 = JPG, 3 = PNG, 5 = PSD, 6 = BMP

	$thumbName = "./" . $config["thumb_dir"] . "/" . $config["thumb_prefix"] . $imageName;
	if (! file_exists($thumbName))
	{
		if ($imageFormat == 1){ //GIF
			$img = imagecreatefromgif($imageName);
		} elseif ($imageFormat == 2) { //JPG
			$img = imagecreatefromjpeg($imageName);
		} elseif ($imageFormat == 3) { //PNG
			$img = imagecreatefrompng($imageName);
		} else {
			$img=imagecreatetruecolor($thumb_width,$thumb_height);
		}
		if (!$img){
			$thumbName = $imageName;
		} else {   
			$img_thumb=imagecreatetruecolor($thumb_width,$thumb_height);
			imagecopyresampled($img_thumb,$img,0,0,0,0,$thumb_width,$thumb_height,$size[0],$size[1]);
			Imagejpeg($img_thumb, $thumbName, $config["thumb_quality"]);
			ImageDestroy($img_thumb);
			ImageDestroy($img);
		}
	}

	$urlThumbName = "./" . rawurlencode($config["thumb_dir"]) . "/" . rawurlencode($config["thumb_prefix"] . $imageName);
	echo "<div class=\"thumbout\">";
	echo "<p class=\"thumb\">";
	echo  "<a href=\"".$config["apa_file"]
		."?vm=1"
		."&fname=".rawurlencode($imageName)
		."&fnb=$imageNumber\" >\n";
	echo  "<img src=\"".$urlThumbName."\" "
		."thumb_width=\"$thumb_width\" "
		."thumb_height=\"$thumb_height\" ";
	if ($width > $height){
		echo " class=\"horizimg\" ";
	}
	echo " alt=\"Enlarge $imageName\" />\n";
	echo  "</a>\n";
	echo "</p>\n\n";
	echo "<span class=\"comment\">".$comment[$imageName]."</span>\n";
	echo "</div>\n";

}

function loadImages($mydirectory){
	global $config;
	global $image;
	global $comment;
	$nbfiles=0;
	$handle=opendir($mydirectory);

	//create a directory for thumbnail images
	if (! is_dir($config["thumb_dir"]))
	{
		mkdir($config["thumb_dir"], 0777);
	}

	while ($currfile = readdir($handle))  
	{
		// We get the extension of the current file and keep only image files
		$extension= strtolower(substr( strrchr( $currfile,  "." ), 1 ));  
		if (preg_match("/(gif|png|jpe?g)/",$extension))
		{
			// $currfile = trim($currfile);
			$image[$nbfiles]=$currfile;
			$nbfiles++;
		}
	}
	closedir($handle);
}


function showAlbum(){
	global $comment;
	global $config;
	global $image;

	$viewmode=$_GET["vm"];
	$filename=urldecode($_GET["fname"]);
	$filenumber=$_GET["fnb"];

	if ($viewmode == "") {
		$couter=0;
		for ($i=0;$i<count($image);$i++){
			displayThumb($i);
			$counter++;
			//if ($counter == $config["album_columns"]){
			//	$counter = 0;
			//  echo  "<hr class=\"spacer\">";
			//}
		}
	} elseif ($viewmode == 1) {

		echo "<p id=\"btnbar\">";
		if ($filenumber > 0) {
			echo "<span class=\"btnprev\">[";
			$prevnumber = $filenumber - 1;
			echo "<a href=\"".rawurlencode($config["apa_file"])
				."?vm=1"
				."&fname=".rawurlencode($image[$prevnumber])
				."&fnb=$prevnumber\">";
			echo  " << Previous </a>";
		} else {
			echo "<span class=\"btnprev btngray\">[";
			echo  " << Previous ";
		}
		echo "]</span>\n";

		echo "<span class=\"btnmain\">";
		echo  "[<a href=\"".$config["apa_file"]."\"> Main Page </a>]\n";
		echo "</span>\n";

		if ($filenumber < count($image)-1) {
			echo "<span class=\"btnext\">[";
			$nextnumber = $filenumber + 1;
			echo  "<a href=\"".rawurlencode($config["apa_file"])
				."?vm=1"
				."&fname=".rawurlencode($image[$nextnumber])
				."&fnb=".$nextnumber
				."\">";
			echo  " Next >> </a>";
		} else {
			echo "<span class=\"btnext btngray\">[";
			echo  " Next >> \n";
		}
		echo "]</span>";
		echo "</p>";

		echo  "<p class=\"bigcomment\">\n";
		echo $comment[$filename];
		echo "</p>\n";

		echo  "<p>\n";
		echo  "<img src=\"".rawurlencode($filename)."\">\n";
		echo "<br><b> $filename </b><br>";
		echo  "<p><br>\n\n";

		echo "\n\n";
	}

}

loadConfig();
loadComment(".");
loadImages(".");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title><?php print $config["album_title"]; ?></title>
<style>
body {
	font-family: sans-serif;
}
h1{
	font-size: 110%;
	text-align:center;
}

#btnbar a {
	text-decoration: none;
color: blue;
}

#btnbar a:hover {
	text-decoration: none;
color: red;
}

.btnprev {
width: 150px;
color: blue;
}


.btngray {
color: #ccc;
}

.btnmain {
width: 150px;
	   margin-left: 10px;
	   margin-right: 10px;
}

.btnnext {
width: 150px;
}

.album {
display: block;
left: 0px;
right: 0px;
	   margin-left: 10%;
	   margin-right: 10%;
	   text-align: center;
	   /* width: <?php print ($config["thumb_size"]+50)*$config["album_columns"]; ?>px; */
}

.thumb {
display: block;
float: top;
border: 2px solid black;
background: #eee;
width: <?php print ($config["thumb_size"]+20); ?>px;
height: <?php print ($config["thumb_size"]+20); ?>px;
		text-align: center;
		vertical-align: middle;
		border-width: 1px 3px 3px 1px;
		-moz-border-radius: 25px;
}

.thumbout {
display: block;
float: left;
width: <?php print ($config["thumb_size"]+20); ?>px;
height: <?php print ($config["thumb_size"]+90); ?>px;
margin: 10px;
}

.thumb img {
display:block;
		margin-top: 7px;
		margin-left: auto;
		margin-right: auto;
padding:0px;
border: 1px solid #000;
		vertical-align: middle;
}

.thumb img.horizimg {
	margin-top: 25px;
}

.footer {
	font-size: 70%;
}

.comment {
display:block;
padding: 0px;
margin: 0px;
float: top; 
	   text-align: center;
	   vertical-align:bottom;
	   font-size: 80%;
}

.bigcomment {
padding: 10px;
		 font-size: 110%;
		 font-weight: bold;
}
.spacer {
clear: both;
visibility: hidden;
}
</style>


</head>
<body>
<h1><?php print $config["album_title"]; ?></h1>
<div class="album">
<?php 
showAlbum();
?>
<hr class="spacer" />
<p class="footer">Powered by Automatic PHP Album v<?php print $config["apa_version"]; ?>
&copy; 2005, Glenn ROLLAND
</p>
</div>

</body>
</html>

