// See http://www.kevincooney.com/projects/audrey/resize.txt
include("resize.php");
// Some Constants
$BASE_PHOTO_DIR = "C:/Documents and Settings/Kevin.MEDIA/My Documents/My Pictures"; // Top level directory for photos
$DOCROOT = "C:/inetpub/wwwroot"; // Webserver docroot
$PROJECT_DIR = "/photos"; // Relative directory off docroot
$PHOTO_FILE = "photos.txt"; // Name of directory cache file
$MAX_FILE_AGE = 7 * 24 * 60 * 60; // Max age for cache before rebuilding it
$PHOTO_TO_LOAD = "picframe.jpg"; // We'll put the resized image here
$PHOTO_FILE_FULL_PATH = $DOCROOT . $PROJECT_DIR . "/" . $PHOTO_FILE;
// Recursive function to read all photos under a directory
function getPhotosFromDir($dir) {
global $BASE_PHOTO_DIR;
$photo_str = "";
if ($handle = opendir($BASE_PHOTO_DIR . "/" . $dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($BASE_PHOTO_DIR . "/" . $dir . "/" . $file)) {
$photo_str .= getPhotosFromDir($dir . "/" . $file);
} elseif (preg_match("/\.(gif|jpg)$/i", $file) == 1) {
$photo_str .= $dir . "/" . $file . "\n";
}
}
}
closedir($handle);
}
return $photo_str;
}
// Rebuild cache file
function rebuild_photo_file() {
global $BASE_PHOTO_DIR;
global $PHOTO_FILE_FULL_PATH;
$photo_str = "";
if (file_exists($BASE_PHOTO_DIR)) {
$photo_str = getPhotosFromDir("");
} else {
print "Photo directory not found: $BASE_PHOTO_DIR
\n";
exit;
}
rtrim($photo_str);
print $photo_str;
file_put_contents($PHOTO_FILE_FULL_PATH , $photo_str);
}
// Check if cache is still "fresh"
if (!file_exists($PHOTO_FILE_FULL_PATH) || time() - filemtime($PHOTO_FILE_FULL_PATH) > $MAX_FILE_AGE) {
rebuild_photo_file();
}
// Make sure the Cache file was built properly
if (!file_exists($PHOTO_FILE_FULL_PATH)) {
print "Sorry, I can't find the photo file: '$PHOTO_FILE_FULL_PATH'
\n";
exit;
}
$tmp_photo = $DOCROOT . $PROJECT_DIR . "/" . $PHOTO_TO_LOAD;
// Show the photo, as long as it's actually there
if (file_exists($tmp_photo)) {
?>
flush();
sleep(1);
}
// Read cache
$photo_listing = file_get_contents($PHOTO_FILE_FULL_PATH);
// Choose photo at random, and resize it to fit on screen
if ($photo_listing){
$photos = array();
$photos = explode("\n", $photo_listing);
$photo_index = rand(0, count($photos));
$src_file = $BASE_PHOTO_DIR . "/" . $photos[$photo_index];
image_createThumb($src_file,$tmp_photo,615,461);
}
?>