Skip to content

Commit 72048a3

Browse files
committed
wip
1 parent c199aad commit 72048a3

1 file changed

Lines changed: 24 additions & 62 deletions

File tree

app/Console/Commands/Images/GenerateOgImageCommand.php

Lines changed: 24 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,21 @@ protected function handleAutomatic(): int
6868
$type = $this->option('type') ?? 'all';
6969
$force = (bool) $this->option('force');
7070

71-
$generated = 0;
72-
$skipped = 0;
71+
$this->info('Using '.($this->driver() instanceof ImagickDriver ? 'Imagick' : 'GD').' driver');
7372

74-
if (in_array($type, ['blog', 'all'])) {
75-
$this->info('Processing blog posts...');
76-
[$blogGenerated, $blogSkipped] = $this->generateBlogImages($force);
77-
$generated += $blogGenerated;
78-
$skipped += $blogSkipped;
79-
}
73+
$types = collect([
74+
'blog' => fn () => $this->generateImagesFor('blog', Post::all(), $force),
75+
'speaking' => fn () => $this->generateImagesFor('speaking', Speaking::all(), $force),
76+
]);
8077

81-
if (in_array($type, ['speaking', 'all'])) {
82-
$this->info('Processing speaking pages...');
83-
[$speakingGenerated, $speakingSkipped] = $this->generateSpeakingImages($force);
84-
$generated += $speakingGenerated;
85-
$skipped += $speakingSkipped;
86-
}
78+
[$generated, $skipped] = $types
79+
->when($type !== 'all', fn ($c) => $c->only($type))
80+
->map(function ($generator, $name) {
81+
$this->info("Processing {$name}...");
82+
83+
return $generator();
84+
})
85+
->reduce(fn ($carry, $counts) => [$carry[0] + $counts[0], $carry[1] + $counts[1]], [0, 0]);
8786

8887
$this->newLine();
8988
$this->info("Generated: {$generated} image(s)");
@@ -93,61 +92,30 @@ protected function handleAutomatic(): int
9392
}
9493

9594
/**
95+
* @param iterable<int, Post|Speaking> $items
9696
* @return array{int, int}
9797
*/
98-
protected function generateBlogImages(bool $force = false): array
99-
{
100-
$posts = Post::all();
101-
$generated = 0;
102-
$skipped = 0;
103-
104-
foreach ($posts as $post) {
105-
$path = public_path("images/share/og/blog/{$post->slug}.png");
106-
107-
if (! $force && file_exists($path)) {
108-
$this->comment(" Skipping blog/{$post->slug}.png (already exists)");
109-
$skipped++;
110-
111-
continue;
112-
}
113-
114-
try {
115-
$this->generateImage($post->slug, $post->title, 'blog');
116-
$this->info(" Generated blog/{$post->slug}.png");
117-
$generated++;
118-
} catch (Exception $e) {
119-
$this->error(" Failed to generate blog/{$post->slug}.png: ".$e->getMessage());
120-
}
121-
}
122-
123-
return [$generated, $skipped];
124-
}
125-
126-
/**
127-
* @return array{int, int}
128-
*/
129-
protected function generateSpeakingImages(bool $force = false): array
98+
protected function generateImagesFor(string $type, iterable $items, bool $force): array
13099
{
131-
$speaking = Speaking::all();
132100
$generated = 0;
133101
$skipped = 0;
134102

135-
foreach ($speaking as $item) {
136-
$path = public_path("images/share/og/speaking/{$item->slug}.png");
103+
foreach ($items as $item) {
104+
$path = public_path("images/share/og/{$type}/{$item->slug}.png");
137105

138106
if (! $force && file_exists($path)) {
139-
$this->comment(" Skipping speaking/{$item->slug}.png (already exists)");
107+
$this->comment(" Skipping {$type}/{$item->slug}.png (already exists)");
140108
$skipped++;
141109

142110
continue;
143111
}
144112

145113
try {
146-
$this->generateImage($item->slug, $item->title, 'speaking');
147-
$this->info(" Generated speaking/{$item->slug}.png");
114+
$this->generateImage($item->slug, $item->title, $type);
115+
$this->info(" Generated {$type}/{$item->slug}.png");
148116
$generated++;
149117
} catch (Exception $e) {
150-
$this->error(" Failed to generate speaking/{$item->slug}.png: ".$e->getMessage());
118+
$this->error(" Failed to generate {$type}/{$item->slug}.png: ".$e->getMessage());
151119
}
152120
}
153121

@@ -188,15 +156,9 @@ protected function generateImage(string $name, string $title, ?string $type = nu
188156

189157
protected function driver(): GdDriver|ImagickDriver
190158
{
191-
if (extension_loaded('imagick')) {
192-
$this->info('Using Imagick driver');
193-
194-
return new ImagickDriver;
195-
}
196-
197-
$this->info('Using GD driver');
198-
199-
return new GdDriver;
159+
return extension_loaded('imagick')
160+
? new ImagickDriver
161+
: new GdDriver;
200162
}
201163

202164
/**

0 commit comments

Comments
 (0)