33
44namespace Imahmood \FileStorage ;
55
6+ use Illuminate \Support \Facades \Storage ;
67use Imahmood \FileStorage \Config \Configuration ;
78use Imahmood \FileStorage \Exceptions \NotWritableException ;
89use Imahmood \FileStorage \Models \Media ;
@@ -27,8 +28,8 @@ public function optimizeImage(Media $media): void
2728 }
2829
2930 $ this ->resizeImage (
30- sourceFile : $ media-> original_absolute_path ,
31- targetFile: $ media ->original_absolute_path ,
31+ media : $ media ,
32+ targetFile: $ media ->original_relative_path ,
3233 maxDimension: $ this ->config ->maxDimension ,
3334 );
3435 }
@@ -47,8 +48,8 @@ public function generatePreview(Media $media): void
4748 $ previewName = $ this ->generatePreviewName ($ media ->file_name );
4849
4950 $ this ->resizeImage (
50- sourceFile : $ media-> original_absolute_path ,
51- targetFile: $ media ->dir_absolute_path .$ previewName ,
51+ media : $ media ,
52+ targetFile: $ media ->dir_relative_path .$ previewName ,
5253 maxDimension: $ this ->config ->previewDimension ,
5354 );
5455
@@ -67,17 +68,19 @@ protected function generatePreviewName(string $fileName): string
6768 * @throws \Imahmood\FileStorage\Exceptions\NotWritableException
6869 * @throws \Jcupitt\Vips\Exception
6970 */
70- protected function resizeImage (string $ sourceFile , string $ targetFile , int $ maxDimension ): void
71+ protected function resizeImage (Media $ media , string $ targetFile , int $ maxDimension ): void
7172 {
72- $ image = VipsImage::newFromFile ($ sourceFile );
73+ $ image = VipsImage::newFromBuffer (
74+ Storage::disk ($ media ->disk )->get ($ media ->original_relative_path )
75+ );
7376
7477 $ width = min ($ image ->width , $ maxDimension );
7578 $ height = min ($ image ->height , $ maxDimension );
7679 $ fileExt = pathinfo ($ targetFile , PATHINFO_EXTENSION );
7780
78- $ saved = file_put_contents (
79- $ targetFile ,
80- $ image ->thumbnail_image ($ width , ['height ' => $ height ])->writeToBuffer ('. ' .$ fileExt )
81+ $ saved = Storage:: disk ( $ media -> disk )-> put (
82+ path: $ targetFile ,
83+ contents: $ image ->thumbnail_image ($ width , ['height ' => $ height ])->writeToBuffer ('. ' .$ fileExt ),
8184 );
8285
8386 $ image = null ;
0 commit comments