Skip to content

Commit 99a218e

Browse files
committed
feat(glide): add function to return supported formats
1 parent b90d503 commit 99a218e

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

src/Glide/GlideManager.php

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class GlideManager
1414
public function server(): Server
1515
{
1616
return ServerFactory::create([
17-
'driver' => config('glide.driver'),
17+
'driver' => $this->driver(),
1818
'source' => config('glide.source'),
1919
'cache' => $this->cacheDisk()->getDriver(),
2020
'defaults' => config('glide.defaults'),
@@ -27,6 +27,11 @@ public function server(): Server
2727
]);
2828
}
2929

30+
public function driver(): string
31+
{
32+
return config('glide.driver', 'gd');
33+
}
34+
3035
public function cacheDisk(): Filesystem
3136
{
3237
return is_string(config('glide.cache_disk'))
@@ -83,4 +88,72 @@ public function imageIsSupported(string $path, array $params = []): bool
8388
return false;
8489
}
8590
}
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 [];
158+
}
86159
}

0 commit comments

Comments
 (0)