-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathFormat.php
More file actions
185 lines (161 loc) Β· 5.13 KB
/
Format.php
File metadata and controls
185 lines (161 loc) Β· 5.13 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
declare(strict_types=1);
namespace Intervention\Image;
use Error;
use Intervention\Image\Encoders\AvifEncoder;
use Intervention\Image\Encoders\BmpEncoder;
use Intervention\Image\Encoders\GifEncoder;
use Intervention\Image\Encoders\HeicEncoder;
use Intervention\Image\Encoders\IcoEncoder;
use Intervention\Image\Encoders\Jpeg2000Encoder;
use Intervention\Image\Encoders\JpegEncoder;
use Intervention\Image\Encoders\PngEncoder;
use Intervention\Image\Encoders\TiffEncoder;
use Intervention\Image\Encoders\WebpEncoder;
use Intervention\Image\Exceptions\InvalidArgumentException;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\Interfaces\EncoderInterface;
use ReflectionClass;
use ReflectionParameter;
enum Format
{
case AVIF;
case BMP;
case GIF;
case HEIC;
case ICO;
case JP2;
case JPEG;
case PNG;
case TIFF;
case WEBP;
/**
* Create format from given identifier.
*
* @throws InvalidArgumentException
*/
public static function create(string|self|MediaType|FileExtension $identifier): self
{
if ($identifier instanceof self) {
return $identifier;
}
if ($identifier instanceof MediaType) {
return $identifier->format();
}
if ($identifier instanceof FileExtension) {
return $identifier->format();
}
try {
$format = MediaType::from(strtolower($identifier))->format();
} catch (Error) {
try {
$format = FileExtension::from(strtolower($identifier))->format();
} catch (Error) {
throw new InvalidArgumentException('Unable to create format from "' . $identifier . '"');
}
}
return $format;
}
/**
* Try to create format from given identifier and return null on failure.
*
* @param string|Format|MediaType|FileExtension $identifier
* @return Format|null
*/
public static function tryCreate(string|self|MediaType|FileExtension $identifier): ?self
{
try {
return self::create($identifier);
} catch (InvalidArgumentException) {
return null;
}
}
/**
* Return the possible media (MIME) types for the current format.
*
* @return array<MediaType>
*/
public function mediaTypes(): array
{
return array_filter(
MediaType::cases(),
fn(MediaType $mediaType): bool => $mediaType->format() === $this
);
}
/**
* Return the first found media type for the current format.
*
* @throws NotSupportedException
*/
public function mediaType(): MediaType
{
$types = $this->mediaTypes();
$result = reset($types);
if (!$result instanceof MediaType) {
throw new NotSupportedException('Unable to retrieve unsupported media type from format');
}
return $result;
}
/**
* Return the possible file extension for the current format.
*
* @return array<FileExtension>
*/
public function fileExtensions(): array
{
return array_filter(
FileExtension::cases(),
fn(FileExtension $fileExtension): bool => $fileExtension->format() === $this
);
}
/**
* Return the first found file extension for the current format.
*
* @throws NotSupportedException
*/
public function fileExtension(): FileExtension
{
$extensions = $this->fileExtensions();
$result = reset($extensions);
if (!$result instanceof FileExtension) {
throw new NotSupportedException('Unable to retrieve unsupported file extension for format');
}
return $result;
}
/**
* Create an encoder instance with given options that matches the format.
*/
public function encoder(mixed ...$options): EncoderInterface
{
// get classname of target encoder from current format
$classname = match ($this) {
self::AVIF => AvifEncoder::class,
self::BMP => BmpEncoder::class,
self::GIF => GifEncoder::class,
self::HEIC => HeicEncoder::class,
self::ICO => IcoEncoder::class,
self::JP2 => Jpeg2000Encoder::class,
self::JPEG => JpegEncoder::class,
self::PNG => PngEncoder::class,
self::TIFF => TiffEncoder::class,
self::WEBP => WebpEncoder::class,
};
// get parameters of target encoder
$parameters = [];
$reflectionClass = new ReflectionClass($classname);
$constructor = $reflectionClass->getConstructor();
if ($constructor !== null) {
$parameters = array_map(
fn(ReflectionParameter $parameter): string => $parameter->getName(),
$constructor->getParameters(),
);
}
// filter out unavailable options of target encoder
$options = array_filter(
$options,
fn(mixed $key): bool => in_array($key, $parameters),
ARRAY_FILTER_USE_KEY,
);
return new $classname(...$options);
}
}