-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertiPhotoExport.php
65 lines (57 loc) · 1.75 KB
/
convertiPhotoExport.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
<?php
/**
* @copyright 2021 City of Bloomington, Indiana
* @license https://www.gnu.org/licenses/agpl.txt GNU/AGPL, see LICENSE
*/
declare (strict_types=1);
function iterate(string $dir)
{
$d = parseDate(basename($dir));
$dirdate = $d
? \DateTime::createFromFormat('F j, Y', $d)
: null;
foreach (scandir($dir) as $f) {
if (substr($f, 0, 1) != '.') {
$path = "$dir/$f";
$ext = strtolower(substr($f, -3));
if (is_dir($path)) {
iterate($path);
}
if (is_file($path)) {
echo "$path\n";
$exif = ($ext == 'jpg')
? exif_read_data($path)
: [];
$date = extractDatetime($exif, $dirdate ? $dirdate : null);
$out_dir = "./output/{$date->format('Y/m/d')}";
if (!is_dir($out_dir)) { mkdir($out_dir, 0777, true); }
copy($path, "$out_dir/$f");
}
}
}
}
function extractDatetime(array $exif, ?DateTime $fallback=null): \DateTime
{
$possibleKeys = ['DateTime', 'Date and Time', 'FileDateTime'];
foreach ($possibleKeys as $k) {
if (!empty($exif[$k])) {
$d = is_numeric($exif[$k])
? \DateTime::createFromFormat('U', (string)$exif[$k])
: new \DateTime($exif[$k]);
if ($d) { return $d; }
}
}
if ($fallback) {
return $fallback;
}
print_r($exif);
exit();
throw new \Exception('missingDateTime');
}
function parseDate(string $string): ?string
{
$matches = [];
preg_match('/[a-zA-Z]+\s\d{1,2},\s\d{4}/', $string, $matches);
return $matches[0] ?? null;
}
iterate('./export');