This repository has been archived by the owner on Jun 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimages.php
60 lines (57 loc) · 1.48 KB
/
images.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
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/**
* Build image crediting link from filename.
*
* @author Fabian Dennler <[email protected]>
*
* @param string $filename Unsplash filename.
* @return string $credits Image crediting link.
*/
function imageCrediting($filename)
{
$credits = str_replace('-unsplash.jpg', '', $filename);
$credits = preg_replace('/[0-9]+/', '', $credits);
$credits = trim(str_replace('-', ' ', $credits));
$credits = ucwords($credits);
$credits = sprintf('<i class="far fa-image"></i> <strong>%s</strong>', $credits);
return $credits;
}
/**
* Build the array of images (Only JPG) from folder.
*
* @author Fabian Dennler <[email protected]>
*
* @return array Shuffled images with crediting informations.
*/
function loadImages()
{
$images = array();
$allowed_types = [ 'jpg' ];
$handle = opendir(dirname(realpath(__FILE__)).'/images/');
while($file = readdir($handle))
{
if($file !== '.' && $file !== '..')
{
$file_type = pathinfo($file, PATHINFO_EXTENSION);
if (in_array($file_type, $allowed_types) == true)
{
$credits = imageCrediting($file);
array_push($images, [
'file' => $file,
'credits' => $credits,
]);
}
}
}
shuffle($images);
return $images;
}
#if( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && ( $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' ) )
#{
$images = loadImages();
if (is_array($images)) {
header('Content-Type: application/json');
echo json_encode($images);
}
#}
?>