-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathEncoder.php
More file actions
163 lines (142 loc) · 3.88 KB
/
Encoder.php
File metadata and controls
163 lines (142 loc) · 3.88 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
<?php
declare(strict_types=1);
namespace League\Glide\Api;
use Intervention\Image\Interfaces\EncodedImageInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\MediaType;
/**
* Encoder Api class to convert a given image to a specific format.
*/
class Encoder
{
/**
* The manipulation params.
*/
protected array $params;
/**
* Class constructor.
*
* @param array $params the manipulator params
*/
public function __construct(array $params = [])
{
$this->params = $params;
}
/**
* Set the manipulation params.
*
* @param array $params The manipulation params.
*
* @return $this
*/
public function setParams(array $params)
{
$this->params = $params;
return $this;
}
/**
* Get a specific manipulation param.
*/
public function getParam(string $name): mixed
{
return array_key_exists($name, $this->params)
? $this->params[$name]
: null;
}
/**
* Perform output image manipulation.
*
* @param ImageInterface $image The source image.
*
* @return EncodedImageInterface The encoded image.
*/
public function run(ImageInterface $image): EncodedImageInterface
{
$format = $this->getFormat($image);
$quality = $this->getQuality();
$shouldInterlace = filter_var($this->getParam('interlace'), FILTER_VALIDATE_BOOLEAN);
if ('pjpg' === $format) {
$shouldInterlace = true;
$format = 'jpg';
}
$encoderOptions = [];
switch ($format) {
case 'avif':
case 'heic':
case 'tiff':
case 'webp':
$encoderOptions['quality'] = $quality;
break;
case 'jpg':
$encoderOptions['quality'] = $quality;
$encoderOptions['progressive'] = $shouldInterlace;
break;
case 'gif':
case 'png':
$encoderOptions['interlaced'] = $shouldInterlace;
break;
default:
throw new \Exception("Invalid format provided: {$format}");
}
return $image->encodeByExtension($format, ...$encoderOptions);
}
/**
* Resolve format.
*
* @param ImageInterface $image The source image.
*
* @return string The resolved format.
*/
public function getFormat(ImageInterface $image): string
{
$fm = (string) $this->getParam('fm');
if ($fm) {
if (!array_key_exists($fm, static::supportedFormats())) {
throw new \InvalidArgumentException("Invalid format provided: {$fm}");
}
return $fm;
}
$mediaType = MediaType::tryFrom($image->origin()->mediaType());
if (null === $mediaType) {
return 'jpg';
}
$fm = $mediaType->format()->fileExtension()->value;
return array_key_exists($fm, static::supportedFormats()) ? $fm : 'jpg';
}
/**
* Get a list of supported image formats and MIME types.
*
* @return array<string,string>
*/
public static function supportedFormats(): array
{
return [
'avif' => 'image/avif',
'gif' => 'image/gif',
'jpg' => 'image/jpeg',
'pjpg' => 'image/jpeg',
'png' => 'image/png',
'webp' => 'image/webp',
'tiff' => 'image/tiff',
'heic' => 'image/heic',
];
}
/**
* Resolve quality.
*
* @return int The resolved quality.
*/
public function getQuality(): int
{
$default = 85;
$q = $this->getParam('q');
if (
!is_numeric($q)
|| $q < 0
|| $q > 100
) {
return $default;
}
return (int) $q;
}
}