File tree Expand file tree Collapse file tree 3 files changed +10
-20
lines changed Expand file tree Collapse file tree 3 files changed +10
-20
lines changed Original file line number Diff line number Diff line change 88
99class 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 ) {
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 ,
You can’t perform that action at this time.
0 commit comments