-
Notifications
You must be signed in to change notification settings - Fork 0
/
getRandomImage.php
47 lines (40 loc) · 1.55 KB
/
getRandomImage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
/*==============================================================================
* (C) Copyright 2014, 2020 John J Kauflin, All rights reserved.
*----------------------------------------------------------------------------
* DESCRIPTION: Service to return the url path to a random photo in a dir
*----------------------------------------------------------------------------
* Modification History
* 2014-11-12 JJK Initial version from old displayRandomImages function
* 2020-03-14 JJK Added a MediaRootDir include to define variables
* 2020-12-19 JJK Hard-coded media root and reset levels
*============================================================================*/
$MediaRootDir = "Media/";
$phpRootReset = "../../../";
$photoURL = '';
try {
$mediaImagesDir = $MediaRootDir . 'images/';
$rootDir = $phpRootReset . $mediaImagesDir;
if (isset($_REQUEST["rootDir"])) {
$inDir = urldecode($_REQUEST["rootDir"] . '/');
$mediaImagesDir = $mediaImagesDir . $inDir;
$rootDir = $rootDir . $inDir;
}
//error_log(date('[Y-m-d H:i] '). '$rootDir = ' . $rootDir . PHP_EOL, 3, 'php.log');
if (file_exists($rootDir)) {
$files = scandir($rootDir);
// filter out non images
foreach($files as $file) {
if (stripos($rootDir . $file,'.JPG')) {
$imagesArray[] = $file;
}
}
$arrayIndex = array_rand($imagesArray);
$photoURL = $mediaImagesDir . $imagesArray[$arrayIndex];
} // if (file_exists($rootDir)) {
}
catch (Exception $e) {
//echo 'An error occured: ' . $e->message;
}
echo $photoURL;
?>