-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebcam.php
1222 lines (1053 loc) · 51.4 KB
/
webcam.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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* ============================================================
//
// webcam.php
//
// Code: https://github.com/cloveras/webcam
//
// Example: http://lilleviklofoten.no/webcam/
//
============================================================ */
// Functions
// ============================================================
// Page header with title and Javascript navigation
// ------------------------------------------------------------
function page_header($title, $previous, $next, $up, $down) {
print <<<END1
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="description" content="Lofoten webcam with view towards west from Vik, Gimsøy, Lofoten, Norway.">
<meta name="keywords" content="lofoten,webcam,webcamera,webkamera,web cam, webcam,vik,gimsøy,lofoten islands,nordland,norway">
<meta name="robot" content="index">
<meta name="generator" content="webcam.php: https://github.com/cloveras/webcam">
<link rel="icon" href="/wp-content/uploads/2020/08/cropped-lillevik-drone-001-20200613-0921-21-2-scaled-2-32x32.jpg" sizes="32x32">
<link rel="icon" href="/wp-content/uploads/2020/08/cropped-lillevik-drone-001-20200613-0921-21-2-scaled-2-192x192.jpg" sizes="192x192">
<link rel="apple-touch-icon" href="/wp-content/uploads/2020/08/cropped-lillevik-drone-001-20200613-0921-21-2-scaled-2-180x180.jpg">
<link rel="stylesheet" type="text/css" href="css.php">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
END1;
// Set some web variables for command-line use.
if (! $_SERVER['SERVER_NAME']) {
$_SERVER['SERVER_NAME'] = "lilleviklofoten.no";
$_SERVER['SCRIPT_NAME'] = "webcam.php";
}
if ($previous) {
echo " <link rel=\"prefetch\" title=\"Previous\" href=\"http://" . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'] . "$previous\">\n";
}
if ($next) {
echo " <link rel=\"prefetch\" title=\"Next\" href=\"http://" . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'] . "$next\">\n";
}
print <<<END2
<title>$title</title>
END2;
// Javascript for navigation using arrow keys. Only print the ones that do something.
if ($previous || $next || $up || $down) {
echo "\n\n <!-- Javascript for navigation using arrow keys. -->\n";
echo " <script>\n";
function printArrowScript($keyCode, $url) {
echo " function {$keyCode}ArrowPressed() { window.location.href=\"$url\"; }\n";
}
if ($previous) printArrowScript("left", $previous);
if ($next) printArrowScript("right", $next);
if ($up) printArrowScript("up", $up);
if ($down) printArrowScript("down", $down);
echo " document.onkeydown = function(evt) {\n";
echo " evt = evt || window.event;\n";
echo " switch (evt.keyCode) {\n";
if ($previous) echo " case 37: leftArrowPressed(); break;\n";
if ($up) echo " case 38: upArrowPressed(); break;\n";
if ($next) echo " case 39: rightArrowPressed(); break;\n";
if ($down) echo " case 40: downArrowPressed(); break;\n";
echo " }\n";
echo " };\n";
echo " </script>\n\n";
}
// Google Analytics and Microsoft Clarity
print<<<END3
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-P8Z20DT0NR"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-P8Z20DT0NR');
</script>
<!-- Microsoft Clarity -->
<script>
(function(c,l,a,r,i,t,y){
c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
})(window, document, "clarity", "script", "brp4ocus57");
</script>
</head>
<body>
<h1>$title</h1>
<p>
<a href=".">Webcam</a>
at
<a href="https://lilleviklofoten.no">Lillevik Lofoten</a>,
Vik, Gimsøy, Lofoten, Norway.
See also: <a href="https://lilleviklofoten.no/webcams/">many other webcams in Lofoten</a>.
</p>
END3;
}
// Debug
// ------------------------------------------------------------
function debug($txt) {
global $debug;
if ($debug) {
echo "$txt<br/>\n";
}
}
// Footer
// ------------------------------------------------------------
function footer($images_printed, $previous, $next, $up, $down) {
$touch = true;
if ($touch) {
echo <<<TOUCH
<!-- Touch gestures -->
<!-- script src="https://hammerjs.github.io/dist/hammer.min.js"></script -->
<script src="https://cdn.jsdelivr.net/npm/hammerjs@2.0.8"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
var body = document.body;
var hammer = new Hammer(body);
hammer.on('swiperight', function() {
window.location.href = '{$previous}';
});
hammer.on('swipeleft', function() {
window.location.href = '{$next}';
});
});
</script>
TOUCH;
}
// Navigation links
echo "\n\n<p>";
if ($touch) {
echo "Swipe left/right or use ";
} else {
echo "Use ";
}
echo 'the arrow keys to navigate: ';
if ($next) {
echo "<a href=\"$next\">forward</a> (→), ";
}
if ($previous) {
echo "<a href=\"$previous\">back</a> (←), ";
}
if ($up) {
echo "<a href=\"$up\">up</a> (↑) ";
}
if ($down) {
echo "and <a href=\"$down\">down</a> (↓)\n";
}
echo ".</p>\n\n";
echo "<p style=\"color: rgb(200, 200, 200);\" >Made with <a style=\"color: rgb(200, 200, 200);\" href=\"https://github.com/cloveras/webcam\">webcam.php</a></p>\n\n";
echo "</body>\n</html>\n";
}
// Get variables from the date part of the image filename.
// ------------------------------------------------------------
function split_image_filename($image_filename) {
if ($debug) {
debug("<br/>IN split_image_filename($image_filename): $year-$month-$day $hour:$minute:$seconds");
}
// Example filename: 20231114134047.jpg
$yyyymmddhhmmss = get_yyyymmddhhmmss($image);
// We now have: 20231114134047
// Which is : YYYYMMDDHHMMSS
$year = substr($image_filename, 0, 4);
$month = substr($image_filename, 4, 2);
$day = substr($image_filename, 6, 2);
$hour = substr($image_filename, 8, 2);
$minute = substr($image_filename, 10, 2);
$seconds = substr($image_filename, 12, 2);
if ($debug) {
debug("<br/>split_image_filename($image_filename): $year-$month-$day $hour:$minute:$seconds");
}
return array($year, $month, $day, $hour, $minute, $seconds);
}
// Midnight sun? (tested with date_sunrise() and GPS coordinates used in this script on yr.no)
// ------------------------------------------------------------
function midnight_sun($timestamp, $latitude, $longitude) {
$month = date('m', $timestamp);
$day = date('d', $timestamp);
return ($month == 5 && $day >= 24) || ($month == 6) || ($month == 7 && $day <= 18);
}
// Is it polar night? (tested with date_sunrise() and GPS coordinates used in this script on yr.no)
// ------------------------------------------------------------
function polar_night($timestamp, $latitude, $longitude) {
$month = date('m', $timestamp);
$day = date('d', $timestamp);
return ($month == 12 && $day >= 6) || ($month == 1 && $day <= 6);
}
// Find sunrise and sunset, return all kinds of stuff we need later.
// Fakes sunrise and sunset for midnight sun and polar night.
// Adjusts dawn and susk to be the same day as sunrise and sunset.
// ------------------------------------------------------------
function find_sun_times($timestamp) {
// Return timestamps for everything.
debug("find_sun_times($timestamp) (" . date('Y-m-d H:i', $timestamp) . ")");
// Default values
$sunrise = $sunset = $dawn = $dusk = 0;
$midnight_sun = $polar_night = false;
// When to start showing images during the polar night.
$polar_night_fake_sunrise_hour = 8;
$polar_night_fake_sunset_hour = 15;
$adjust_dawn_dusk_hours = 2; // How much before/after sunrise/sunset is dawn/dusk.
$adjust_dawn_dusk_seconds = $adjust_dawn_dusk_hours * 60 * 60; // How much before/after sunrise/sunset is dawn/dusk.
$polar_night_fake_dawn_hour = $polar_night_fake_sunrise_hour - $adjust_dawn_dusk_hours;
$polar_night_fake_dusk_hour = $polar_night_fake_sunset_hour + $adjust_dawn_dusk_hours;
$polar_night_hours = 30; // Adding this to the hours below.
// Where: Årstrandveien 663, 8314 Gimsøysand.
$latitude = 68.3300814; // North
$longitude = 14.0917529 ; // East
// We will need these below.
$year = date('Y', $timestamp);
$month = date('m', $timestamp);
$day = date('d', $timestamp);
// Find the timestamps for sunrise, sunset, dawn, and dusk (as unix timestamps)
if (midnight_sun($timestamp, $latitude, $longitude)) {
debug("MIIDNIGHT SUN!");
$midnight_sun = true;
// We still need to show a few images, so: faking sunrise and sunset.
$sunrise = mktime(0, 0, 1, $month, $day, $year); // 00:00:01, One second after midnight
$sunset = mktime(23, 59, 59, $month, $day, $year); // 23:59:59
$dawn = $sunrise;
$dusk = $sunset;
} elseif (polar_night($timestamp, $latitude, $longitude)) {
debug("POLAR NIGHT!");
$polar_night = true;
// We still need to show a few images, so: faking sunrise and sunset.
$sunrise = mktime($polar_night_fake_sunrise_hour, 0, 0, $month, $day, $year);
$sunset = mktime($polar_night_fake_sunset_hour, 0, 0, $month, $day, $year);
$dawn = mktime($polar_night_fake_dawn_hour, 0, 0, $month, $day, $year);
$dusk = mktime($polar_night_fake_dusk_hour, 0, 0, $month, $day, $year);
} else {
// Do the math! Use the $timestamp passed as a parameter.
debug("NOT MIDNIGHT SUN OR POLAR NIGHT! timestamp: $timestamp, human-readable timestamp: " . date('Y-m-d H:i:s', $timestamp));
// Get all sun info with PHP 8's built-in functionality.
$sun_info = date_sun_info($timestamp, $latitude, $longitude);
debug("date_sun_info($timestamp, $latitude, $longitude");
foreach ($sun_info as $key=>$val) {
debug("$key: [$val]" . date("Y-m-d H:i'", $val));
}
$sunrise = $sun_info['sunrise'];
$sunset = $sun_info['sunset'];
debug("<br/>STEP 1: find_sun_times($timestamp) (" . date('Y-m-d H:i', $timestamp) . ")");
debug("sunrise: $sunrise (" . date('Y-m-d H:i', $sunrise) . ")");
debug("sunset: $sunset (" . date('Y-m-d H:i', $sunset) . ")");
// Find sunrise, and fix it if we don't get a time for it.
if ($sunrise == 1 or $sunrise == 1715464868 or ! $sunrise) {
debug("Sunrise TO FIX: " . date('Y-m-d H:i', $sunrise));
$sunrise = mktime(0, 0, 0, $month, $day, $year); // Midnight
debug("Sunrise FIXDED: " . date('Y-m-d H:i', $sunrise));
}
// Find sunset, and fix it if we don't get a time for it.
if ($sunset == 1 or $sunset == 1715464868 or ! $sunset) {
debug("Sunset TO FIX: " . date('Y-m-d H:i', $sunset));
$sunset = mktime(23, 59, 59, $month, $day, $year); // Almost midnight again
debug("Sunset FIXED: " . date('Y-m-d H:i', $sunset));
}
// At the beginning and end of the midnight sun and polar night periods,
// the dawn and dusk need a bit of extra work.
// Find dawn
$dawn = $sun_info['nautical_twilight_begin'];
debug("Dawn 1: $dawn ( " . date('Y-m-d H:i', $dawn) . ")");
if ($dawn == 1715464868 or ! $dawn) {
debug("No nautical_twilight_begin, setting dawn to: sunrise - $adjust_dawn_dusk_seconds seconds");
// Set dawn to the fixed time before sunrise.
$dawn = $sunrise - $adjust_dawn_dusk_seconds;
// If dawn was set to the day before: Fix it.
if (date('d', $dawn) != $day) {
// Set dawn to 00:00:00
debug("Set dawn to 00:00:00");
$dawn = mktime(0, 0, 0, $month, $day, $year);
}
debug("Dawn 2: $dawn ( " . date('Y-m-d H:i', $dawn) . ")");
}
// Find dusk
$dusk = $sun_info['nautical_twilight_end'];
debug("Dusk 1: $dusk ( " . date('Y-m-d H:i', $dusk) . ")");
if ($dusk == 1715464868 or ! $dusk) {
debug("No nautical_twilight_end, setting dusk to: sunset + $adjust_dawn_dusk_seconds seconds");
// Set dawn to the fixed time after sunset.
$dusk = $sunset + $adjust_dawn_dusk_seconds;
if (date('d', $dusk) != $day) {
// Set dusk to 23:59:59
debug("Set dusk to 23:59:59");
$dusk = mktime(23, 59, 59, $month, $day, $year);
}
debug("Dusk 2: $dusk ( " . date('Y-m-d H:i', $dusk) . ")");
}
}
debug("<br/>STEP 2: find_sun_times($timestamp) (" . date('Y-m-d H:i', $timestamp) . ")");
debug("midnight_sun: $midnight_sun");
debug("polar_night: $polar_night");
debug("dawn: $dawn (" . date('Y-m-d H:i', $dawn) . ")");
debug("sunrise: $sunrise (" . date('Y-m-d H:i', $sunrise) . ")");
debug("sunset: $sunset (" . date('Y-m-d H:i', $sunset) . ")");
debug("dusk: $dusk (" . date('Y-m-d H:i', $dusk) . ")");
return array($sunrise, $sunset, $dawn, $dusk, $midnight_sun, $polar_night);
}
// Print one image for every day in the month.
// ------------------------------------------------------------
function print_full_month($year, $month) {
debug("<br/>print_full_month($year, $month)");
global $size;
global $monthly_day;
global $monthly_hour;
global $large_image_width;
global $large_image_height;
global $mini_image_width;
global $mini_image_height;
// Find previous and next month, and create the links to them.
list($year_previous, $month_previous, $year_next, $month_next) = find_previous_and_next_month($year, $month);
$previous = "?type=month&year=$year_previous&month=$month_previous&size=$size"; // Previous month.
$next = "?type=month&year=$year_next&month=$month_next&size=$size"; // Next month.
$up = "?type=year&year=$year"; // Up: SHow the full year.
// "Down" goes to the first day n this month that has images.
$first_day_with_images = find_first_day_with_images($year, $month);
if ($first_day_with_images) {
$down = "?type=day&date=" . find_first_day_with_images($year, $month);
} else {
$down = false;
}
// Make timestamp for this month.
$minute = 0;
$second = 0;
$timestamp = mktime($monthly_hour, 0, 0, $month, $monthly_day, $year); // Using the $monthly_day as average.
$title = "Lillevik Lofoten webcam: " . date("F Y", $timestamp) . " (ca. $monthly_hour:00 each day)";
page_header($title, $previous, $next, $up, $down);
list($sunrise, $sunset, $dawn, $dusk, $midnight_sun, $polar_night) = find_sun_times($timestamp);
print_sunrise_sunset_info($sunrise, $sunset, $dawn, $dusk, $midnight_sun, $polar_night, "average");
print_yesterday_tomorrow_links($timestamp, true);
// CSS overlay
echo "<div class=\"grid-container\">";
$images_printed = 0;
for ($i = 1; $i <= 31; $i+=1) { // Works for February and 30-day months too.
$now = mktime($hour, $minute, $second, $month, $i, $year);
$i = sprintf("%02d", $i); // Need to pad the days with 0 first. Still works fine in for() above.
// Get all *jpg images that start with the right year, month, day and hour.
$directory = $year . "/" . $month . "/" . $i;
if (file_exists($directory)) {
debug("Directory exists: $directory");
// Getting the latest image in that directory for that hour (monthly hour = the fixed hour we look for).
$image = get_latest_image_in_directory_by_date_hour($directory, $monthly_hour);
if ($image) {
debug("Image found: $image");
// There was at least one image: 2023/11/14/20231114154051.jpg
$yyyymmddhhmmss = get_yyyymmddhhmmss($image);
debug("Image datepart: $yyyymmddhhmmss<br/>");
list($year, $month, $day, $hour, $minute, $seconds) = split_image_filename($yyyymmddhhmmss);
// Print mini images, link to all images for that day.
// CSS overlay
echo "<div class=\"grid-item\">";
echo "<a href=\"?type=day&date=$year$month$day\">";
echo "<img alt=\"Lillevik Lofoten webcam: $year-$month-$day $hour:$minute\" ";
//echo "title=\"$year-$month-$day $hour:$minute\" ";
echo "src=\"$year/$month/$day/";
if ($size == "mini" || empty($size)) {
// Mini. If the mini version has been created: Use that. If not: Scale down the large version.
if (file_exists("$year/$month/$day/mini/$yyyymmddhhmmss.jpg")) {
echo "mini/";
}
echo "$yyyymmddhhmmss.jpg\" width=\"$mini_image_width\" height=\"$mini_image_height\" ";
} else {
// Large.
echo "$yyyymmddhhmmss.jpg\" width=\"$large_image_width\" height=\"$large_image_height\" ";
}
echo "></a>\n";
// CSS overlay
if ($size == "mini" || empty($size)) {
echo "<span class=\"time\">$day</span>";
}
echo "</div>";
$images_printed += 1; // Count the image just printed.
}
} else {
debug("Directory does not exist: $directory");
}
}
// CSS overlay
echo "</div>\n";
if ($images_printed == 0) {
// No pictures found for this month.
echo "<p>(No photos to display for " . date("Y-m-d", $timestamp) . ")</p>\n";
}
footer($images_printed, $previous, $next, $up, $down);
}
// Print images for a whole year.
// ------------------------------------------------------------
function print_full_year($year) {
debug("<br/>print_full_year($year)");
global $monthly_hour;
global $mini_image_width;
global $mini_image_height;
// Find previous and next year, and create the links to them.
$previous = $next = $up = $down = false;
$previous = "?type=year&year=" . ($year - 1);
if ($year < date('Y')) {
$next = "?type=year&year=" . ($year + 1); // Next year only if it exists.
}
// Find the first day in the month, and use that for the down link.
$first_day_with_images = "";
for ($month = 1; $month <= 12; $month++) {
$month = sprintf("%02d", $month);
$first_day_with_images = find_first_day_with_images($year, $month);
if ($first_day_with_images) {
// We found a month (and also a day, which we don't need now).
$down = "?type=month&year=$year&month=$month";
break;
}
}
page_header("Lillevik Lofoten webcam: $year (ca. $monthly_hour:00 each day)", $previous, $next, $up, $down);
print_previous_next_year_links($year);
// Links to all months 1-12: Commas and "and" for the last one.
echo "\n<p>Months: \n";
$monthLinks = [];
for ($i = 1; $i <= 12; $i++) {
$monthLinks[] = "<a href=\"?type=month&year=$year&month=" . sprintf("%02d", $i) . "\">" . sprintf("%02d", $i) . "</a>";
}
$formattedMonthLinks = implode(', ', array_slice($monthLinks, 0, -1)) . ' and ' . end($monthLinks);
print $formattedMonthLinks . ".\n";
// Link to today.
echo "<a href=\"?type=day&date=" . date('Ymd') . "\">Today: " . date("M d") . "</a>.\n";
echo "<a href=\"?type=last\">Latest image</a>.\n";
echo "</p>\n\n";
// Loop through all months 1-12 (again) and print images for the $days if they exist.
// CSS overlay
echo "<div class=\"grid-container\">";
$days = range(1, 31);
$images_printed = 0;
$yyyymmddhhmmss = "";
$image_filename = "";
for ($month = 1; $month <= 12; $month++) {
$month = sprintf("%02d", $month);
// Check for each of the days in the $days array
foreach ($days as $day) {
$day = sprintf("%02d", $day);
// Find first image for that day taken after $monthly_hour
debug("monthly_hour: $monthly_hour");
debug("find_first_image_after_time($year, $month, $day, $monthly_hour, 0, 0);");
$yyyymmddhhmmss = find_first_image_after_time($year, $month, $day, $monthly_hour, 0, 0);
if ($yyyymmddhhmmss) {
// There was an image.
$hour = substr($yyyymmddhhmmss, 8, 2);
$minute = substr($yyyymmddhhmmss, 10, 2);
$image_filename = $year . "/" . $month . "/" . $day . "/" . $yyyymmddhhmmss . ".jpg";
debug($year . "/" . $month . "/" . $day . "/" . $yyyymmddhhmmss . ".jpg");
// Print mini images (never large images for full years), link to all images for that day.
// CSS overlay
echo "<div class=\"grid-item\">";
echo "<a href=\"?type=one&image=$yyyymmddhhmmss\">";
echo "<img alt=\"Lillevik Lofoten webcam: $year-$month-$day $hour:$minute\" ";
//echo "title=\"$year-$month-$day $hour:$minute\" ";
echo "width=\"$mini_image_width\" height=\"$mini_image_height\" ";
echo "src=\"$year/$month/$day/";
// If the mini version has been created: Use that. If not: Scale down the full version.
if (file_exists("$year/$month/$day/mini/$yyyymmddhhmmss.jpg")) {
echo "mini/";
}
echo "$yyyymmddhhmmss.jpg\"></a>\n";
// CSS overlay
if ($size == "mini" || empty($size)) {
echo "<span class=\"time\">$year-$month-$day</span>";
}
//echo "<span class=\"time\">$year-$month-$day</span>";
echo "</div>";
$images_printed += 1;
}
}
}
// CSS overlay
echo "</div>\n";
if ($images_printed > 0) {
echo "</p>\n";
} else {
// No pictures found for this year.
echo "<p>(No photos to display for " . date("Y", mktime(12, 0, 0, 1, 1, $year)) . ")</p>\n";
}
footer($images_printed, $previous, $next, $up, $down);
}
// Print images for all years
// ------------------------------------------------------------
function print_all_years() {
debug("<br/>print_all_years()");
global $monthly_day;
global $monthly_hour;
global $mini_image_width;
global $mini_image_height;
$start_year = 2015;
$this_year = date('Y');
$monthly_days = [1, 7, 14, 21, 28]; // Only show these days in each month.
$previous = $next = $up = $down = false;
page_header("Lillevik Lofoten webcam: $start_year" . "-" . "$this_year", $previous, $next, $up, $down);
echo "<p>Displaying images for $monthly_hour:00 on the $monthly_day" . "th for each month for all year.</p>\n";
echo "<p>\n<a href=\"?type=day&date=" . date('Ymd') . "\">Today: " . date("M d") . "</a> \n";
echo "<a href=\"?type=last\">Latest image</a>.\n";
echo "</p>\n\n";
for ($year = $start_year; $year <= $this_year; $year++) {
print "\n<h2>$year</h2>\n\n";
// Loop through all months 1-12 (again) and print images for the $days if they exist.
$days = range(1, 31);
$images_printed = 0;
$yyyymmddhhmmss = "";
$image_filename = "";
// Loop through all months for this year.
// CSS overlay
echo "<div class=\"grid-container\">";
for ($month = 1; $month <= 12; $month++) {
$month = sprintf("%02d", $month);
// Loop through all $monthly_days for this month.
foreach($monthly_days as $monthly_day) {
// Find first image for the $monthly_day taken after $monthly_hour
debug("find_first_image_after_time($year, $month, $monthly_day, $monthly_hour, 0, 0);");
$yyyymmddhhmmss = find_first_image_after_time($year, $month, $monthly_day, $monthly_hour, 0, 0);
if ($yyyymmddhhmmss) {
// There was an image.
$hour = substr($yyyymmddhhmmss, 8, 2);
$minute = substr($yyyymmddhhmmss, 10, 2);
// Print mini images (never large images for full years), link to all images for that day.
// CSS overlay
echo "<div class=\"grid-item\">";
echo "<a href=\"?type=one&image=$yyyymmddhhmmss\">";
echo "<img alt=\"Lillevik Lofoten webcam: $year-$month--$monthly_day $hour:$minute\" ";
//echo "title=\"$year-$month-$monthly_day $hour:$minute\" ";
echo "width=\"$mini_image_width\" height=\"$mini_image_height\" ";
echo "src=\"$year/$month/$monthly_day/";
// If the mini version has been created: Use that. If not: Scale down the full version.
if (file_exists("$year/$month/$monthly_dayy/mini/$yyyymmddhhmmss.jpg")) {
echo "mini/";
}
echo "$yyyymmddhhmmss.jpg\"></a>\n";
// CSS overlay
if ($size == "mini" || empty($size)) {
echo "<span class=\"time\">$year-$month-$day</span>";
}
echo "</div>";
$images_printed += 1;
}
}
}
// CSS overlay
echo "</div>\n";
if ($images_printed == 0) {
echo "(No photos to display for $year)\n";
}
echo "</p>\n\n";
}
footer($images_printed, $previous, $next, $up, $down);
}
// Print links to mini and large images
// ------------------------------------------------------------
function print_mini_large_links($timestamp, $size) {
$date = date('Ymd', $timestamp);
echo "<p>\n";
if ($size == "large") { // Link to mini if we showed large, or don't know.
echo "<a href=\"?type=day&date=$date&size=mini\">Mini photos</a>. ";
}
if ($size == "mini" || empty($size)) { // Links to large if we showed mini, or don't know.
echo "<a href=\"?type=day&date=$date&size=large\">Large photos</a>. ";
}
echo "</p>\n\n";
}
// Returns only the date part of an image filename (removes directory and ".jpg").
// ------------------------------------------------------------
function get_yyyymmddhhmmss($fullPath) {
// Input: 2023/11/14/20231114144049.jpg
// Output: 20231114144049
return preg_replace("/[^0-9]/", "", pathinfo(basename($fullPath), PATHINFO_FILENAME));
}
// Finds the latest "*jpg" file in today's directory. Returns only date part of filename.
// ------------------------------------------------------------
function find_latest_image() {
// Find newest directory with the right name format
list($year, $month, $day) = explode('-', date('Y-m-d'));
if (is_dir("$year/$month/$day")) {
debug("NORMAL: max(glob(\"$year/$month/$day/*.jpg\", GLOB_BRACE))");
$latest_image = max(glob("$year/$month/$day/*.jpg", GLOB_BRACE));
} else if (is_dir("$year/$month")) {
debug("MONTH: max(glob(\"$year/$month/*.jpg\", GLOB_BRACE))");
$latest_image = max(glob("$year/$month/**/*.jpg", GLOB_BRACE));
} else if (is_dir("$year/$month")) {
debug("YEAR: max(glob(\"$year/$month/*.jpg\", GLOB_BRACE))");
$latest_image = max(glob("$year/**/*.jpg", GLOB_BRACE));
}
$image = get_yyyymmddhhmmss($latest_image);
debug("FOUND: image (datepart): $image");
// Now we have: 2015120209401200
return $image;
}
// Finds the first day with images for a specific year and month. Returns only date part of filename.
// ------------------------------------------------------------
function find_first_day_with_images($year, $month) {
// Find newest directory with the right name format
debug("<br/>find_first_day_with_images($year, $month)");
$directories = glob("$year$month*"); // Get the first first. 2* works until the year 3000.
$directory = $directories[0]; // This is the first one in that month.
debug("First day with images: $directory");
return $directory;
}
// Finds the first year with images.
// ------------------------------------------------------------
function find_first_year_with_images() {
debug("<br/>find_first_year_with_images()");
return 2015; // Hah!
}
// Gets all images in the directory for a specific day (YYYYMMDD: 20231114).
// ------------------------------------------------------------
function get_all_images_in_directory($directory) {
$images = glob("$directory/*.jpg");
debug("<br/>get_all_images_in_directory($directory/*.jpg): " . count($images) . " images found.");
return $images;
}
// Gets all images in the directory for a specific day and hour.
// ------------------------------------------------------------
function get_latest_image_in_directory_by_date_hour($directory, $hour) {
// $date = 2023/11/14
$date = preg_replace("/[^0-9]/", "", $directory);
$images = glob("$directory/$date$hour*.jpg");
debug("<br/>get_latest_image_in_directory_by_date_hour($directory, $hour)<br/>Found " . count($images) . "images, returning " . $images[0]);
return $images[0];
}
// Find the first image after a given time. Used when going to the first image in a day.
// ------------------------------------------------------------
// function find_first_image_after_time_old($year, $month, $day, $hour, $minute, $seconds) {
// if ($minute < 10) {
// $minute = sprintf("%02d", $minute);
// }
// if ($seconds < 10) {
// $seconds = sprintf("%02d", $seconds);
// }
// debug("<br/>find_first_image_after_time($year, $month, $day, $hour, $minute, $seconds)");
// // Find all images for the specified date and hour.
// $image = "";
// $images = glob("$year/$month/$day/$year$month$day$hour*");
// debug("Looking in directory: $year/$month/$day/$year$month$day$hour*");
// if (!empty($images)) {
// $image = $images[0];
// $image = get_yyyymmddhhmmss($image);
// } else {
// debug("No images found in directory: $year/$month/$day/$year$month$day$hour*");
// }
// return $image;
// }
function find_first_image_after_time($year, $month, $day, $hour, $minute, $seconds) {
$minute = sprintf("%02d", $minute);
$seconds = sprintf("%02d", $seconds);
debug("<br/>find_first_image_after_time($year, $month, $day, $hour, $minute, $seconds)");
// Find all images for the specified date and hour.
$image = "";
$imagePattern = sprintf("%s/%s/%s/%s%s%s%s*", $year, $month, $day, $year, $month, $day, $hour);
debug("Looking in directory $year/$month/$day with pattern: $imagePattern");
$images = glob($imagePattern);
if (!empty($images)) {
$image = get_yyyymmddhhmmss($images[0]);
debug("image found: $image");
} else {
debug("No images found in directory: $imagePattern");
}
return $image;
}
// Print a single image, specified by the date part of the filename (no .jpg suffix, no path)
// ------------------------------------------------------------
function print_single_image($image_filename, $last_image) {
// $image_filename example: "20231114144049"
global $large_image_width;
global $large_image_height;
// Find the date and time for the image.
debug("split_image_filename($image_filename)");
list($year, $month, $day, $hour, $minute, $seconds) = split_image_filename($image_filename);
// Make a timestamp for the image's date and time.
debug(" mktime($hour, $minute, 0, $month, $day, $year)");
$timestamp = mktime($hour, $minute, 0, $month, $day, $year); // Using 0 for minutes to get the one(s) before too.
// Calculate the sun times for the image's timestamp.
list($sunrise, $sunset, $dawn, $dusk, $midnight_sun, $polar_night) = find_sun_times($timestamp);
// Get previous and next image: First get all images for the same day as the images passed as parameter.
$directory = "$year/$month/$day";
// Loop through all images to find previous and next based on the $image_filename's day's directory.
$images = get_all_images_in_directory($directory);
$previous_image = "";
$next_image = "";
$number_of_images = count($images); // Avoid count() in every iteration below.
$i = 0;
foreach($images as $image) {
if (strpos($images[$i], $image_filename) !== false) { // Faster than preg_match().
// We found a match for $image_filename, now get the previous and next images.
$image_filename = get_yyyymmddhhmmss($images[$i]) . ".jpg";
if ($i != 0) {
// This was not the first image in the array, so we can get the previous one.
$previous_image = $images[$i - 1];
}
if ($i != $number_of_images) {
// This was not the Latest image in the array, so we can get the next one.
$next_image = $images[$i + 1];
}
break;
}
$i += 1;
}
// Links to previous, next, up, down.
debug("previous_image: $previous_image<br/>next_image: $next_image<br/>");
if ($previous_image) {
$previous_image_datepart = get_yyyymmddhhmmss($previous_image);
$previous = "?type=one&image=$previous_image_datepart"; // Only date for the link.
}
if ($next_image) {
$next_image_datepart = get_yyyymmddhhmmss($next_image);
$next = "?type=one&image=$next_image_datepart"; // Only date for the link.
}
$up_image_datepart = get_yyyymmddhhmmss($image_filename);
$up = "?type=day&date=$up_image_datepart"; // The full day.
$down = false; // Already showing a single image, not possible to go lower.
debug("PREV: $previous<br/>NEXT: $next<br/>UP: $up<br/>DOWN: $down<br/>");
// Print!
$title = "Lillevik Lofoten webcam";
if (! $last_image) {
$title .= ": " . date("Y-m-d H:i", $timestamp);
}
page_header($title, $previous, $next, $up, $down);
print_sunrise_sunset_info($sunrise, $sunset, $dawn, $dusk, $midnight_sun, $polar_night, false);
print_full_day_link($timestamp);
if ($previous_datepart || $next_datepart) {
echo "<p>";
if ($previous_datepart) {
echo "<a href=\"$previous\">Previous: " . substr($previous_datepart, 8, 2) . ":" . substr($previous_datepart, 10, 2) . "</a>.\n";
}
if ($next_datepart) {
echo "<a href=\"$next\">Next: " . substr($next_datepart, 8, 2) . ":" . substr($next_datepart, 10, 2) . "</a>.\n";
}
echo "</p>\n\n";
}
echo "<p>\n";
echo "<a href=\"?type=day&date=$year$month$day\">";
echo "<img alt=\"Lillevik Lofoten webcam: $year-$month-$day $hour:$minute\" ";
//echo "title=\"$year-$month-$day $hour:$minute\" ";
echo "width=\"$large_image_width\" height=\"$large_image_height\" ";
echo "src=\"$year/$month/$day/$image_filename\">";
echo "</a>\n";
echo "</p>\n\n";
list($width, $height) = getimagesize("$year/$month/$day/$image_filename");
echo "<p>\n<a href=\"$year/$month/$day/$image_filename\">Full size ($width x $height)</a>\n</p>\n\n";
footer($images_printed, $previous, $next, $up, $down);
}
// Print details about the sun, and what images are shown.
// ------------------------------------------------------------
function print_sunrise_sunset_info($sunrise, $sunset, $dawn, $dusk, $midnight_sun, $polar_night, $include_interval) {
debug("<br/>print_sunrise_sunset_info($sunrise, $sunset, $dawn, $dusk, $midnight_sun, $polar_night, $include_interval)");
global $monthly_day;
echo "\n\n<p>";
if ($midnight_sun) {
echo "Midnight sun ☀";
} else if ($polar_night) {
echo "Polar night";
} else {
echo "Sunrise: " . date('H:i', $sunrise) . ". Sunset: " . date('H:i', $sunset);
}
if ($include_interval == "day") {
echo ". Displaying photos taken between " . date('H:i', $dawn) . " and " . date('H:i', $dusk);
} else if ($include_interval == "average") {
echo " (calculated for " . date('F', $dawn) . " $monthly_day)";
//echo " with the newest images first";
}
echo ".</p>\n\n";
}
// Find the previous and next month, even for January and December.
// ------------------------------------------------------------
function find_previous_and_next_month($year, $month) {
$month_previous = ($month == 1) ? 12 : sprintf("%02d", $month - 1);
$year_previous = ($month == 1) ? sprintf("%4d", $year - 1) : $year;
$month_next = ($month == 12) ? "01" : sprintf("%02d", $month + 1);
$year_next = ($month == 12) ? sprintf("%4d", $year + 1) : $year;
debug("<br/>find_previous_and_next_month($year, $month)<br/>year_previous: $year_previous<br/>month_previous: $month_previous<br/>year_next: $year_next<br/>month_next: $month_next");
return array($year_previous, $month_previous, $year_next, $month_next);
}
// Links to previsou and next year.
// ------------------------------------------------------------
function print_previous_next_year_links($year) {
echo "<p><a href=\"?type=year&year=" . ($year - 1) . "\">Previous (" . ($year - 1) . ")</a>.\n";
if ($year < date('Y')) {
echo "<a href=\"?type=year&year=" . ($year + 1) . "\">Next (" . ($year + 1) . ")</a>.\n";
}
echo "<p>\n";
}
// Links to yesterday and (possibly) tomorrow.
// ------------------------------------------------------------
function print_yesterday_tomorrow_links($timestamp, $is_full_month) {
global $size;
if ($is_full_month) {
// No links to yesterday and tomorrow, but the the previous and next months. Easy.
list($year_previous, $month_previous, $year_next, $month_next) = find_previous_and_next_month(date('Y', $timestamp), date('m', $timestamp));
echo "<p><a href=\"?type=month&year=$year_previous&month=$month_previous\">Previous: " . date("F", mktime(0, 0, 0, $month_previous, 1, $year_previous)) . "</a>. \n";
echo "<a href=\"?type=month&year=$year_next&month=$month_next\">Next: " . date("F", mktime(0, 0, 0, $month_next, 1, $year_previous)) . "</a>. \n";
$requested_month = date('Y-m', $timestamp);
$this_month = date('Y-m'); // 2023-11
$previous_month = date('Y-m', time() - 60 * 60 * 24 * 30); // 2023-10
if ($requested_month != $this_month) {
echo "<a href=\"?type=month&year=" . date('Y') . "&month=" . date('m') . "\">Now: " . date("F") . "</a>. \n";
}
echo "<a href=\"?type=day&date=" . date('Ymd') . "\">Today: " . date("F d") . "</a>, \n";
echo "<a href=\"?type=year&year=" . date('Y', $timestamp) . "\">Entire " . date('Y', $timestamp) . "</a>.\n";
//echo "<a href=\"?type=last\">Latest image</a>.\n";
} else {
// Not showing a full month: Work hard to find the days.
// Previous: Yesterday always exists.
$yesterday_timestamp = $timestamp - 60 * 60 * 24;
echo "<p>\n<a href=\"?type=day&date=" . date('Ymd', $yesterday_timestamp) . "&size=$size\">Previous: " . date("F d", $yesterday_timestamp). "</a>.\n";
// Next: Is there a tomorrow, based on the selected day?
if (date('Y-m-d', $timestamp) == date('Y-m-d')) {
// The $timestamp is today, so there is no tomorrow.
} else {
$tomorrow_timestamp = $timestamp + 60 * 60 * 24; // Add 24 hours for the "Next" link.
echo "<a href=\"?type=day&date=" . date('Ymd', $tomorrow_timestamp) . "\">Next: " . date("F d", $tomorrow_timestamp) . "</a>.\n";
}
// Today: Only if this is the day before yesterday, or earlier.
if (date('Y-m-d', $timestamp) <= date('Y-m-d', strtotime('-2 day'))) {
echo "<a href=\"?type=day&date=" . date('Ymd') . "\">Today: " . date("F d") . "</a>, \n";
}
//echo "<a href=\"?type=last\">Latest image</a>.\n";
// Link to the full month and year - and everything.
//------------------------------------------------------------
echo "<a href=\"?type=month&year=" . date('Y', $timestamp) . "&month=" . date('m', $timestamp) . "\">Entire " . date("F", $timestamp) . "</a>.\n";
echo "<a href=\"?type=year&year=" . date('Y', $timestamp) . "\">Entire " . date('Y', $timestamp) . "</a>.\n";
}
echo "<a href=\"?type=last\">Latest image</a>.\n";
echo "</p>\n\n";
}
// Print link to all images for the day specified with a timestamp.
// ------------------------------------------------------------
function print_full_day_link($timestamp) {
echo "<p>";
echo "<a href=\"?type=day&date=" . date('Ymd', $timestamp) . "\">The whole day</a>.\n";
echo "<a href=\"?type=month&year=" . date('Y', $timestamp) . "&month=" . date('m', $timestamp) . "\">Entire " . date("F", $timestamp) . "</a>.\n";
echo "<a href=\"?type=year&year=" . date('Y', $timestamp) . "\">Entire " . date('Y', $timestamp) . "</a>.\n";
echo "</p>\n\n";
}
// Rename files in case there are new ones that have not been handles by cron yet.
// ------------------------------------------------------------
function check_and_rename_files_hack($filename_prefix) {
list($year, $month, $day) = explode('-', date('Y-m-d'));
debug("glob(\"$year/$month/$day/$filename_prefix*\")");
$images = glob("$year/$month/$day/$filename_prefix*");
debug("Found " . count($images) . " images to rename.");
foreach ($images as $image_to_rename) {
$new_name = str_replace($filename_prefix, '', $image_to_rename);
debug("rename($image_to_rename, $new_name)");
rename($image_to_rename, $new_name);
}
}
// Dawn and dusk rounding to get more of the sub-horizon sunlight.
// ------------------------------------------------------------
function roundDawnAndDusk($dawn, $dusk) {
$dawn_adjust = 1; // Add hour(s) of dawn.
$dusk_adjust = 1; // Add hour(s) of dusk.
$dawn -= $dawn_adjust * 60 * 60;
$roundedDawn = floor($dawn / 3600) * 3600; // Round down to nearest hour.
$dusk += $dusk_adjust * 60 * 60;
$roundedDusk = ceil($dusk / 3600) * 3600; // Round up to nearest hour.
return [$roundedDawn, $roundedDusk];
}
// Print one day: All images in a directory, between dawn and dusk, with mini/large size, optionally limited by a number.
// ------------------------------------------------------------
function print_full_day($timestamp, $image_size, $number_of_images) {
global $size;