<?php
/*
By Olaf Nöhring, 2010, http://www.team-noehring.de
Purpose of this script:
Read a RANDOM file in a RANDOM subdirectory and return this file (image in this specific case).
Example: Show a random image
Problem or - why is this script needed:
There are about 60.000 files in the subdirectories that are in our pool of possible files.
Other methods I found were not able to recreate this effect since the PHP execution time always ran our (of it produced memory problems).
This should be quite easy and fast.
*/
//Define source directory, no trailing slash! Example "../photo" NOT this: "../photo/"
$source_directory = "../../photo";
//./html/projekte/bilder";
//What width and height is the maximum for the pictures (in pixel!)
//TO BE SET AT A LATER TIME!
$max_width = 200;
$max_height = 200;
//--------------- NO CHANGES BELOW REQUIRED ----------------------
//STart randomizer (Base: time)
srand((double)microtime()*1000000);
//array of files without directories... optionally filtered by extension
function file_list($d,$x){
foreach(array_diff(scandir($d),array('.','..','@eaDir','lost+found')) as $f)if(is_file($d.'/'.$f)&&(($x)?preg_match($x,$f):1))$l[]=$f;
return $l;
}
//array of directories
function dir_list($d){
// Next line removed from array to have access to a path where the recursive algorithm is in at a certain time
// '.',
foreach(array_diff(scandir($d),array('..','@eaDir','lost+found')) as $f)if(is_dir($d.'/'.$f))$l[]=$f;
return $l;
}
//Choose randomly a directory from which an image should be picked
function choose_dir($calculated_dir) {
$directories = dir_list($calculated_dir);
//Choose a directory in the initial source directory. It's possible to choose the source directory itself (Value: @eaDir on a Synology DS209)
$count_dirs = count($directories);
$random = (rand()%$count_dirs);
//START debug
echo "<hr>V1:Verzeichnisse in <strong>". $calculated_dir ."</strong>:<br>";
print_r($directories);
echo "<br>----V2:Gewähltes Verzeichnis: ". $directories[$random];
echo "<br>----V3:Länge Verzeichnis: ". strlen($directories[$random]);
//END DEBUG
return $directories[$random];
}
//Choose an image from the previously calculated directory
function choose_file ($s_dir) {
//Choose filename in the chosen directory:
$dateien = file_list($s_dir,'(gif|GIF|png|PNG|jpg|JPG)');
//We do not shuffle the array itself, because we do not need to and I suppose this would take more time. We need only 1 part of the array!
//Choose ONE File to return:
$count_files = count($dateien);
if ($count_files ==0) {
return "/no-file/";
}
$random = (rand()%$count_files);
//$return = $s_dir."/".$dateien[$random];
$return = $dateien[$random];
//START debug
//echo "<hr>Dateien:<br>";
//print_r($dateien);
//echo "<br><br>Gewählte Datei: ". $return;
//END DEBUG
return $return;
}
// Calculates restricted dimensions with a maximum of $goal_width by $goal_height
function resize_dimensions($goal_width,$goal_height,$width,$height) {
$return = array('width' => $width, 'height' => $height);
// If the ratio > goal ratio and the width > goal width resize down to goal width
if ($width/$height > $goal_width/$goal_height && $width > $goal_width) {
$return['width'] = $goal_width;
$return['height'] = $goal_width/$width * $height;
}
// Otherwise, if the height > goal, resize down to goal height
else if ($height > $goal_height) {
$return['width'] = $goal_height/$height * $width;
$return['height'] = $goal_height;
}
return $return;
}
function getmicrotime()
{
list($usec, $sec) = explode(" ",microtime());
return ($usec + $sec);
}
$Laufzeit = getmicrotime();
//Test if a single image is being returned
//echo "<br>Testausgabe einer Datei in Verzeichnis: ".choose_file($source_directory)."<br>";
$target_dir = "";
while ($target_dir != '/.') {
$source_directory = $source_directory.$target_dir;
$target_dir = '/'. choose_dir ($source_directory);
}
echo "<br>Am Ende:" . $source_directory ."<br>";
//Resize Image if needed and output it - we are done
$target_file = choose_file($source_directory);
if ($target_file == "/no-file/") {
//maybe there is no file in the directory
//ggf. Function !
}
$image = $source_directory."/".$target_file;
// Usage example to find the proper dimensions to resize an image down to 300x400 pixels maximum:
list($width, $height) = getimagesize($image);
$new_dimensions = resize_dimensions($max_width,$max_height,$width,$height);
echo "<hr><br>Link: ". $image;
?>
<br>
<img src="<?php echo $image; ?>" width="<?php echo $new_dimensions[width]; ?>" height="<?php echo $new_dimensions[height] ?>
">
<br>Fertig
<?php
$Laufzeit = getmicrotime() - $Laufzeit;
echo "<br>Laufzeit: ". $Laufzeit;
?>