| 
<?php
 /**
 * Script used to sort the images from a directory by EXIF data.
 *
 * @category Sorting, File Manipulation
 * @package Sorting
 * @author Marius Zadara <[email protected]>
 * @copyright (C) Marius Zadara <[email protected]>
 * @version 2.0
 * @since File available since version 2.0
 */
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // PREREQUISITES //////////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 // LOAD THE EXCEPTIONS
 include_once 'exceptions/_addExceptions.php';
 
 // LOAD THE UTILITY CLASSES
 include_once 'classes/_addClasses.php';
 
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // RUN ////////////////////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 
 // unlimited time executinon granted to script?
 if (Constants::$UNLIMITED_TIME !== false)
 {
 // assume unlimited time execution
 $seconds = 0;
 
 // numeric time execution seconds
 if (Constants::$UNLIMITED_TIME !== true)
 $seconds = Constants::$UNLIMITED_TIME;
 
 // set the time limit
 set_time_limit($seconds);
 
 // clear the memory being used
 unset($seconds);
 }
 
 
 // SET-UP /////////////////////////////////////////////////////////////////////////////////////////
 // include the script responsible for the source and destination directories setup
 require_once("setup.php");
 
 // LOADING ////////////////////////////////////////////////////////////////////////////////////////
 // load the content of the source directory into the local array
 // use the $srcDirectory from the "setup.php" file
 try
 {
 // get the source directory content
 $srcDirContent = $srcDirectory->getContent();
 
 // the list with the source directory files
 $srcDirFiles = array();
 
 // the list with the files from the source direcoty
 // for which the exif data could not be read
 $badSrcDirFiles = array();
 
 // maximum datetime
 $max = 0;
 
 
 // parse the list with files from the source directory
 foreach ($srcDirContent as $index => $filename)
 {
 $file = new File($filename);
 $dateTime = $file->getDateTime();
 $mimeType = $file->getMimeType();
 
 // if no information found,
 // add the filename to the bad list
 // these files will also be added in the list
 if (($dateTime === false) || ($mimeType === false))
 $badSrcDirFiles[] = $filename;
 
 // get the maximum timestamp
 $max = ($dateTime > $max) ? $dateTime : $max;
 
 // add to the list
 $srcDirFiles[$filename] = $dateTime;
 
 // clear the memory used
 unset($file, $dateTime, $mimeType);
 }
 
 // sort the source directory files by the datetime
 // using the configuration flag
 if (Constants::$OUTPUT_ORDER == "ASC")
 asort($srcDirFiles, SORT_NUMERIC);
 else
 arsort($srcDirFiles, SORT_NUMERIC);
 
 
 // after the files have been sorted,
 // add the "bad files" to the list as the last ones
 if (sizeof($badSrcDirFiles) > 0)
 {
 foreach ($badSrcDirFiles as $index => $filename)
 $srcDirFiles[$filename] = ++$max;
 }
 
 // init the counter using the config value
 $counter = Constants::$START_COUNTER;
 
 // after all the files have been added to the list and sorted,
 // is time to update the destination directory
 foreach ($srcDirFiles as $filename  => $dateTime)
 {
 // reinstanciate the class
 $file = new File($filename);
 
 // get the filename pathinfo
 $pathinfo = pathinfo($filename);
 
 // save the current counter into a temporary variable
 $finalCounter = $counter;
 
 // format the counter
 $finalCounter = str_pad($finalCounter, Constants::$COUNTER_LENGTH, Constants::$PAD_CHAR, Constants::$PAD_TYPE);
 
 // make the destination filename format
 $destinationFormat = str_replace(
 array("%counter%", "%basename%", "%extension%", "%filename%"),
 array($finalCounter, $pathinfo['basename'], $pathinfo['extension'], $pathinfo['filename']),
 Constants::$DESTINATION_FORMAT
 );
 
 // make the destionation full path
 $destPath = sprintf("%s%s%s", Constants::$DESTINATION_DIRECTORY, DIRECTORY_SEPARATOR, $destinationFormat);
 
 // default function to apply
 $function = "copyTo";
 
 // move or copy function ?
 if (Constants::$ACTION != "COPY")
 $function = "moveTo";
 
 // call the function on the file object, setting the destination path
 if (!call_user_func(array($file, $function), $destPath))
 throw new FileException(sprintf("Error occured while output the file '%s' to '%s'.", $filename, $destPath));
 
 // verify the breakpoint - if reached, terminate the loop
 if ((Constants::$OUTPUT_BREAKPOINT !== false) && ($counter == Constants::$OUTPUT_BREAKPOINT))
 break;
 
 // increment the counter
 $counter ++;
 }
 
 // clear the memory used
 unset($counter, $srcDirFiles, $filename, $dateTime, $file, $destPath, $finalCounter);
 
 // display a message
 echo sprintf("Completed to ouput files to directory '%s'.", Constants::$DESTINATION_DIRECTORY);
 }
 catch (DirectoryException $dirEx)
 {
 echo $dirEx;
 }
 catch (FileException $fileEx)
 {
 echo $fileEx;
 }
 
 
 ?>
 |