<?php
/*
  THIS SETUP PROGRAM CREATES THUMBNAIL IMAGES FOR ALL PNG
  IMAGES WITHIN THE SAME FOLDER AS THIS SCRIPT.

  IT ONLY NEEDS TO BE CALLED ONCE TO INITIALLY CREATE
  THE THUMBNAIL IMAGES FOR ALL PNG IMAGES PUT INTO
  THE GALLERY HOME FOLDER.

  ALL THUMBNAIL IMAGES WILL BE CREATED IN THE SAME ASPECT
  RATIO (HORIZ/VERT PROPORTIONS) AS THE SOURCE IMAGES.
*/


/* ---------------------------------------------
   Set pixel width of the thumbnail images. the
   height will be scaled relative to this width.
*/
   $ThumbImageWidth = 256; // pixels


/* --------------------------------------------------
   Delete any existing thumbnail images.  All of the
   thumbnail file names begin with an underscore _
   character like:  _ImageFileName.png
*/
   array_map('unlink', glob("_*"));

// ------------------------------------
// DELETE any existing PNG images list.

   @unlink('PNG-list.txt');


/* ------------------------------------
   Execute loop to create a list of all
   PNG images within the folder ending
   with file extension '.png' only.

   THE FILE EXTENSION IS CASE-SENSITIVE
*/
   $PNGFilesList = '';

   ForEach (glob("*.png") as $FileName)
           {$PNGFilesList .= "$FileName\n";}

// ---------------------------------------------------
// Copy the created list of PNG image names to a file.

   $PNGFilesList = trim($PNGFilesList);

   File_Put_Contents('PNG-list.txt', $PNGFilesList);

/* ------------------------------------------------
   Put the PNG file names list into a string array
   and count them.
*/
   $PNGImages = preg_split("[\n]", $PNGFilesList);
           $n = count($PNGImages);

/* --------------------------------------------------
   Cycle through all of the PNG file names and create
   corresponding thumbnail images for each of them.
*/
   for ($i=0;   $i < $n;   $i++)
       {
        $ImageFileName = trim($PNGImages[$i]);

        if ($ImageFileName != '')
           {Create_Thumbnail_Image($ImageFileName, $ThumbImageWidth);}
       }
  $ImageCount = $n;

// DONE.
   print "OK ... Ready<br>\n<br>Total Image Count: $n<br>\n";





// -------------------------------------------------------------
// THIS FUNCTION CREATES AND SAVES A THUMBNAIL IMAGE IN THE SAME
// XY-PROPORTIONS AS THE ORIGINAL SOURCE IMAGE WITH A SPECIFIED
// PIXEL WIDTH.

   function Create_Thumbnail_Image ($SourceImageName, $ThumbImageWidth)
{
   global $ImageCount;

// DEFINE SOURCE IMAGE HANDLE FOR INTERNAL WORK.
   $SourceImageHandle = ImageCreateFromPNG($SourceImageName)
                        or die("ERROR opening source image.");

// DEFINE NAME OF THUMBNAIL IMAGE.  THIS WILL BE IDENTICAL TO
// THE SOURCE IMAGE PRECEDED BY AN UNDERSCORE CHARACTER.

   $ThumbImageName = "_" . $SourceImageName;

// DEFINE PATH TO THUMBNAIL IMAGES.
// EMPTY STRING = SAME FOLDER AS SOURCE IMAGE.
   $ThumbImagePath = '';

// GET SOURCE IMAGE WIDTH AND HEIGHT.
   $xW = ImageSX($SourceImageHandle);
   $yH = ImageSY($SourceImageHandle);

// COMPUTE IMAGE XY-PROPORTIONS RATIO.
   $XYRatio = $xW / $yH;

// COMPUTE THUMBNAIL IMAGE SIZE IN SAME
// XY-PROPORTIONS AS THE ORIGINAL IMAGE.
   $ThumbImageHeight = $ThumbImageWidth / $XYRatio;

// DEFINE THUMBNAIL IMAGE HANDLE.
   $ThumbImageHandle = ImageCreateTrueColor($ThumbImageWidth, $ThumbImageHeight)
                       or die("ERROR while creating thumbnail image.");

// CREATE THE THUMBNAIL IN THE COMPUTED PROPORTIONS
// WITH RESAMPLING TO MAINTAIN IMAGE CLARITY.
   ImageCopyResampled
  (
   $ThumbImageHandle, $SourceImageHandle,  0,0,0,0,

   $ThumbImageWidth, $ThumbImageHeight,    $xW, $yH
  )
   or die("ERROR in resizing source image.");

// SAVE THE CREATED THUMBNAIL IMAGE FILE.
   ImageJPEG($ThumbImageHandle, $ThumbImagePath . $ThumbImageName)
      or die("ERROR writing thumbnail image.");

} // END OF  Generate_Thumbnail_Image()



?>



