-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathAbstractDecoder.php
More file actions
97 lines (83 loc) Β· 2.78 KB
/
AbstractDecoder.php
File metadata and controls
97 lines (83 loc) Β· 2.78 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
<?php
declare(strict_types=1);
namespace Intervention\Image\Drivers;
use Exception;
use Intervention\Image\Collection;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Exceptions\InvalidArgumentException;
use Intervention\Image\Exceptions\RuntimeException;
use Intervention\Image\Interfaces\CollectionInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Traits\CanBuildStream;
use Intervention\Image\Traits\CanParseFilePath;
use Stringable;
use Throwable;
abstract class AbstractDecoder implements DecoderInterface
{
use CanBuildStream;
use CanParseFilePath;
/**
* Determine if the given input is GIF data format.
*/
protected function isGifFormat(string $input): bool
{
return str_starts_with($input, 'GIF87a') || str_starts_with($input, 'GIF89a');
}
/**
* Extract and return EXIF data from given input which can be a file path
* or a stream stream resource.
*
* @throws InvalidArgumentException
* @throws DecoderException
* @return CollectionInterface<string, mixed>
*/
protected function extractExifData(string $input): CollectionInterface
{
if (!function_exists('exif_read_data')) {
return new Collection();
}
try {
// source might be file path
$source = self::readableFilePathOrFail($input);
} catch (Throwable) {
try {
// source might be stream resource
$source = self::buildStreamOrFail($input);
} catch (RuntimeException) {
return new Collection();
}
}
try {
// extract exif data
$data = @exif_read_data($source, null, true);
if (is_resource($source)) {
fclose($source);
}
} catch (Exception) {
$data = [];
}
return new Collection(is_array($data) ? $data : []);
}
/**
* Decodes given base64 encoded data.
*
* @throws InvalidArgumentException
* @throws DecoderException
*/
protected function decodeBase64Data(mixed $input): string
{
if (!is_string($input) && !$input instanceof Stringable) {
throw new InvalidArgumentException(
'Base64-encoded data must be either of type string or instance of Stringable',
);
}
$decoded = base64_decode((string) $input, true);
if ($decoded === false) {
throw new DecoderException('Input is not valid Base64-encoded data');
}
if (base64_encode($decoded) !== str_replace(["\n", "\r"], '', (string) $input)) {
throw new DecoderException('Input is not valid Base64-encoded data');
}
return $decoded;
}
}