<?php
/******************************************************************************
*
* SIMPLE IMAGE FLIPPER v.3
*
* Freeware, no restrictions
* Updated 2009-02-01 Created 2008-07-16
* by Björn Törnroth, bjorn.tornroth@nettitonttu.fi
* Get latest version at http://nettitonttu.fi/pub/ImageFlipper.zip
*
* INSTRUCTIONS
*
* Create a directory on your web-server.
* Put som images and this file in it.
*
* REQUIREMENTS
*
* PHP support and appropriate read directory rights.
*
* OPTIONAL
*
* Create header.htm to be displayed at the top (XHTML 1.0 Strict).
*
* Put a background image in folder "background". It will be tiled.
*
* Put large versions of the images (with exactly the same filename)
* in folder "large". The current image will then be clickable to see the large version.
*
* Create commentfiles to the images in folder "comments".
* If such a file exists, the comment will be displayed above the current image.
* One file per comment for each image. The filename must be exactly the same as the image,
* except for the extension, which can be .php .htm or .txt XHTML 1.0 Strict code is ok.
*
* Edit colors immediately below.
*
*******************************************************************************/
define ("BACKGROUND_COLOR", "rgb(48,48,48)");
define ("TEXT_COLOR", "rgb(250,250,250)");
define ("LINK_COLOR", "rgb(255,128,0)");
define ("FONT_NAME", "Verdana");
define ("FONT_SIZE", "9pt");
function fnBackgroundImage ()
{
if (! file_exists ("background")) return "";
if (! is_dir ("background")) return "";
if (! $dp = @opendir ("background")) return "";
while ($file = readdir ($dp)) {
if (is_dir ($file)) continue;
if (@GetImageSize ("background/$file") === FALSE) continue;
else {
@closedir ($dp);
return "background: url('background/$file');\n";
}
}
@closedir ($dp);
return "";
}
function fnImageArray ()
{
// GENERATE ARRAY OF IMAGES IN CURRENT DIR.
// Returns an array of arrays.
if (isset ($_GET["debug"])) $debug = TRUE;
else $debug = FALSE;
//$debug = TRUE;
$aaImages = array ();
if (!$dp = @opendir(".")) die ("Darn! Could not open directory for reading.");
while ($file = readdir ($dp)) {
if ($debug) echo $file;
if (is_dir ($file)) { if ($debug) { echo " (dir)<br />"; } continue; }
if (!$aImg = GetImageSize ($file)) { if ($debug) { echo " (not image)<br />"; } continue; }
list ($x, $y, $type) = $aImg;
$aaImages[] = array ($file, $x, $y);
if ($debug) echo " (". $aImg["mime"] .")<br />";
}
closedir ($dp);
sort ($aaImages);
return $aaImages;
}
function fnCreateCommentFiles (&$aaImages)
{
echo "<p><strong>Creating comment files.</strong><br />";
if (is_dir ("comments")) {
echo "Directory \"comments\" exists.<br />";
} else {
echo "<p>Creating directory \"comments\".<br />";
if (!mkdir ("comments")) die ("Could not create directory \"comments\". Please create it manually.</p>");
}
foreach ($aaImages as $aImg) {
$strCommentFile = "comments/". substr ($aImg[0], 0, strrpos ($aImg[0], ".")) .".txt";
if (file_exists ($strCommentFile)) {
echo "File \"$strCommentFile\" exists.<br />";
} else {
if (! $fp = fopen ($strCommentFile, "w")) die ("Could not create file.</p>");
fwrite ($fp, "");
fclose ($fp);
echo "File \"$strCommentFile\" created.<br />";
}
}
echo "</p>";
return;
}
function fnListOfImages (&$aaImages, $intImg)
{
// PRINT OUT LINK LIST OF IMAGES.
echo "<div id='images'>";
foreach ($aaImages as $id => $aImg) {
$id++;
if ($intImg + 1 <> $id) echo " <a href='?img=$id' name='i$id' id='i$id' title='". $aImg[0] ."'>$id</a> ";
else echo "<span> $id </span> ";
}
echo "</div>";
return;
}
function fnFlipButtons ($img, $last)
{
echo "<div id='flipButtons'>";
if ($img > 0) echo " <a href='?img=". ($img) ."'> « </a> ";
else echo " <span style='color:rgb(128,128,128)'> « </span> ";
if ($img < $last - 1) echo " <a href='?img=". ($img + 2) ."'> » </a> ";
else echo " <span style='color:rgb(128,128,128)'> » </span> ";
echo "</div>";
return;
}
function fnComment ($img, $width)
{
$strCommentFile = "comments/". substr ($img, 0, strrpos ($img, "."));
switch (TRUE) {
case file_exists ("$strCommentFile.php"): $strCommentFile = "$strCommentFile.php"; break;
case file_exists ("$strCommentFile.htm"): $strCommentFile = "$strCommentFile.htm"; break;
case file_exists ("$strCommentFile.txt"): $strCommentFile = "$strCommentFile.txt"; break;
default: echo "<div>"; return;
}
if (! filesize ($strCommentFile)) {
echo "<div>";
return;
}
if ($width < 550) {
echo "<div style='width:748px; margin:auto;'>
<div style='width:". (710 - $width) ."px; float:left; margin-top:10px; text-align:left;'>";
} else {
echo "<div><div style='width:". ($width + 14) ."px; margin:auto; margin-top:3px; text-align:justify;'>";
}
readfile ($strCommentFile);
echo "</div>";
return;
}
function fnViewImage (&$aaImages, $intImg)
{
if (! isset ($aaImages[$intImg])) die ("<p>Sorry, no such image. Index $intImg is out of bounds.</p>");
list ($img, $x, $y) = $aaImages[$intImg];
if (! file_exists ($img)) die ("<p>Sorry, image \"$img]\" not found.</p>");
if (file_exists ("comments")) {
if (is_dir ("comments")) fnComment ($img, $x);
}
if (file_exists ("large/$img")) {
echo "<div><a href='large/$img' title='Click image to see large image'
><img id='img' src='$img' width='$x' height='$y' alt='$img' /></a></div></div>";
} else {
echo "<div><img id='img' src='$img' width='$x' height='$y' alt='$img' title='$img' /></div></div>";
}
return;
}
ob_start (); // Enable output buffering.
if ($_SERVER["SERVER_NAME"] == "localhost") error_reporting (E_ALL); // SHOW ALL ERRORS.
else error_reporting (0); // SHOW NO ERRORS.
echo "<?xml version='1.0' encoding='iso-8859-1'". chr (63) .">
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>";
// Display source, if requested.
if (@strtoupper ($_GET["display"]) == "SOURCE") {
die ("<html><head><title>Source code for Simple Image Flipper</title></head><body><pre>".
htmlentities (file_get_contents (substr ($_SERVER["PHP_SELF"], strrpos ($_SERVER["PHP_SELF"], "/") + 1))) .
"</pre></body></html>");
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1' />
<title>Simple Image Flipper by Björn Törnroth</title>
<style type="text/css">
body {
margin: 1em;
background-color: <?php echo BACKGROUND_COLOR ?>;
<?php echo fnBackgroundImage (); ?>
color: <?php echo TEXT_COLOR ?>;
font-family: <?php echo FONT_NAME ?>, Verdana, Arial, Helvetica, sans-serif;
font-size: <?php echo FONT_SIZE ?>;
text-align: center;
}
h1, h2 ,h3 {
font-weight: bold;
margin: 0.8em 0px 0.5em 0px;
}
h1 {
font-size: 155%;
margin-top: 0px;
letter-spacing: 7px;
}
h2 {
font-size: 133%;
letter-spacing: 6px;
}
h3 {
font-size: 111%;
letter-spacing: 3px;
}
#images {
font-family: Verdana, Arial, Helvetica, sans-serif;
margin: 1em;
font-weight: bold;
}
#images span {
color: <?php echo BACKGROUND_COLOR ?>;
background-color: <?php echo LINK_COLOR ?>;
}
#flipButtons {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 14pt;
font-weight: bold;
margin-top: -10px;
}
a {
color: <?php echo LINK_COLOR ?>;
cursor: pointer;
text-decoration: none;
}
a:hover {
color: <?php echo BACKGROUND_COLOR ?>;
background-color: <?php echo LINK_COLOR ?>;
}
#img {
font-size: 8pt;
margin: 10px;
background-color: rgb(0,0,0);
padding: 6px;
border: 1px solid rgb(160,160,160);
}
#footer {
font-family: Verdana, Arial, Helvetica, sans-serif;
margin-top: 50px;
border-top: 1px solid rgb(128,128,128);
font-size: 8pt;
text-align: right;
line-height: 2.5em;
}
</style>
</head>
<body>
<?php
// SELECTED IMAGE ID TO DISPLAY.
if (isset ($_GET["img"])) $intImg = (int) $_GET["img"] - 1;
else $intImg = 0;
$aaImages = fnImageArray ();
if (isset ($_GET["createcommentfiles"])) fnCreateCommentFiles ($aaImages);
if (file_exists("header.htm")) readfile ("header.htm");
fnListOfImages ($aaImages, $intImg);
fnFlipButtons ($intImg, count ($aaImages));
fnViewImage ($aaImages, $intImg);
?>
<p id='footer'>
SimpleImageFlipper is utterly freeware and available at
<a href='http://nettitonttu.fi/pub/'>www.nettitonttu.fi/pub</a>
[<a href='<?php echo $_SERVER["PHP_SELF"] ?>?display=source' title='View source'>View source code</a>]
<br />
<a href='http://validator.w3.org/check?uri=referer'
><img style='border:0;width:88px;height:31px' src='http://www.w3.org/Icons/valid-xhtml10' alt='XHTML' title='Valid XHTML 1.0' /></a>
<a href='http://jigsaw.w3.org/css-validator/check/referer'
><img style='border:0;width:88px;height:31px' src='http://jigsaw.w3.org/css-validator/images/vcss' alt='CSS' title='Valid CSS' /></a>
<a href='http://www.nettitonttu.fi/'
><img style='border:0;width:88px;height:31px' src='http://www.nettitonttu.fi/img/webgnome.gif' alt='WebGnome' title='Proud Craftsmanship by WebGnome' /></a>
</p>
</body>
</html>