<?PHP

/* ###########################################################################
   Simple PNG Picture Gallery Program in PHP

   PROG VERSION : v1.1
   PHP VERSION  : v8.2.12
   AUTHOR       : Jay Tanner - 2025
   LICENSE      : Public Domain

   THIS PROGRAM REQUIRES THE GD GRAPHICS LIBRARY WHICH IS MOST LIKELY ALREADY
   AVAILABLE ON THE HOSTING SITE.

   This program automatically creates a web page to display thumbnail images
   of all of the PNG images within this containing folder and links them to
   their corresponding full-sized images within.

   A gallery consists of two (minimum) or more images.

   ###########################################################################
*/

   GLOBAL $PNGImages;

// -----------------------------------
// GET CURRENT LOCAL YEAR FROM SYSTEM.

   $cYear = date('Y');

/* ----------------------------------------------
   Define a title text for the gallery of images
   and set the number of thumbnail images to be
   displayed per row.  Title can include HTML.
*/
   $GalleryTitle = "Title Of Your PNG Gallery Goes Here";
   $ThumbsPerRow = 3;

// ----------------------------------------------
// Define optional note to append after the image
// count at the top of the gallery page.

  $OptionalNote =
  "<span style='font-size:9pt;'>Optional note to append below the image count.</span>";


/* -----------------------------------
   Count PNG images in the PNG listing
   created by the 'init.php' program.
*/
   $PNGList = 'PNG-list.txt';
   $ImageCount = Substr_Count(trim(File_Get_Contents($PNGList)), "\n") + 1;



/* -----------------------------------------------
   Call the function to display all the thumbnails
   in the gallery home folder using the specified
   number of thumbs per row.  The thumbnail images
   are linked to their related full-sized images.
*/
   Show_Picture_Gallery ($GalleryTitle, $ThumbsPerRow);




/* --------------------------------------------------------
   At this point, there should be a list of all PNG images
   in a text file called 'PNG-list.txt' within the folder.
   This list will then be used to generate an HTML page of
   linked thumbnails.
*/
   Function Show_Picture_Gallery ($PageTitle, $ImagesPerRow)
{
   GLOBAL $cYear, $ThumbsPerRow, $ImageCount, $OptionalNote, $PNGList;

// --------------------------------------
// DEFINE FILE EXTENSIONS FOR THE IMAGES.

   $imageExt = '.png';
   $thumbExt = '.png';

   if ($ImageCount == 1 and trim($PNGImages) == '') {$ImageCount = 'No ';}

// --------------------------
// Generate client HTML page.

Print <<< _HTML

<!DOCTYPE HTML>
<html lang='en'>
<head>
<meta http-equiv=content-type content="text/html; charset=UTF-8">

<meta http-equiv='pragma'  content='no-cache'>
<meta http-equiv='expires' content='-1'>

<style type="text/css">
table {font-family:Verdana;  font-weight:bold;}

td
{
padding:4px;
font-family:Helvetica;
font-size:10px;
font-weight:bold;
text-align:center;
border:thin solid #282828;
}

   A:link
{
   color:            GreenYellow;
   background-color: black;
   padding:          2px;
   line-height:      150%;
   font-family:      Verdana, Arial, Tahoma, Helvetica;
   font-size:        10px;
   font-weight:      bold;
   text-decoration:  none;
}


   A:visited
{
   color:            #AAAAAA;
   background-color: black;
   padding:          2px;
   line-height:      150%;
   font-family:      Verdana, Arial, Tahoma, Helvetica;
   font-size:        10px;
   font-weight:      bold;
   text-decoration:  none;
}


   A:hover
{
   color:            orange;
   background-color: black;
   padding:          2px;
   line-height:      150%;
   font-family:      Verdana, Arial, Tahoma, Helvetica;
   font-size:        10px;
   font-weight:      bold;
   text-decoration:  none;
}


   A:active
{
   color:            red;
   background-color: black;
   border:           1px solid black;
   padding:          2px;
   line-height:      150%;
   font-family:      Verdana, Arial, Tahoma, Helvetica;
   font-size:        10px;
   font-weight:      bold;
}

</style>

<title>$PageTitle</title>
</head>
<body bgcolor='black'>


<div align="left">

<table bgcolor='black'  cellspacing="4" border="0">

<tr><td colspan="$ThumbsPerRow"><span style='color:GreenYellow; font-size:150%; text-align:center;'>$PageTitle</span><br><span style='color:silver; font-size:15px; font-weight:normal;'><br>$ImageCount Images<br>$OptionalNote</style></td></tr>

_HTML;




// *********************************************
// THE FOLLOWING CODE GENERATES THE HTML CODE TO
// DISPLAY THE LINKED THUMBNAILS BASED ON THE
// GENERATED FILES LISTING.  THE IMAGES WILL
// BE DISPLAYED IN A TABLE.



// ----------------------------------------
// GET IMAGE FILE NAMES LIST INTO A WORKING
// STRING ARRAY. THIS IMAGE LIST IS CREATED
// BY THE Setup.php PROGRAM.
   $ImageFileName = file("PNG-list.txt");

// --------------------------------------------------
// SORT THE FILE NAMES ARRAY INTO ALPHABETICAL ORDER.
   sort($ImageFileName, SORT_STRING);

// -----------------------------------
// COUNT THE NUMBER OF IMAGE FILENAMES
// IN THE WORKING ARRAY.
   $ImageCount = SizeOf($ImageFileName);



// ------------------------------------------
// DETERMINE THE NUMBER OF TABLE ROWS NEEDED.
   $rows = floor($ImageCount / $ImagesPerRow) + 1;

// ------------------------
// Initialize file counter.

   $FileCounter = 0;

// -------------------------------------------
// Display all except the final row of images.
// All these rows will have the maximum number
// of images.  The final row may possibly have
// less than the maximum number of images and
// will be handled separately later.

   for ($row=0;   $row < $rows - 1;   $row++)
{

// ------------------------
// Start current table row.

   print "<tr>\n";

// ---------------------------------------
// Populate the table rows data cells with
// thumbnail images.

   for ($i=0;   $i < $ImagesPerRow;   $i++)

     {
       $w = Trim($ImageFileName[$FileCounter]);

       If ($w != "")
      {
       $w  = "_" . $w;
       $w  = SubStr($w, 1, StrLen($w));
       $ii = StrPos($w, ".");
       $BaseFileName = SubStr($w, 0, $ii);

       $AltText = str_replace('_', ' ', $BaseFileName);

       print <<< _HTML

<td>
<a href="$BaseFileName$imageExt" target="_blank">
<img src="_$BaseFileName$thumbExt" alt="$BaseFileName$imageExt">

<!--
or call existing page.
<a href="$BaseFileName.html" target="_blank">
-->

<br>&nbsp;$AltText&nbsp;<br>
</a>
</td>

_HTML;

       $FileCounter++;
      }

     }

// ---------------------------------
//  Close out the current table row.

    print "</tr>\n\n";

}



/*

Determine how many residual thumbnail images are
in the final row.  The final row may or may not
have the maximum number of images per row.  If
none, then finished.  Otherwise display the
final row of images.  If the total number of
files is a perfect multiple of the maximum
number of images per row, then there will be
no residual table row to follow since there
will be no files left and we are done.

*/



   $ResidualImages = ($ImageCount % $ImagesPerRow);

// -------------------------------------------
// If there are any residual images left over,
// then display them.

   if ($ResidualImages > 0)
{

// ----------------------
// Start final table row.

   print "<tr>\n\n";

// -------------------------------------------
// Loop through the remaining residual images.

   For ($i=0;   $i < $ResidualImages;   $i++)

       {
        $w = Trim($ImageFileName[$FileCounter]);

             If ($w != "")
            {
             $w  = "_" . $w;
             $w  = SubStr($w, 1, StrLen($w));
             $ii = StrPos($w, ".");
       $BaseFileName = SubStr($w, 0, $ii);

       $AltText = str_replace('_', ' ', $BaseFileName);

       print <<< _HTML
<td>
<a href="$BaseFileName$imageExt" target="_blank">
<img src="_$BaseFileName$thumbExt" alt="$BaseFileName$imageExt">
<br>&nbsp;$AltText&nbsp;<br>
</a>
</td>

_HTML;

             $FileCounter++;
            }
       }

// -----------------------
//  Close final table row.

    print "</tr><tr><td colspan='$ThumbsPerRow' style='color:silver;'><br></td></tr>\n\n";
}







// -----------------------------------------
// Finish up by generating end of HTML page.

   print <<< END_OF_HTML_PAGE

</table>

<br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br>

</div>

</body>

</HTML>


END_OF_HTML_PAGE;

}

?>
