-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-photo-manifest.php
125 lines (91 loc) · 3.3 KB
/
generate-photo-manifest.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
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Thumbhash\Thumbhash;
use function Thumbhash\extract_size_and_pixels_with_imagick;
$json = json_decode(file_get_contents(__DIR__ . '/images.json'), true);
foreach ($json['images'] as $key => $image) {
if (!file_exists(__DIR__ . '/public/photos/' . $image['src'])) {
echo "File not found: /public/photos/{$image['src']}\n";
continue;
}
$file = new SplFileInfo(__DIR__ . '/public/photos/' . $image['src']);
[$width, $height] = getimagesize($file->getPathname());
if ($image['description'] !== "" && !str_ends_with($image['description'], '.')) {
$json['images'][$key]['description'] = $image['description'] . '.';
}
if (str_ends_with($image['title'], '.')) {
$json['images'][$key]['title'] = substr($image['title'], 0, -1);
}
$json['images'][$key]['ratio'] = getRatio($file);
$json['images'][$key]['orientation'] = $width > $height ? 'landscape' : 'portrait';
$json['images'][$key]['exif'] = exif($file);
$json['images'][$key]['hash'] = getImageHash($file);
if (!isset($image['albums'])) {
$json['images'][$key]['albums'] = [];
}
}
function getImageHash(SplFileInfo $file): string
{
$thumbContent = create_thumbnail($file);
[$width, $height, $pixels] = extract_size_and_pixels_with_imagick($thumbContent);
$hash = Thumbhash::RGBAToHash($width, $height, $pixels);
return implode(",", $hash);
}
function create_thumbnail(SplFileInfo $file): string
{
$image = new Imagick($file->getPathname());
$width = $image->getImageWidth();
$height = $image->getImageHeight();
$longSide = max($width, $height);
if ($longSide == $width) {
$newWidth = 99;
$newHeight = 0;
} else {
$newHeight = 99;
$newWidth = 0;
}
$image->scaleImage($newWidth, $newHeight);
return $image->__toString();
}
function getRatio($file): string
{
[$width, $height] = getimagesize($file->getPathname());
$divisor = gmp_intval(gmp_gcd($width, $height));
return ($width / $divisor) . ' / ' . ($height / $divisor);
}
function exif(SplFileInfo $file): array
{
$data = exif_read_data($file->getPathname(), 'EXIF', true);
$fNumber = explode("/", $data['EXIF']['FNumber']);
$actualFNumber = $fNumber[0] / $fNumber[1];
$fLength = explode("/", $data['EXIF']['FocalLength']);
$actualFLength = $fLength[0] / $fLength[1];
$date = DateTime::createFromFormat('Y:m:d H:i:s', $data['EXIF']['DateTimeOriginal']);
$model = implode(
", ",
array_filter(
[
mapMake($data['IFD0']['Make'] ?? null),
$data['IFD0']['Model'] ?? null
]
)
);
return [
'Aperture' => 'f/' . number_format($actualFNumber, 1),
'Exposure' => $data['EXIF']['ExposureTime'] ?? 'N/A',
'ISO' => $data['EXIF']['ISOSpeedRatings'] ?? 'N/A',
'Focal Length' => $actualFLength . ' mm',
'Date Taken' => $date->format('M j, Y'),
'Model' => $model
];
}
function mapMake(?string $make): ?string {
if (null === $make) {
return null;
}
$replaces = [
'NIKON CORPORATION' => 'Nikon'
];
return $replaces[$make] ?? $make;
}
file_put_contents(__DIR__ . '/images.json', json_encode($json, JSON_PRETTY_PRINT));