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
|
|
|
); |
67
|
|
|
|
68
|
|
|
/** @var array Class map that links image extensions/mime types to class */ |
69
|
|
|
protected $classMap; |
70
|
|
|
|
71
|
|
|
/** @var array An array containing the classes of supported image types */ |
72
|
|
|
protected $type; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Constructor for fastImageSize class |
76
|
|
|
*/ |
77
|
57 |
|
public function __construct() |
78
|
|
|
{ |
79
|
57 |
|
foreach ($this->supportedTypes as $imageType => $extension) |
80
|
|
|
{ |
81
|
57 |
|
$className = '\FastImageSize\Type\Type' . ucfirst(strtolower($imageType)); |
82
|
57 |
|
$this->type[$imageType] = new $className($this); |
83
|
|
|
|
84
|
|
|
// Create class map |
85
|
57 |
|
foreach ($extension as $ext) |
86
|
|
|
{ |
87
|
|
|
/** @var Type\TypeInterface */ |
88
|
57 |
|
$this->classMap[$ext] = $this->type[$imageType]; |
89
|
57 |
|
} |
90
|
57 |
|
} |
91
|
57 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get image dimensions of supplied image |
95
|
|
|
* |
96
|
|
|
* @param string $file Path to image that should be checked |
97
|
|
|
* @param string $type Mimetype of image |
98
|
|
|
* @return array|bool Array with image dimensions if successful, false if not |
99
|
|
|
*/ |
100
|
57 |
|
public function getImageSize($file, $type = '') |
101
|
|
|
{ |
102
|
|
|
// Reset values |
103
|
57 |
|
$this->resetValues(); |
104
|
|
|
|
105
|
|
|
// Treat image type as unknown if extension or mime type is unknown |
106
|
57 |
|
if (!preg_match('/\.([a-z0-9]+)$/i', $file, $match) && empty($type)) |
107
|
57 |
|
{ |
108
|
15 |
|
$this->getImagesizeUnknownType($file); |
109
|
15 |
|
} |
110
|
|
|
else |
111
|
|
|
{ |
112
|
42 |
|
$extension = (isset($match[1])) ? $match[1] : preg_replace('/.+\/([a-z0-9-.]+)$/i', '$1', $type); |
113
|
|
|
|
114
|
42 |
|
$this->getImageSizeByExtension($file, $extension); |
115
|
|
|
} |
116
|
|
|
|
117
|
57 |
|
return sizeof($this->size) > 1 ? $this->size : false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get dimensions of image if type is unknown |
122
|
|
|
* |
123
|
|
|
* @param string $filename Path to file |
124
|
|
|
*/ |
125
|
15 |
|
protected function getImagesizeUnknownType($filename) |
126
|
|
|
{ |
127
|
|
|
// Grab the maximum amount of bytes we might need |
128
|
15 |
|
$data = $this->getImage($filename, 0, Type\TypeJpeg::JPEG_MAX_HEADER_SIZE, false); |
129
|
|
|
|
130
|
15 |
|
if ($data !== false) |
131
|
15 |
|
{ |
132
|
14 |
|
foreach ($this->type as $imageType) |
133
|
|
|
{ |
134
|
14 |
|
$imageType->getSize($filename); |
135
|
|
|
|
136
|
14 |
|
if (sizeof($this->size) > 1) |
137
|
14 |
|
{ |
138
|
14 |
|
break; |
139
|
|
|
} |
140
|
14 |
|
} |
141
|
14 |
|
} |
142
|
15 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get image size by file extension |
146
|
|
|
* |
147
|
|
|
* @param string $file Path to image that should be checked |
148
|
|
|
* @param string $extension Extension/type of image |
149
|
|
|
*/ |
150
|
42 |
|
protected function getImageSizeByExtension($file, $extension) |
151
|
|
|
{ |
152
|
42 |
|
if (isset($this->classMap[$extension])) |
153
|
42 |
|
{ |
154
|
41 |
|
$this->classMap[$extension]->getSize($file); |
155
|
41 |
|
} |
156
|
42 |
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Reset values to default |
160
|
|
|
*/ |
161
|
57 |
|
protected function resetValues() |
162
|
|
|
{ |
163
|
57 |
|
$this->size = array(); |
164
|
57 |
|
$this->data = ''; |
165
|
57 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Set mime type based on supplied image |
169
|
|
|
* |
170
|
|
|
* @param int $type Type of image |
171
|
|
|
*/ |
172
|
33 |
|
public function setImageType($type) |
173
|
|
|
{ |
174
|
33 |
|
$this->size['type'] = $type; |
175
|
33 |
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Set size info |
179
|
|
|
* |
180
|
|
|
* @param array $size Array containing size info for image |
181
|
|
|
*/ |
182
|
39 |
|
public function setSize($size) |
183
|
|
|
{ |
184
|
39 |
|
$this->size = $size; |
185
|
39 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get image from specified path/source |
189
|
|
|
* |
190
|
|
|
* @param string $filename Path to image |
191
|
|
|
* @param int $offset Offset at which reading of the image should start |
192
|
|
|
* @param int $length Maximum length that should be read |
193
|
|
|
* @param bool $forceLength True if the length needs to be the specified |
194
|
|
|
* length, false if not. Default: true |
195
|
|
|
* |
196
|
|
|
* @return false|string Image data or false if result was empty |
197
|
|
|
*/ |
198
|
56 |
|
public function getImage($filename, $offset, $length, $forceLength = true) |
199
|
|
|
{ |
200
|
56 |
|
if (empty($this->data)) |
201
|
56 |
|
{ |
202
|
56 |
|
$this->data = @file_get_contents($filename, null, null, $offset, $length); |
203
|
|
|
|
204
|
56 |
|
if ($this->data === false) { |
205
|
|
|
|
206
|
|
|
$headers = array( |
207
|
5 |
|
'Range: bytes=0-' . ($length - 1) |
208
|
5 |
|
); |
209
|
|
|
|
210
|
5 |
|
$curl = curl_init($filename); |
211
|
5 |
|
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); |
212
|
5 |
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
213
|
5 |
|
$this->data = curl_exec($curl); |
214
|
5 |
|
curl_close($curl); |
215
|
|
|
|
216
|
5 |
|
if (isset($offset)) { |
217
|
5 |
|
$this->data = substr($this->data, $offset); |
218
|
5 |
|
} |
219
|
5 |
|
} |
220
|
56 |
|
} |
221
|
|
|
|
222
|
|
|
// Force length to expected one. Return false if data length |
223
|
|
|
// is smaller than expected length |
224
|
56 |
|
if ($forceLength === true) |
225
|
56 |
|
{ |
226
|
40 |
|
return (strlen($this->data) < $length) ? false : substr($this->data, $offset, $length) ; |
227
|
|
|
} |
228
|
|
|
|
229
|
30 |
|
return empty($this->data) ? false : $this->data; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Get return data |
234
|
|
|
* |
235
|
|
|
* @return array|bool Size array if dimensions could be found, false if not |
236
|
|
|
*/ |
237
|
|
|
protected function getReturnData() |
238
|
|
|
{ |
239
|
|
|
return sizeof($this->size) > 1 ? $this->size : false; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|