-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathFileExtension.php
More file actions
146 lines (133 loc) Β· 3.81 KB
/
FileExtension.php
File metadata and controls
146 lines (133 loc) Β· 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
declare(strict_types=1);
namespace Intervention\Image;
use Error;
use Intervention\Image\Exceptions\InvalidArgumentException;
use Intervention\Image\Exceptions\NotSupportedException;
enum FileExtension: string
{
case JPG = 'jpg';
case JPEG = 'jpeg';
case PJPG = 'pjpg';
case PJPEG = 'pjpeg';
case WEBP = 'webp';
case AVIF = 'avif';
case BMP = 'bmp';
case GIF = 'gif';
case PNG = 'png';
case TIF = 'tif';
case TIFF = 'tiff';
case JP2 = 'jp2';
case J2K = 'j2k';
case JP2K = 'jp2k';
case JPF = 'jpf';
case JPM = 'jpm';
case JPG2 = 'jpg2';
case J2C = 'j2c';
case JPC = 'jpc';
case JPX = 'jpx';
case HEIC = 'heic';
case HEIF = 'heif';
case ICO = 'ico';
/**
* Create file extension from given identifier.
*
* @throws InvalidArgumentException
*/
public static function create(string|self|Format|MediaType $identifier): self
{
if ($identifier instanceof self) {
return $identifier;
}
if ($identifier instanceof Format) {
try {
return $identifier->fileExtension();
} catch (NotSupportedException $e) {
throw new InvalidArgumentException(
'Unable to create file extension from "' . $identifier::class . '"',
previous: $e
);
}
}
if ($identifier instanceof MediaType) {
try {
return $identifier->fileExtension();
} catch (NotSupportedException $e) {
throw new InvalidArgumentException(
'Unable to create file extension from "' . $identifier->value . '"',
previous: $e
);
}
}
try {
$extension = self::from(strtolower($identifier));
} catch (Error) {
try {
$extension = MediaType::from(strtolower($identifier))->fileExtension();
} catch (Error | NotSupportedException) {
throw new InvalidArgumentException('Unable to create file extension from "' . $identifier . '"');
}
}
return $extension;
}
/**
* Try to create media type from given identifier and return null on failure.
*/
public static function tryCreate(string|self|Format|MediaType $identifier): ?self
{
try {
return self::create($identifier);
} catch (InvalidArgumentException) {
return null;
}
}
/**
* Return the matching format for the current file extension.
*/
public function format(): Format
{
return match ($this) {
self::JPEG,
self::JPG,
self::PJPEG,
self::PJPG => Format::JPEG,
self::WEBP => Format::WEBP,
self::GIF => Format::GIF,
self::PNG => Format::PNG,
self::AVIF => Format::AVIF,
self::BMP => Format::BMP,
self::TIF,
self::TIFF => Format::TIFF,
self::JP2,
self::JP2K,
self::J2K,
self::JPF,
self::JPM,
self::JPG2,
self::J2C,
self::JPC,
self::JPX => Format::JP2,
self::HEIC,
self::HEIF => Format::HEIC,
self::ICO => Format::ICO,
};
}
/**
* Return media types for the current file extension.
*
* @return array<MediaType>
*/
public function mediaTypes(): array
{
return $this->format()->mediaTypes();
}
/**
* Return the first found media type for the current file extension.
*
* @throws NotSupportedException
*/
public function mediaType(): MediaType
{
return $this->format()->mediaType();
}
}