3
3
namespace Codepane \LaravelImageHandler ;
4
4
5
5
use Exception ;
6
+ use Illuminate \Support \Facades \File ;
6
7
use Illuminate \Support \Facades \Storage ;
7
8
use Intervention \Image \ImageManager ;
8
9
@@ -30,6 +31,8 @@ public function store(object $image, string $imgPath = null): string
30
31
/* Creating a file name for the original image. */
31
32
$ fileOriginalName = $ this ->makeFileName ($ imgPath , $ time , 'orig ' );
32
33
34
+ $ this ->makeDirectoryIfDoesNotExist ($ storageDisk , $ fileOriginalName );
35
+
33
36
$ this ->storeImg ($ storageDisk , $ fileOriginalName , file_get_contents ($ image ));
34
37
35
38
foreach ($ dimensions as $ key => $ dimension ) {
@@ -47,6 +50,24 @@ public function store(object $image, string $imgPath = null): string
47
50
return $ fileOriginalName ;
48
51
}
49
52
53
+ /**
54
+ * make a directory if does not exist
55
+ *
56
+ * @param string $storageDisk
57
+ * @param string $fileName
58
+ *
59
+ * @return void
60
+ */
61
+ public function makeDirectoryIfDoesNotExist (string $ storageDisk , string $ fileName )
62
+ {
63
+ $ directory = pathinfo ($ fileName , PATHINFO_DIRNAME );
64
+
65
+ if (($ storageDisk == 'local ' || $ storageDisk == 'public ' ) && !Storage::disk ($ storageDisk )->exists ($ directory )) {
66
+ $ dirPath = Storage::disk ($ storageDisk )->path ($ directory );
67
+ File::makeDirectory ($ dirPath , 0777 );
68
+ }
69
+ }
70
+
50
71
/**
51
72
* It stores an image in a specified disk.
52
73
*
0 commit comments