Skip to content

Commit 993d91a

Browse files
committed
simpler image optimisation
1 parent 788ca7c commit 993d91a

File tree

3 files changed

+10
-20
lines changed

3 files changed

+10
-20
lines changed

app/Actions/OptimizeWebpImageAction.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,10 @@
88

99
class OptimizeWebpImageAction
1010
{
11-
public function handleFromUploadedFile(UploadedFile $file): array
12-
{
13-
return $this->optimize($file->getRealPath());
14-
}
15-
16-
public function handleFromPath(string $path): array
17-
{
18-
return $this->optimize($path);
19-
}
20-
21-
protected function optimize(string $path): array
11+
public function handle(string $input): array
2212
{
2313
// Image optimization
24-
$image = Image::read($path);
14+
$image = Image::read($input);
2515

2616
// Scale down only
2717
if ($image->width() > 1000) {

app/Http/Controllers/PuppyController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function store(Request $request)
6464
$image_url = null;
6565
if ($request->hasFile('image')) {
6666

67-
$optimized = (new OptimizeWebpImageAction())->handleFromUploadedFile($request->file('image'));
67+
$optimized = (new OptimizeWebpImageAction())->handle($request->file('image'));
6868

6969
$path = 'puppies/' . $optimized['fileName'];
7070

database/seeders/PuppySeeder.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,16 @@ public function run(): void
141141
$optimizer = new OptimizeWebpImageAction();
142142

143143
foreach ($puppies as $puppy) {
144-
$storagePath = storage_path('app/public/puppies/' . $puppy['image']);
145-
$optimized = $optimizer->handleFromPath($storagePath);
146144

145+
// Optimize the image
146+
$input = storage_path('app/public/puppies/' . $puppy['image']);
147+
$optimized = $optimizer->handle($input);
148+
149+
// Grab the path of the image
147150
$path = 'puppies/' . $optimized['fileName'];
148-
$stored = Storage::disk('public')->put($path, $optimized['webpString']);
149151

150-
if (!$stored) {
151-
dump('Failed to store image: ' . $puppy['image']);
152-
continue;
153-
}
152+
// Store that image
153+
Storage::disk('public')->put($path, $optimized['webpString']);
154154

155155
Puppy::create([
156156
'user_id' => $simon->id,

0 commit comments

Comments
 (0)