-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageFileManager.php
More file actions
305 lines (258 loc) · 11.7 KB
/
ImageFileManager.php
File metadata and controls
305 lines (258 loc) · 11.7 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php
/**
* ImageFileManager class
*
* Handles all file system operations related to webcam images:
* - Finding images in directories
* - Extracting date/time from filenames
* - File operations like renaming
*/
class ImageFileManager {
public function __construct() {
}
/**
* Extract date/time components from image filename
*
* @param string $filename Example: "20231114134047.jpg" or "2023/11/14/20231114134047.jpg"
* @return array [year, month, day, hour, minute, seconds]
*/
public function splitImageFilename(string $filename): array {
$yyyymmddhhmmss = $this->getYYYYMMDDHHMMSS($filename);
$year = substr($yyyymmddhhmmss, 0, 4);
$month = substr($yyyymmddhhmmss, 4, 2);
$day = substr($yyyymmddhhmmss, 6, 2);
$hour = substr($yyyymmddhhmmss, 8, 2);
$minute = substr($yyyymmddhhmmss, 10, 2);
$seconds = substr($yyyymmddhhmmss, 12, 2);
return [$year, $month, $day, $hour, $minute, $seconds];
}
/**
* Extract only the date part (YYYYMMDDHHMMSS) from a filename
* Removes directory path and .jpg extension
*
* @param string $fullPath Example: "2023/11/14/20231114144049.jpg"
* @return string Example: "20231114144049"
*/
public function getYYYYMMDDHHMMSS(string $fullPath): string {
return preg_replace("/[^0-9]/", "", pathinfo(basename($fullPath), PATHINFO_FILENAME));
}
/**
* Find the latest image in today's directory
*
* @return string Date part of filename (YYYYMMDDHHMMSS)
*/
public function findLatestImage(): string {
[$year, $month, $day] = explode('-', date('Y-m-d'));
if (is_dir("$year/$month/$day")) {
$images = glob("$year/$month/$day/*.jpg", GLOB_BRACE);
} elseif (is_dir("$year/$month")) {
$images = glob("$year/$month/**/*.jpg", GLOB_BRACE);
} elseif (is_dir("$year")) {
$images = glob("$year/**/*.jpg", GLOB_BRACE);
} else {
return '';
}
if (empty($images)) return '';
$latest_image = max($images);
$image = $this->getYYYYMMDDHHMMSS($latest_image);
return $image;
}
/**
* Find the first day with images for a specific year and month
*
* @return string Date in YYYYMMDD format (e.g., "20231101")
*/
public function findFirstDayWithImages(string $year, string $month): string {
// Pattern matches directories like "20231101", "20231102", etc.
$pattern = sprintf("%s%s*", $year, $month);
$directories = glob($pattern);
if (empty($directories)) {
return '';
}
$directory = $directories[0]; // First one in that month
return $directory;
}
/**
* Get all images in a directory for a specific day
*
* @param string $directory Example: "2023/11/14"
* @return array Array of image file paths
*/
public function getAllImagesInDirectory(string $directory): array {
$images = glob("$directory/*.jpg");
return $images;
}
/**
* Find the image in a directory whose time is closest to a target hour.
* Used as a fallback when no image exists for the exact target hour.
*
* @param string $directory Example: "2023/11/14"
* @param int $targetHour Hour (0-23)
* @return string Full path to closest image, or empty string if directory is empty
*/
public function findClosestImageToHour(string $directory, int $targetHour): string {
$images = glob("$directory/*.jpg");
if (empty($images)) return '';
$best = '';
$bestDiff = PHP_INT_MAX;
foreach ($images as $img) {
$yyyymmddhhmmss = $this->getYYYYMMDDHHMMSS($img);
$imgMinutes = (int)substr($yyyymmddhhmmss, 8, 2) * 60 + (int)substr($yyyymmddhhmmss, 10, 2);
$diff = abs($imgMinutes - $targetHour * 60);
if ($diff < $bestDiff) {
$bestDiff = $diff;
$best = $img;
}
}
return $best;
}
/**
* Get the latest image in a directory for a specific hour.
* Falls back to the image closest to that hour if none exists at the exact hour.
*
* @param string $directory Example: "2023/11/14"
* @param int $hour Hour (0-23)
* @return string Full path to image or empty string
*/
public function getLatestImageInDirectoryByDateHour(string $directory, int $hour): string {
$date = preg_replace("/[^0-9]/", "", $directory);
$hour_padded = sprintf("%02d", $hour);
$images = glob("$directory/$date$hour_padded*.jpg");
return !empty($images) ? $images[0] : $this->findClosestImageToHour($directory, $hour);
}
/**
* Find the first image after a given time.
* Falls back to the image closest to that hour if none exists at the exact hour.
*
* @param string $year
* @param string $month
* @param string $day
* @param int $hour
* @param int $minute
* @param int $seconds
* @return string Date part of filename (YYYYMMDDHHMMSS) or empty string
*/
public function findFirstImageAfterTime(string $year, string $month, string $day,
int $hour, int $minute, int $seconds): string {
$minute = sprintf("%02d", $minute);
$seconds = sprintf("%02d", $seconds);
$hour = sprintf("%02d", $hour);
$imagePattern = sprintf("%s/%s/%s/%s%s%s%s*",
$year, $month, $day, $year, $month, $day, $hour);
$images = glob($imagePattern);
if (!empty($images)) {
$image = $this->getYYYYMMDDHHMMSS($images[0]);
return $image;
}
// Fallback: no image at the target hour — pick the closest one in the day
$closest = $this->findClosestImageToHour("$year/$month/$day", (int)$hour);
return $closest ? $this->getYYYYMMDDHHMMSS($closest) : '';
}
/**
* Check and rename files that haven't been processed by cron yet
* This is a hack to handle files before cron processes them
*
* @param string $filenamePrefix Prefix to search for and remove
*/
public function checkAndRenameFilesHack(string $filenamePrefix): void {
[$year, $month, $day] = explode('-', date('Y-m-d'));
$images = glob("$year/$month/$day/$filenamePrefix*");
foreach ($images as $imageToRename) {
$newName = str_replace($filenamePrefix, '', $imageToRename);
rename($imageToRename, $newName);
}
}
/**
* Collect images to prefetch for performance optimization
*
* @param string $type Type of prefetch: 'month', 'day', or 'single'
* @param array $params Parameters specific to the type
* @return array Array of image paths to prefetch
*/
public function collectPrefetchImages(string $type, array $params): array {
$prefetch_images = [];
switch ($type) {
case 'month':
// Prefetch first 5 images of the month
$year = $params['year'];
$month = $params['month'];
$monthly_hour = $params['monthly_hour'] ?? 12;
$size = $params['size'] ?? 'mini';
$directories = glob("$year/$month/*", GLOB_ONLYDIR);
if ($directories) {
sort($directories);
$count = 0;
foreach ($directories as $directory) {
if ($count >= 5) break;
$image = $this->getLatestImageInDirectoryByDateHour($directory, $monthly_hour);
if ($image) {
$yyyymmddhhmmss = $this->getYYYYMMDDHHMMSS($image);
[$img_year, $img_month, $img_day] = $this->splitImageFilename($yyyymmddhhmmss);
if ($size == "mini" || empty($size)) {
if (file_exists("$img_year/$img_month/$img_day/mini/$yyyymmddhhmmss.jpg")) {
$prefetch_images[] = "$img_year/$img_month/$img_day/mini/$yyyymmddhhmmss.jpg";
} else {
$prefetch_images[] = "$img_year/$img_month/$img_day/$yyyymmddhhmmss.jpg";
}
} else {
$prefetch_images[] = "$img_year/$img_month/$img_day/$yyyymmddhhmmss.jpg";
}
$count++;
}
}
}
break;
case 'day':
// Prefetch first 5 images of the day
$directory = $params['directory'];
$dawn = $params['dawn'];
$dusk = $params['dusk'];
$size = $params['size'] ?? 'mini';
if (file_exists($directory)) {
$all_images = glob("$directory/*.jpg");
// Get last 10 images, then filter
$recent_images = array_slice($all_images, -10);
rsort($recent_images);
$count = 0;
foreach ($recent_images as $img) {
if ($count >= 5) break;
$img_yyyymmddhhmmss = $this->getYYYYMMDDHHMMSS($img);
[$img_year, $img_month, $img_day, $img_hour, $img_minute, $img_seconds] =
$this->splitImageFilename($img_yyyymmddhhmmss);
$img_timestamp = mktime((int)$img_hour, (int)$img_minute, (int)$img_seconds,
(int)$img_month, (int)$img_day, (int)$img_year);
if ($img_timestamp >= $dawn && $img_timestamp <= $dusk) {
if ($size == "mini" || empty($size)) {
if (file_exists("$img_year/$img_month/$img_day/mini/$img_yyyymmddhhmmss.jpg")) {
$prefetch_images[] = "$img_year/$img_month/$img_day/mini/$img_yyyymmddhhmmss.jpg";
} else {
$prefetch_images[] = "$img_year/$img_month/$img_day/$img_yyyymmddhhmmss.jpg";
}
} else {
$prefetch_images[] = "$img_year/$img_month/$img_day/$img_yyyymmddhhmmss.jpg";
}
$count++;
}
}
}
break;
case 'single':
// Prefetch current and next image
$year = $params['year'];
$month = $params['month'];
$day = $params['day'];
$image_filename = $params['image_filename'];
$next_image = $params['next_image'] ?? null;
// Prefetch the current image
$prefetch_images[] = "$year/$month/$day/$image_filename";
// Prefetch next image if available
if ($next_image) {
$next_img_datepart = $this->getYYYYMMDDHHMMSS($next_image);
[$next_year, $next_month, $next_day] = $this->splitImageFilename($next_img_datepart);
$prefetch_images[] = "$next_year/$next_month/$next_day/$next_img_datepart.jpg";
}
break;
}
return $prefetch_images;
}
}