PluginProbe
QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress / 3.2.1
QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress v3.2.1
4.1.2 4.1.1 4.1.0 4.0.1 4.0.0 3.3.1 3.3.0 trunk 1.0.0 2.0.0 2.1.0 3.0.0 3.1.0 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8
quickwebp / includes / vendor / intervention / image / src / Intervention / Image / AbstractDecoder.php

AbstractDecoder.php in QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress 3.2.1, at includes/vendor/intervention/image/src/Intervention/Image/AbstractDecoder.php

365 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Intervention\Image;
4
5 use GuzzleHttp\Psr7\Stream;
6 use Intervention\Image\Exception\NotReadableException;
7 use Psr\Http\Message\StreamInterface;
8
9 abstract class AbstractDecoder
10 {
11 /**
12 * Initiates new image from path in filesystem
13 *
14 * @param string $path
15 * @return \Intervention\Image\Image
16 */
17 abstract public function initFromPath($path);
18
19 /**
20 * Initiates new image from binary data
21 *
22 * @param string $data
23 * @return \Intervention\Image\Image
24 */
25 abstract public function initFromBinary($data);
26
27 /**
28 * Initiates new image from GD resource
29 *
30 * @param Resource $resource
31 * @return \Intervention\Image\Image
32 */
33 abstract public function initFromGdResource($resource);
34
35 /**
36 * Initiates new image from Imagick object
37 *
38 * @param \Imagick $object
39 * @return \Intervention\Image\Image
40 */
41 abstract public function initFromImagick(\Imagick $object);
42
43 /**
44 * Buffer of input data
45 *
46 * @var mixed
47 */
48 private $data;
49
50 /**
51 * Creates new Decoder with data
52 *
53 * @param mixed $data
54 */
55 public function __construct($data = null)
56 {
57 $this->data = $data;
58 }
59
60 /**
61 * Init from given URL
62 *
63 * @param string $url
64 * @return \Intervention\Image\Image
65 */
66 public function initFromUrl($url)
67 {
68
69 $options = [
70 'http' => [
71 'method'=>"GET",
72 'protocol_version'=>1.1, // force use HTTP 1.1 for service mesh environment with envoy
73 'header'=>"Accept-language: en\r\n".
74 "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36\r\n"
75 ]
76 ];
77
78 $context = stream_context_create($options);
79
80
81 if ($data = @file_get_contents($url, false, $context)) {
82 return $this->initFromBinary($data);
83 }
84
85 throw new NotReadableException(
86 "Unable to init from given url (".$url.")."
87 );
88 }
89
90 /**
91 * Init from given stream
92 *
93 * @param StreamInterface|resource $stream
94 * @return \Intervention\Image\Image
95 */
96 public function initFromStream($stream)
97 {
98 if (!$stream instanceof StreamInterface) {
99 $stream = new Stream($stream);
100 }
101
102 try {
103 $offset = $stream->tell();
104 } catch (\RuntimeException $e) {
105 $offset = 0;
106 }
107
108 $shouldAndCanSeek = $offset !== 0 && $stream->isSeekable();
109
110 if ($shouldAndCanSeek) {
111 $stream->rewind();
112 }
113
114 try {
115 $data = $stream->getContents();
116 } catch (\RuntimeException $e) {
117 $data = null;
118 }
119
120 if ($shouldAndCanSeek) {
121 $stream->seek($offset);
122 }
123
124 if ($data) {
125 return $this->initFromBinary($data);
126 }
127
128 throw new NotReadableException(
129 "Unable to init from given stream"
130 );
131 }
132
133 /**
134 * Determines if current source data is GD resource
135 *
136 * @return boolean
137 */
138 public function isGdResource()
139 {
140 if (is_resource($this->data)) {
141 return (get_resource_type($this->data) == 'gd');
142 }
143
144 if ($this->data instanceof \GdImage) {
145 return true;
146 }
147
148 return false;
149 }
150
151 /**
152 * Determines if current source data is Imagick object
153 *
154 * @return boolean
155 */
156 public function isImagick()
157 {
158 return is_a($this->data, 'Imagick');
159 }
160
161 /**
162 * Determines if current source data is Intervention\Image\Image object
163 *
164 * @return boolean
165 */
166 public function isInterventionImage()
167 {
168 return is_a($this->data, '\Intervention\Image\Image');
169 }
170
171 /**
172 * Determines if current data is SplFileInfo object
173 *
174 * @return boolean
175 */
176 public function isSplFileInfo()
177 {
178 return is_a($this->data, 'SplFileInfo');
179 }
180
181 /**
182 * Determines if current data is Symfony UploadedFile component
183 *
184 * @return boolean
185 */
186 public function isSymfonyUpload()
187 {
188 return is_a($this->data, 'Symfony\Component\HttpFoundation\File\UploadedFile');
189 }
190
191 /**
192 * Determines if current source data is file path
193 *
194 * @return boolean
195 */
196 public function isFilePath()
197 {
198 if (is_string($this->data)) {
199 try {
200 return is_file($this->data);
201 } catch (\Exception $e) {
202 return false;
203 }
204 }
205
206 return false;
207 }
208
209 /**
210 * Determines if current source data is url
211 *
212 * @return boolean
213 */
214 public function isUrl()
215 {
216 return (bool) filter_var($this->data, FILTER_VALIDATE_URL);
217 }
218
219 /**
220 * Determines if current source data is a stream resource
221 *
222 * @return boolean
223 */
224 public function isStream()
225 {
226 if ($this->data instanceof StreamInterface) return true;
227 if (!is_resource($this->data)) return false;
228 if (get_resource_type($this->data) !== 'stream') return false;
229
230 return true;
231 }
232
233 /**
234 * Determines if current source data is binary data
235 *
236 * @return boolean
237 */
238 public function isBinary()
239 {
240 if (is_string($this->data)) {
241 $mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $this->data);
242 return (substr($mime, 0, 4) != 'text' && $mime != 'application/x-empty');
243 }
244
245 return false;
246 }
247
248 /**
249 * Determines if current source data is data-url
250 *
251 * @return boolean
252 */
253 public function isDataUrl()
254 {
255 $data = $this->decodeDataUrl($this->data);
256
257 return is_null($data) ? false : true;
258 }
259
260 /**
261 * Determines if current source data is base64 encoded
262 *
263 * @return boolean
264 */
265 public function isBase64()
266 {
267 if (!is_string($this->data)) {
268 return false;
269 }
270
271 return base64_encode(base64_decode($this->data)) === str_replace(["\n", "\r"], '', $this->data);
272 }
273
274 /**
275 * Initiates new Image from Intervention\Image\Image
276 *
277 * @param Image $object
278 * @return \Intervention\Image\Image
279 */
280 public function initFromInterventionImage($object)
281 {
282 return $object;
283 }
284
285 /**
286 * Parses and decodes binary image data from data-url
287 *
288 * @param string $data_url
289 * @return string
290 */
291 private function decodeDataUrl($data_url)
292 {
293 if (!is_string($data_url)) {
294 return null;
295 }
296
297 $pattern = "/^data:(?:image\/[a-zA-Z\-\.]+)(?:charset=\".+\")?;base64,(?P<data>.+)$/";
298 preg_match($pattern, str_replace(["\n", "\r"], '', $data_url), $matches);
299
300 if (is_array($matches) && array_key_exists('data', $matches)) {
301 return base64_decode($matches['data']);
302 }
303
304 return null;
305 }
306
307 /**
308 * Initiates new image from mixed data
309 *
310 * @param mixed $data
311 * @return \Intervention\Image\Image
312 */
313 public function init($data)
314 {
315 $this->data = $data;
316
317 switch (true) {
318
319 case $this->isGdResource():
320 return $this->initFromGdResource($this->data);
321
322 case $this->isImagick():
323 return $this->initFromImagick($this->data);
324
325 case $this->isInterventionImage():
326 return $this->initFromInterventionImage($this->data);
327
328 case $this->isSplFileInfo():
329 return $this->initFromPath($this->data->getRealPath());
330
331 case $this->isBinary():
332 return $this->initFromBinary($this->data);
333
334 case $this->isUrl():
335 return $this->initFromUrl($this->data);
336
337 case $this->isStream():
338 return $this->initFromStream($this->data);
339
340 case $this->isDataUrl():
341 return $this->initFromBinary($this->decodeDataUrl($this->data));
342
343 case $this->isFilePath():
344 return $this->initFromPath($this->data);
345
346 // isBase64 has to be after isFilePath to prevent false positives
347 case $this->isBase64():
348 return $this->initFromBinary(base64_decode($this->data));
349
350 default:
351 throw new NotReadableException("Image source not readable");
352 }
353 }
354
355 /**
356 * Decoder object transforms to string source data
357 *
358 * @return string
359 */
360 public function __toString()
361 {
362 return (string) $this->data;
363 }
364 }
365