-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.php
165 lines (139 loc) · 6.2 KB
/
helper.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
// Helper File
// Function for Console Output
function debug_to_console( $data ) {
$output = $data;
if ( is_array( $output ) )
$output = implode( ',', $output);
echo "<script>console.log( 'Debug Information: " . $output . "' );</script>";
}
if($apikey == ''){
$noapikeywarn = '<div class="nx-block nx-center nx-warning"><b>WARNING</b><br>Your API Key is not set. Please enter your Flickr API Key in the settings.<br>You can get an API Key <a href="https://www.flickr.com/services/api/misc.api_keys.html" target="_blank">here</a></div>';
echo $noapikeywarn;
alert($noapikeywarn);
}
if($photosetID == ''){
$nosetidwarn = '<div class="nx-block nx-center nx-warning"><b>WARNING</b><br>There is no Photoset ID given. Please enter a Flickr Photoset ID in the settings.</div>';
echo $nosetidwarn;
alert($nosetidwarn);
}
function setvar($type,$url){
// use global var's
global $photoset_array;
global $photoset_JSON_url;
global $photoset_length;
global $photoset_title;
$response = json_decode(file_get_contents($url));
switch($type){
case 1: // Get Gallery Informations (photoarray,length, title)
$photoset_JSON_url = $url;
$photoset_array = $response->photoset->photo;
$photoset_length = $response->photoset->total;
$photoset_title = $response->photoset->title;
break;
case 2: // nothing to do if we start the function with the JSON for imagesizes
return $response;
break;
}
}
checkCache(1,$photosetID,$apikey);
function checkCache($type,$ID,$apikey){
global $showdebug;
global $photosetID;
switch($type){
case 1: // set $file path if we check for photoset informations
$file = "cache/$photosetID/photoset_$ID.json";
break;
case 2: // set $file path if we check for image informations
$file = "cache/$photosetID/img_$ID.json";
break;
}
if (file_exists($file)) {
if($showdebug){
debug_to_console( "The File $file exists already");
}
$response = setvar($type,$file);
} else {
if($showdebug){
debug_to_console( "The File $file does not exist");
}
// if file does not exists get the informations and create it
$filecreated = createCache($type,$ID,$apikey);
$response = setvar($type, $filecreated);
if($showdebug){print_r($response);}
}
if($type == 2){
// if we use the function for image informations get now the large Image URL from the JSON
$imglargeurl = getZoomedIMG($response);
return $imglargeurl;
}
}
// creates the JSON Files into the chache folder
function createCache($type,$ID,$apikey){
global $photosetID;
global $showdebug;
switch ($type){
case 1: // create folder & file for the photoset informations
global $photoset_url;
$photoset_url = "https://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=$apikey&photoset_id=$ID&format=json&nojsoncallback=1";
$photoset = json_decode(file_get_contents($photoset_url),true);
if (!file_exists('cache/'.$photosetID)) {
mkdir('cache/'.$photosetID, 0777, true);
}
$fp = fopen('cache/'.$photosetID.'/photoset_'.$ID.'.json', 'w');
fwrite($fp, json_encode($photoset));
fclose($fp);
$filecreated = "cache/$photosetID/photoset_$ID.json"; // "cache/123456789/photoset_987654321.json"
break;
case 2: // create file for image informations (each image has its own .JSON File)
$url = "https://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=$apikey&photo_id=$ID&format=json&nojsoncallback=1";
$sizesArray = json_decode(file_get_contents($url),true);
$fp = fopen('cache/'.$photosetID.'/img_'.$ID.'.json', 'w');
fwrite($fp, json_encode($sizesArray));
fclose($fp);
$filecreated = "cache/$photosetID/img_$ID.json"; // "cache/123456789/img_987654321.json"
break;
}
if($showdebug){debug_to_console( "The File $filecreated was successfully created");}
return($filecreated);
}
function getZoomedIMG($imgDetails){
global $showdebug;
if($showdebug){
echo '<div class="uk-width-1-1"><br><hr><h3>Image Details</h3>';
print_r ($imgDetails->sizes->size);
echo '<br></div>';
echo '<div class="uk-width-1-1"><h4>Maximal Available Size</h4>';
}
if (in_array("Large 2048", array_column($imgDetails->sizes->size, 'label'))) {
if($showdebug){echo 'Large 2048 is available<br>';}
$key = array_search('Large 2048', array_column($imgDetails->sizes->size, 'label'));
if($showdebug){echo 'Large 2048 is available on position '.$key.'<br></div>';}
$imglargeurl = $imgDetails->sizes->size[$key]->source;
if($showdebug){echo '<div class="uk-width-1-1"><h4>Large Image Link</h4><a href="'.$imglargeurl.'" target="_blank">'.$imglargeurl.'</a></div>';}
}elseif(in_array("Large 1600", array_column($imgDetails->sizes->size, 'label'))){
if($showdebug){echo 'Large 1600 is available<br>';}
$key = array_search('Large 1600', array_column($imgDetails->sizes->size, 'label'));
if($showdebug){echo 'Large 1600 is available on position '.$key.'<br></div>';}
$imglargeurl = $imgDetails->sizes->size[$key]->source;
if($showdebug){echo '<div class="uk-width-1-1"><h4>Large Image Link</h4><a href="'.$imglargeurl.'" target="_blank">'.$imglargeurl.'</a></div>';}
}elseif(in_array("Large 1024", array_column($imgDetails->sizes->size, 'label'))){
if($showdebug){echo 'Large 1024 is available<br>';}
$key = array_search('Large 1024', array_column($imgDetails->sizes->size, 'label'));
if($showdebug){echo 'Large 1024 is available on position '.$key.'<br></div>';}
$imglargeurl = $imgDetails->sizes->size[$key]->source;
if($showdebug){echo '<div class="uk-width-1-1"><h4>Large Image Link</h4><a href="'.$imglargeurl.'" target="_blank">'.$imglargeurl.'</a></div>';}
}elseif(in_array("Original", array_column($imgDetails->sizes->size, 'label'))){
if($showdebug){echo 'Original is available<br>';}
$key = array_search('Original', array_column($imgDetails->sizes->size, 'label'));
if($showdebug){echo 'Original is available on position '.$key.'<br></div>';}
$imglargeurl = $imgDetails->sizes->size[$key]->source;
if($showdebug){echo '<div class="uk-width-1-1"><h4>Large Image Link</h4><a href="'.$imglargeurl.'" target="_blank">'.$imglargeurl.'</a></div>';}
}else{
$imglargeurl = '';
if($showdebug){echo '<b>There where no large images available.</b></div>';}
if($showdebug){debug_to_console('There where no large images available');}
}
return $imglargeurl;
}
?>