-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BuildPhotoStream.php
102 lines (82 loc) · 3.49 KB
/
BuildPhotoStream.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
<?php
use Carbon\Carbon;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
use TightenCo\Jigsaw\Jigsaw;
class BuildPhotoStream
{
public function handle(Jigsaw $jigsaw): void
{
// Clear out existing photo markdown files...
$this->clearExistingFiles('./source/_photos', '.md');
// Clear out existing thumbnails and large processed images...
$this->clearExistingFiles('./source/assets/photos/large', '.jpg');
$this->clearExistingFiles('./source/assets/photos/thumbnail', '.jpg');
// Collect new photos...
$filesystem = new Filesystem();
$files = $filesystem->allFiles('./source/assets/photos/original');
collect($files)->filter(function($file){
return Str::endsWith($file, '.jpg');
})->each(function(SplFileInfo $file, $index) use ($jigsaw) {
$filename = str_replace('--', '-', strtolower(str_replace('_', '-', $file->getBasename('.' . $file->getExtension()))));
$image = imagecreatefromjpeg($file->getRealPath());
// Work out average colour of images to create tint...
$tint = (new Tooleks\Php\AvgColorPicker\Gd\AvgColorPicker)->getImageAvgHexByPath($file->getRealPath());
// Resize images for efficiency...
$thumbnailSize = $jigsaw->getConfig('images.thumbnail.size');
$thumbnail = imagescale($image, $thumbnailSize);
imagejpeg($thumbnail, $thumbnailFileName = sprintf('source/assets/photos/thumbnail/%s.jpg', $filename), 80);
$largeImageSize = $jigsaw->getConfig('images.large.size');
$large = imagescale($image, $largeImageSize);
imagejpeg($large, $largeFileName = sprintf('source/assets/photos/large/%s.jpg', $filename));
list($width, $height) = getimagesize($thumbnailFileName);
// Bad exif data will throw a warning but not an exception...
$exif = @exif_read_data($file->getRealPath(), 'FILE');
if(!isset($exif['DateTimeOriginal'])) {
$exif = [
'DateTimeOriginal' => $exif['FileDateTime'] ?? Carbon::today(),
];
}
$thumbnailFileName = str_replace('source/', '', $thumbnailFileName);
$largeFileName = str_replace('source/', '', $largeFileName);
$name = str_replace(['-'], ' ', $filename);
// Create markdown file contents...
$contents = sprintf('---
id: %s
photo: "%s"
thumbnail: "%s"
tint: "%s"
filename: "%s"
name: "%s"
height: "%s"
width: "%s"
date: "%s"
---
',
($index +1),
$largeFileName,
$thumbnailFileName,
$tint,
$filename,
$name,
$height,
$width,
Carbon::parse($exif['DateTimeOriginal'])
);
$jigsaw->writeSourceFile(sprintf('./_photos/%s.md', $filename), $contents);
});
}
private function clearExistingFiles(string $path, string $fileType) : void
{
$filesystem = new Filesystem();
if(!$filesystem->exists($path)){
$filesystem->makeDirectory($path);
}
$existingFiles = collect($filesystem->allFiles($path))->filter(function (SplFileInfo $file) use($fileType) {
return Str::endsWith($fileType, $file->getExtension());
})->map(function (SplFileInfo $file) {
return $file->getRealPath();
});
$filesystem->delete($existingFiles->toArray());
}
}