1 | <?php |
||
16 | class StreamReader |
||
17 | { |
||
18 | /** @var bool Flag whether allow_url_fopen is enabled */ |
||
19 | protected $isFopenEnabled = false; |
||
20 | |||
21 | /** @var string Data retrieved from remote */ |
||
22 | public $data = ''; |
||
23 | |||
24 | /** |
||
25 | * Constructor for fastImageSize class |
||
26 | */ |
||
27 | 76 | public function __construct() |
|
32 | |||
33 | /** |
||
34 | * Reset stream reader data |
||
35 | */ |
||
36 | 75 | public function resetData() |
|
40 | |||
41 | /** |
||
42 | * Get force length data |
||
43 | * |
||
44 | * @param int $offset |
||
45 | * @param int $length |
||
46 | * @return bool|string Returned data if long enough, false if not |
||
47 | */ |
||
48 | 47 | protected function getForcedLengthData($offset, $length) |
|
52 | |||
53 | /** |
||
54 | * Get image from specified path/source |
||
55 | * |
||
56 | * @param string $filename Path to image |
||
57 | * @param int $offset Offset at which reading of the image should start |
||
58 | * @param int $length Maximum length that should be read |
||
59 | * @param bool $forceLength True if the length needs to be the specified |
||
60 | * length, false if not. Default: true |
||
61 | * |
||
62 | * @return false|string Image data or false if result was empty |
||
63 | */ |
||
64 | 73 | public function getImage($filename, $offset, $length, $forceLength = true) |
|
80 | |||
81 | /** |
||
82 | * Get image data for specified filename with offset and length |
||
83 | * |
||
84 | * @param string $filename Path to image |
||
85 | * @param int $offset Offset at which reading of the image should start |
||
86 | * @param int $length Maximum length that should be read |
||
87 | */ |
||
88 | 73 | protected function getImageData($filename, $offset, $length) |
|
89 | { |
||
90 | // Check if we don't have a valid scheme according to RFC 3986 and |
||
91 | // try to use file_get_contents in that case |
||
92 | 73 | if (preg_match('#^([a-z][a-z0-9+\-.]+://)#i', $filename)) |
|
93 | 73 | { |
|
94 | try |
||
95 | { |
||
96 | 4 | $body = $this->getSeekableImageData($filename, $offset); |
|
97 | |||
98 | 2 | while ($body !== null && !$body->eof()) |
|
99 | { |
||
100 | 2 | if (!$this->readDataFromBody($body, $length)) |
|
101 | 2 | { |
|
102 | 1 | break; |
|
103 | } |
||
104 | 1 | } |
|
105 | } |
||
106 | 4 | catch (\GuzzleHttp\Exception\RequestException $exception) |
|
107 | { |
||
108 | // Silently fail in case of issues during guzzle request |
||
109 | } |
||
110 | 4 | } |
|
111 | |||
112 | 73 | $this->getImageDataFopen($filename, $offset, $length); |
|
113 | 73 | } |
|
114 | |||
115 | /** |
||
116 | * Read data from guzzle request body |
||
117 | * |
||
118 | * @param \GuzzleHttp\Stream\StreamInterface $body Request body as stream |
||
119 | * @param int $length Requested length for file |
||
120 | * @return bool True while reading and not having reached the end of the |
||
121 | * targeted length, false if end of targeted length has been reached |
||
122 | */ |
||
123 | 2 | protected function readDataFromBody(\GuzzleHttp\Stream\StreamInterface $body, $length) |
|
124 | { |
||
125 | 2 | $readLength = min($length - strlen($this->data), TypeJpegHelper::JPEG_CHUNK_SIZE); |
|
126 | 2 | $this->data .= $body->read($readLength); |
|
127 | 2 | if ($readLength < TypeJpegHelper::JPEG_CHUNK_SIZE || strlen($this->data == $readLength)) |
|
128 | 2 | { |
|
129 | 1 | return false; |
|
130 | } |
||
131 | |||
132 | 1 | return true; |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * Get image data using file get contents if data is empty and |
||
137 | * allow_url_fopen is enabled |
||
138 | * |
||
139 | * @param string $filename Path to image |
||
140 | * @param int $offset Offset at which reading of the image should start |
||
141 | * @param int $length Maximum length that should be read |
||
142 | */ |
||
143 | 73 | protected function getImageDataFopen($filename, $offset, $length) |
|
150 | |||
151 | /** |
||
152 | * Get seekable image data in form of Guzzle stream interface |
||
153 | * |
||
154 | * @param string $filename Filename / URL to get |
||
155 | * @param int $offset Offset for response body |
||
156 | * @return \GuzzleHttp\Stream\StreamInterface|null Stream interface of |
||
157 | * requested image or null if it could not be retrieved |
||
158 | */ |
||
159 | 31 | public function getSeekableImageData($filename, $offset) |
|
174 | } |
||
175 |