1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* fast-image-size base class |
5
|
|
|
* @package fast-image-size |
6
|
|
|
* @copyright (c) Marc Alexander <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FastImageSize; |
13
|
|
|
|
14
|
|
|
class FastImageSize |
15
|
|
|
{ |
16
|
|
|
/** @var array Size info that is returned */ |
17
|
|
|
protected $size = array(); |
18
|
|
|
|
19
|
|
|
/** @var string Data retrieved from remote */ |
20
|
|
|
protected $data = ''; |
21
|
|
|
|
22
|
|
|
/** @var array List of supported image types and associated image types */ |
23
|
|
|
protected $supportedTypes = array( |
24
|
|
|
'png' => array('png'), |
25
|
|
|
'gif' => array('gif'), |
26
|
|
|
'jpeg' => array( |
27
|
|
|
'jpeg', |
28
|
|
|
'jpg', |
29
|
|
|
'jpe', |
30
|
|
|
'jif', |
31
|
|
|
'jfif', |
32
|
|
|
'jfi', |
33
|
|
|
), |
34
|
|
|
'jp2' => array( |
35
|
|
|
'jp2', |
36
|
|
|
'j2k', |
37
|
|
|
'jpf', |
38
|
|
|
'jpg2', |
39
|
|
|
'jpx', |
40
|
|
|
'jpm', |
41
|
|
|
), |
42
|
|
|
'psd' => array( |
43
|
|
|
'psd', |
44
|
|
|
'photoshop', |
45
|
|
|
), |
46
|
|
|
'bmp' => array('bmp'), |
47
|
|
|
'tif' => array( |
48
|
|
|
'tif', |
49
|
|
|
'tiff', |
50
|
|
|
), |
51
|
|
|
'wbmp' => array( |
52
|
|
|
'wbm', |
53
|
|
|
'wbmp', |
54
|
|
|
'vnd.wap.wbmp', |
55
|
|
|
), |
56
|
|
|
'iff' => array( |
57
|
|
|
'iff', |
58
|
|
|
'x-iff', |
59
|
|
|
), |
60
|
|
|
'ico' => array( |
61
|
|
|
'ico', |
62
|
|
|
'vnd.microsoft.icon', |
63
|
|
|
'x-icon', |
64
|
|
|
'icon', |
65
|
|
|
), |
66
|
|
|
'webp' => array( |
67
|
|
|
'webp', |
68
|
|
|
) |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
/** @var array Class map that links image extensions/mime types to class */ |
72
|
|
|
protected $classMap; |
73
|
|
|
|
74
|
|
|
/** @var array An array containing the classes of supported image types */ |
75
|
|
|
protected $type; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get image dimensions of supplied image |
79
|
|
|
* |
80
|
|
|
* @param string $file Path to image that should be checked |
81
|
|
|
* @param string $type Mimetype of image |
82
|
|
|
* @return array|bool Array with image dimensions if successful, false if not |
83
|
|
|
*/ |
84
|
89 |
|
public function getImageSize($file, $type = '') |
85
|
|
|
{ |
86
|
|
|
// Reset values |
87
|
89 |
|
$this->resetValues(); |
88
|
|
|
|
89
|
|
|
// Treat image type as unknown if extension or mime type is unknown |
90
|
89 |
|
if (!preg_match('/\.([a-z0-9]+)$/i', $file, $match) && empty($type)) |
91
|
89 |
|
{ |
92
|
17 |
|
$this->getImagesizeUnknownType($file); |
93
|
17 |
|
} |
94
|
|
|
else |
95
|
|
|
{ |
96
|
72 |
|
$extension = (empty($type) && isset($match[1])) ? $match[1] : preg_replace('/.+\/([a-z0-9-.]+)$/i', '$1', $type); |
97
|
|
|
|
98
|
72 |
|
$this->getImageSizeByExtension($file, $extension); |
99
|
|
|
|
100
|
|
|
if(!(count($this->size) > 0)) |
101
|
89 |
|
{ |
102
|
|
|
$this->data = ''; |
103
|
|
|
$this->getImagesizeUnknownType($file); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return sizeof($this->size) > 1 ? $this->size : false; |
108
|
|
|
} |
109
|
17 |
|
|
110
|
|
|
/** |
111
|
|
|
* Get dimensions of image if type is unknown |
112
|
17 |
|
* |
113
|
|
|
* @param string $filename Path to file |
114
|
17 |
|
*/ |
115
|
17 |
|
protected function getImagesizeUnknownType($filename) |
116
|
16 |
|
{ |
117
|
16 |
|
// Grab the maximum amount of bytes we might need |
118
|
|
|
$data = $this->getImage($filename, 0, Type\TypeJpeg::JPEG_MAX_HEADER_SIZE, false); |
119
|
16 |
|
|
120
|
|
|
if ($data !== false) |
121
|
16 |
|
{ |
122
|
16 |
|
$this->loadAllTypes(); |
123
|
16 |
|
foreach ($this->type as $imageType) |
124
|
|
|
{ |
125
|
16 |
|
$imageType->getSize($filename); |
126
|
16 |
|
|
127
|
17 |
|
if (sizeof($this->size) > 1) |
128
|
|
|
{ |
129
|
|
|
break; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
72 |
|
/** |
136
|
|
|
* Get image size by file extension |
137
|
72 |
|
* |
138
|
72 |
|
* @param string $file Path to image that should be checked |
139
|
72 |
|
* @param string $extension Extension/type of image |
140
|
72 |
|
*/ |
141
|
71 |
|
protected function getImageSizeByExtension($file, $extension) |
142
|
71 |
|
{ |
143
|
72 |
|
$extension = strtolower($extension); |
144
|
|
|
$this->loadExtension($extension); |
145
|
|
|
if (isset($this->classMap[$extension])) |
146
|
|
|
{ |
147
|
|
|
$this->classMap[$extension]->getSize($file); |
148
|
89 |
|
} |
149
|
|
|
} |
150
|
89 |
|
|
151
|
89 |
|
/** |
152
|
89 |
|
* Reset values to default |
153
|
|
|
*/ |
154
|
|
|
protected function resetValues() |
155
|
|
|
{ |
156
|
|
|
$this->size = array(); |
157
|
|
|
$this->data = ''; |
158
|
|
|
} |
159
|
56 |
|
|
160
|
|
|
/** |
161
|
56 |
|
* Set mime type based on supplied image |
162
|
56 |
|
* |
163
|
|
|
* @param int $type Type of image |
164
|
|
|
*/ |
165
|
|
|
public function setImageType($type) |
166
|
|
|
{ |
167
|
|
|
$this->size['type'] = $type; |
168
|
|
|
} |
169
|
64 |
|
|
170
|
|
|
/** |
171
|
64 |
|
* Set size info |
172
|
64 |
|
* |
173
|
|
|
* @param array $size Array containing size info for image |
174
|
|
|
*/ |
175
|
|
|
public function setSize($size) |
176
|
|
|
{ |
177
|
|
|
$this->size = $size; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get image from specified path/source |
182
|
|
|
* |
183
|
|
|
* @param string $filename Path to image |
184
|
|
|
* @param int $offset Offset at which reading of the image should start |
185
|
88 |
|
* @param int $length Maximum length that should be read |
186
|
|
|
* @param bool $forceLength True if the length needs to be the specified |
187
|
88 |
|
* length, false if not. Default: true |
188
|
88 |
|
* |
189
|
88 |
|
* @return false|string Image data or false if result was empty |
190
|
88 |
|
*/ |
191
|
|
|
public function getImage($filename, $offset, $length, $forceLength = true) |
192
|
|
|
{ |
193
|
|
|
if (empty($this->data)) |
194
|
88 |
|
{ |
195
|
88 |
|
$this->data = @file_get_contents($filename, null, null, $offset, $length); |
196
|
55 |
|
} |
197
|
|
|
|
198
|
|
|
// Force length to expected one. Return false if data length |
199
|
49 |
|
// is smaller than expected length |
200
|
|
|
if ($forceLength === true) |
201
|
|
|
{ |
202
|
|
|
return (strlen($this->data) < $length) ? false : substr($this->data, $offset, $length) ; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return empty($this->data) ? false : $this->data; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Get return data |
210
|
|
|
* |
211
|
|
|
* @return array|bool Size array if dimensions could be found, false if not |
212
|
|
|
*/ |
213
|
|
|
protected function getReturnData() |
214
|
|
|
{ |
215
|
16 |
|
return sizeof($this->size) > 1 ? $this->size : false; |
216
|
|
|
} |
217
|
16 |
|
|
218
|
|
|
/** |
219
|
16 |
|
* Load all supported types |
220
|
16 |
|
*/ |
221
|
16 |
|
protected function loadAllTypes() |
222
|
|
|
{ |
223
|
|
|
foreach ($this->supportedTypes as $imageType => $extension) |
224
|
|
|
{ |
225
|
|
|
$this->loadType($imageType); |
226
|
|
|
} |
227
|
|
|
} |
228
|
72 |
|
|
229
|
|
|
/** |
230
|
72 |
|
* Load an image type by extension |
231
|
72 |
|
* |
232
|
3 |
|
* @param string $extension Extension of image |
233
|
|
|
*/ |
234
|
72 |
|
protected function loadExtension($extension) |
235
|
|
|
{ |
236
|
72 |
|
if (isset($this->classMap[$extension])) |
237
|
72 |
|
{ |
238
|
71 |
|
return; |
239
|
71 |
|
} |
240
|
72 |
|
foreach ($this->supportedTypes as $imageType => $extensions) |
241
|
72 |
|
{ |
242
|
|
|
if (in_array($extension, $extensions, true)) |
243
|
|
|
{ |
244
|
|
|
$this->loadType($imageType); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
} |
248
|
87 |
|
|
249
|
|
|
/** |
250
|
87 |
|
* Load an image type |
251
|
87 |
|
* |
252
|
|
|
* @param string $imageType Mimetype |
253
|
|
|
*/ |
254
|
|
|
protected function loadType($imageType) |
255
|
87 |
|
{ |
256
|
87 |
|
if (isset($this->type[$imageType])) |
257
|
|
|
{ |
258
|
|
|
return; |
259
|
87 |
|
} |
260
|
|
|
|
261
|
|
|
$className = '\FastImageSize\Type\Type' . mb_convert_case(mb_strtolower($imageType), MB_CASE_TITLE); |
262
|
87 |
|
$this->type[$imageType] = new $className($this); |
263
|
87 |
|
|
264
|
87 |
|
// Create class map |
265
|
|
|
foreach ($this->supportedTypes[$imageType] as $ext) |
266
|
|
|
{ |
267
|
|
|
/** @var Type\TypeInterface */ |
268
|
|
|
$this->classMap[$ext] = $this->type[$imageType]; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|