22
33namespace VanOns \LaravelAttachmentLibrary \Glide ;
44
5+ use Exception ;
56use Illuminate \Contracts \Filesystem \Filesystem ;
67use Illuminate \Support \Facades \Storage ;
78use League \Glide \Responses \SymfonyResponseFactory ;
@@ -13,7 +14,7 @@ class GlideManager
1314 public function server (): Server
1415 {
1516 return ServerFactory::create ([
16- 'driver ' => config ( ' glide. driver' ),
17+ 'driver ' => $ this -> driver ( ),
1718 'source ' => config ('glide.source ' ),
1819 'cache ' => $ this ->cacheDisk ()->getDriver (),
1920 'defaults ' => config ('glide.defaults ' ),
@@ -26,6 +27,11 @@ public function server(): Server
2627 ]);
2728 }
2829
30+ public function driver (): string
31+ {
32+ return config ('glide.driver ' , 'gd ' );
33+ }
34+
2935 public function cacheDisk (): Filesystem
3036 {
3137 return is_string (config ('glide.cache_disk ' ))
@@ -63,14 +69,91 @@ public function cacheSize(): int
6369
6470 public function cacheSizeHumanReadable (): string
6571 {
66-
6772 return $ this ->humanReadableSize ($ this ->cacheSize ());
6873 }
6974
7075 public function humanReadableSize (int $ bytes , $ decimals = 2 ): string
7176 {
7277 $ size = ['B ' , 'KB ' , 'MB ' , 'GB ' , 'TB ' , 'PB ' ];
73- $ factor = floor ((strlen (strval ($ bytes )) - 1 ) / 3 );
74- return sprintf ("%. {$ decimals }f " , $ bytes / pow (1024 , $ factor )) . ' ' . $ size [$ factor ];
78+ $ factor = floor ((strlen ((string ) $ bytes ) - 1 ) / 3 );
79+ return sprintf ("%. {$ decimals }f " , $ bytes / (1024 ** $ factor )) . ' ' . $ size [$ factor ];
80+ }
81+
82+ public function imageIsSupported (string $ path , array $ params = []): bool
83+ {
84+ try {
85+ $ this ->server ()->makeImage ($ path , $ params );
86+ return true ;
87+ } catch (Exception ) {
88+ return false ;
89+ }
90+ }
91+
92+ /**
93+ * Retrieve supported image formats for the current driver.
94+ *
95+ * @param bool $onlyCommon Limit to results to the most common formats.
96+ */
97+ public function getSupportedImageFormats (bool $ onlyCommon = true ): array
98+ {
99+ $ commonFormats = [
100+ 'AVIF ' ,
101+ 'BMP ' ,
102+ 'GIF ' ,
103+ 'HEIC ' ,
104+ 'HEIF ' ,
105+ 'ICO ' ,
106+ 'JPEG ' ,
107+ 'JPG ' ,
108+ 'PNG ' ,
109+ 'SVG ' ,
110+ 'TIFF ' ,
111+ 'WEBP ' ,
112+ ];
113+
114+ $ driver = $ this ->driver ();
115+
116+ if ($ driver === 'gd ' && function_exists ('gd_info ' )) {
117+ $ formats = gd_info ();
118+
119+ $ supported = collect ();
120+ foreach ($ formats as $ key => $ value ) {
121+ if ($ value === false ) {
122+ continue ;
123+ }
124+
125+ if ($ onlyCommon ) {
126+ foreach ($ commonFormats as $ format ) {
127+ if (str_contains ($ key , $ format )) {
128+ $ supported ->push ($ format );
129+ break ;
130+ }
131+ }
132+ } else {
133+ $ format = strtoupper (str_replace ([' ' , 'Support ' ], '' , $ key ));
134+ $ supported ->push ($ format );
135+ }
136+ }
137+
138+ return $ supported
139+ ->unique ()
140+ ->values ()
141+ ->sort ()
142+ ->all ();
143+ }
144+
145+ if ($ driver === 'imagick ' && class_exists (\Imagick::class)) {
146+ $ formats = \Imagick::queryFormats ();
147+
148+ return collect ()
149+ ->when ($ onlyCommon , fn ($ collection ) => $ collection ->merge ($ commonFormats )->filter (fn ($ format ) => in_array ($ format , $ formats )))
150+ ->when (!$ onlyCommon , fn ($ collection ) => $ collection ->merge ($ formats ))
151+ ->unique ()
152+ ->values ()
153+ ->sort ()
154+ ->all ();
155+ }
156+
157+ return [];
75158 }
76159}
0 commit comments