|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* FastImage - Because sometimes you just want the size! |
|
5
|
|
|
* Based on the Ruby Implementation by Steven Sykes (https://github.com/sdsykes/fastimage) |
|
6
|
|
|
* |
|
7
|
|
|
* Copyright (c) 2012 Tom Moor |
|
8
|
|
|
* Tom Moor, http://tommoor.com |
|
9
|
|
|
* |
|
10
|
|
|
* MIT Licensed |
|
11
|
|
|
* @version 0.1 |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
class FastImage |
|
15
|
|
|
{ |
|
16
|
|
|
private $strpos = 0; |
|
17
|
|
|
private $str; |
|
18
|
|
|
private $type; |
|
19
|
|
|
private $handle; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct($uri = null) |
|
22
|
|
|
{ |
|
23
|
|
|
if ($uri) $this->load($uri); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
public function load($uri) |
|
28
|
|
|
{ |
|
29
|
|
|
if ($this->handle) $this->close(); |
|
30
|
|
|
|
|
31
|
|
|
$this->handle = fopen($uri, 'r'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
public function close() |
|
36
|
|
|
{ |
|
37
|
|
|
if ($this->handle) |
|
38
|
|
|
{ |
|
39
|
|
|
fclose($this->handle); |
|
40
|
|
|
$this->handle = null; |
|
41
|
|
|
$this->type = null; |
|
42
|
|
|
$this->str = null; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
public function getSize() |
|
48
|
|
|
{ |
|
49
|
|
|
if (!$this->handle) |
|
50
|
|
|
{ |
|
51
|
|
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$this->strpos = 0; |
|
55
|
|
|
if ($this->getType()) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
return array_values($this->parseSize()); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
public function getType() |
|
65
|
|
|
{ |
|
66
|
|
|
if (!$this->handle) |
|
67
|
|
|
{ |
|
68
|
|
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->strpos = 0; |
|
72
|
|
|
|
|
73
|
|
|
if (!$this->type) |
|
74
|
|
|
{ |
|
75
|
|
|
switch ($this->getChars(2)) |
|
76
|
|
|
{ |
|
77
|
|
|
case "BM": |
|
78
|
|
|
return $this->type = 'bmp'; |
|
79
|
|
|
case "GI": |
|
80
|
|
|
return $this->type = 'gif'; |
|
81
|
|
|
case chr(0xFF).chr(0xd8): |
|
82
|
|
|
return $this->type = 'jpeg'; |
|
83
|
|
|
case chr(0x89).'P': |
|
84
|
|
|
return $this->type = 'png'; |
|
85
|
|
|
default: |
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $this->type; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
private function parseSize() |
|
95
|
|
|
{ |
|
96
|
|
|
$this->strpos = 0; |
|
97
|
|
|
|
|
98
|
|
|
switch ($this->type) |
|
99
|
|
|
{ |
|
100
|
|
|
case 'png': |
|
101
|
|
|
return $this->parseSizeForPNG(); |
|
102
|
|
|
case 'gif': |
|
103
|
|
|
return $this->parseSizeForGIF(); |
|
104
|
|
|
case 'bmp': |
|
105
|
|
|
return $this->parseSizeForBMP(); |
|
106
|
|
|
case 'jpeg': |
|
107
|
|
|
return $this->parseSizeForJPEG(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return null; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
private function parseSizeForPNG() |
|
115
|
|
|
{ |
|
116
|
|
|
$chars = $this->getChars(25); |
|
117
|
|
|
|
|
118
|
|
|
return unpack("N*", substr($chars, 16, 8)); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
private function parseSizeForGIF() |
|
123
|
|
|
{ |
|
124
|
|
|
$chars = $this->getChars(11); |
|
125
|
|
|
|
|
126
|
|
|
return unpack("S*", substr($chars, 6, 4)); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
private function parseSizeForBMP() |
|
131
|
|
|
{ |
|
132
|
|
|
$chars = $this->getChars(29); |
|
133
|
|
|
$chars = substr($chars, 14, 14); |
|
134
|
|
|
$type = unpack('C', $chars); |
|
135
|
|
|
|
|
136
|
|
|
return (reset($type) == 40) ? unpack('L*', substr($chars, 4)) : unpack('L*', substr($chars, 4, 8)); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
private function parseSizeForJPEG() |
|
141
|
|
|
{ |
|
142
|
|
|
$state = null; |
|
143
|
|
|
$i = 0; |
|
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
while (true) |
|
146
|
|
|
{ |
|
147
|
|
|
switch ($state) |
|
148
|
|
|
{ |
|
149
|
|
|
default: |
|
150
|
|
|
$this->getChars(2); |
|
151
|
|
|
$state = 'started'; |
|
152
|
|
|
break; |
|
153
|
|
|
|
|
154
|
|
|
case 'started': |
|
155
|
|
|
$b = $this->getByte(); |
|
156
|
|
|
if ($b === false) return false; |
|
157
|
|
|
|
|
158
|
|
|
$state = $b == 0xFF ? 'sof' : 'started'; |
|
159
|
|
|
break; |
|
160
|
|
|
|
|
161
|
|
|
case 'sof': |
|
162
|
|
|
$b = $this->getByte(); |
|
163
|
|
|
if (in_array($b, range(0xe0, 0xef))) |
|
164
|
|
|
{ |
|
165
|
|
|
$state = 'skipframe'; |
|
166
|
|
|
} |
|
167
|
|
|
elseif (in_array($b, array_merge(range(0xC0,0xC3), range(0xC5,0xC7), range(0xC9,0xCB), range(0xCD,0xCF)))) |
|
168
|
|
|
{ |
|
169
|
|
|
$state = 'readsize'; |
|
170
|
|
|
} |
|
171
|
|
|
elseif ($b == 0xFF) |
|
172
|
|
|
{ |
|
173
|
|
|
$state = 'sof'; |
|
174
|
|
|
} |
|
175
|
|
|
else |
|
176
|
|
|
{ |
|
177
|
|
|
$state = 'skipframe'; |
|
178
|
|
|
} |
|
179
|
|
|
break; |
|
180
|
|
|
|
|
181
|
|
|
case 'skipframe': |
|
182
|
|
|
$skip = $this->readInt($this->getChars(2)) - 2; |
|
183
|
|
|
$state = 'doskip'; |
|
184
|
|
|
break; |
|
185
|
|
|
|
|
186
|
|
|
case 'doskip': |
|
187
|
|
|
$this->getChars($skip); |
|
|
|
|
|
|
188
|
|
|
$state = 'started'; |
|
189
|
|
|
break; |
|
190
|
|
|
|
|
191
|
|
|
case 'readsize': |
|
192
|
|
|
$c = $this->getChars(7); |
|
193
|
|
|
|
|
194
|
|
|
return array($this->readInt(substr($c, 5, 2)), $this->readInt(substr($c, 3, 2))); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
|
|
200
|
|
|
private function getChars($n) |
|
201
|
|
|
{ |
|
202
|
|
|
$response = null; |
|
203
|
|
|
|
|
204
|
|
|
// do we need more data? |
|
205
|
|
|
if ($this->strpos + $n -1 >= strlen($this->str)) |
|
206
|
|
|
{ |
|
207
|
|
|
$end = ($this->strpos + $n); |
|
208
|
|
|
|
|
209
|
|
|
while (strlen($this->str) < $end && $response !== false) |
|
210
|
|
|
{ |
|
211
|
|
|
// read more from the file handle |
|
212
|
|
|
$need = $end - ftell($this->handle); |
|
213
|
|
|
|
|
214
|
|
|
if ($response = fread($this->handle, $need)) |
|
215
|
|
|
{ |
|
216
|
|
|
$this->str .= $response; |
|
217
|
|
|
} |
|
218
|
|
|
else |
|
219
|
|
|
{ |
|
220
|
|
|
return false; |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
$result = substr($this->str, $this->strpos, $n); |
|
226
|
|
|
$this->strpos += $n; |
|
227
|
|
|
|
|
228
|
|
|
return $result; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
|
|
232
|
|
|
private function getByte() |
|
233
|
|
|
{ |
|
234
|
|
|
$c = $this->getChars(1); |
|
235
|
|
|
$b = unpack("C", $c); |
|
236
|
|
|
|
|
237
|
|
|
return reset($b); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
|
|
241
|
|
|
private function readInt($str) |
|
242
|
|
|
{ |
|
243
|
|
|
$size = unpack("C*", $str); |
|
244
|
|
|
|
|
245
|
|
|
return ($size[1] << 8) + $size[2]; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
|
|
249
|
|
|
public function __destruct() |
|
250
|
|
|
{ |
|
251
|
|
|
$this->close(); |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: