-
Notifications
You must be signed in to change notification settings - Fork 59
/
Fastimage.php
243 lines (190 loc) · 3.95 KB
/
Fastimage.php
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/**
* FastImage - Because sometimes you just want the size!
* Based on the Ruby Implementation by Steven Sykes (https://github.com/sdsykes/fastimage)
*
* Copyright (c) 2012 Tom Moor
* Tom Moor, http://tommoor.com
*
* MIT Licensed
* @version 0.1
*/
class FastImage
{
private $strpos = 0;
private $str;
private $type;
private $handle;
public function __construct($uri = null)
{
if ($uri) $this->load($uri);
}
public function load($uri)
{
if ($this->handle) $this->close();
$this->handle = fopen($uri, 'r');
}
public function close()
{
if ($this->handle)
{
fclose($this->handle);
$this->handle = null;
$this->type = null;
$this->str = null;
}
}
public function getSize()
{
$this->strpos = 0;
if ($this->getType())
{
return array_values($this->parseSize());
}
return false;
}
public function getType()
{
$this->strpos = 0;
if (!$this->type)
{
switch ($this->getChars(2))
{
case "BM":
return $this->type = 'bmp';
case "GI":
return $this->type = 'gif';
case chr(0xFF).chr(0xd8):
return $this->type = 'jpeg';
case chr(0x89).'P':
return $this->type = 'png';
default:
return false;
}
}
return $this->type;
}
private function parseSize()
{
$this->strpos = 0;
switch ($this->type)
{
case 'png':
return $this->parseSizeForPNG();
case 'gif':
return $this->parseSizeForGIF();
case 'bmp':
return $this->parseSizeForBMP();
case 'jpeg':
return $this->parseSizeForJPEG();
}
return null;
}
private function parseSizeForPNG()
{
$chars = $this->getChars(25);
return unpack("N*", substr($chars, 16, 8));
}
private function parseSizeForGIF()
{
$chars = $this->getChars(11);
return unpack("S*", substr($chars, 6, 4));
}
private function parseSizeForBMP()
{
$chars = $this->getChars(29);
$chars = substr($chars, 14, 14);
$type = unpack('C', $chars);
return (reset($type) == 40) ? unpack('L*', substr($chars, 4)) : unpack('L*', substr($chars, 4, 8));
}
private function parseSizeForJPEG()
{
$state = null;
$i = 0;
while (true)
{
switch ($state)
{
default:
$this->getChars(2);
$state = 'started';
break;
case 'started':
$b = $this->getByte();
if ($b === false) return false;
$state = $b == 0xFF ? 'sof' : 'started';
break;
case 'sof':
$b = $this->getByte();
if (in_array($b, range(0xe0, 0xef)))
{
$state = 'skipframe';
}
elseif (in_array($b, array_merge(range(0xC0,0xC3), range(0xC5,0xC7), range(0xC9,0xCB), range(0xCD,0xCF))))
{
$state = 'readsize';
}
elseif ($b == 0xFF)
{
$state = 'sof';
}
else
{
$state = 'skipframe';
}
break;
case 'skipframe':
$skip = $this->readInt($this->getChars(2)) - 2;
$state = 'doskip';
break;
case 'doskip':
$this->getChars($skip);
$state = 'started';
break;
case 'readsize':
$c = $this->getChars(7);
return array($this->readInt(substr($c, 5, 2)), $this->readInt(substr($c, 3, 2)));
}
}
}
private function getChars($n)
{
$response = null;
// do we need more data?
if ($this->strpos + $n -1 >= strlen($this->str))
{
$end = ($this->strpos + $n);
while (strlen($this->str) < $end && $response !== false)
{
// read more from the file handle
$need = $end - ftell($this->handle);
if ($response = fread($this->handle, $need))
{
$this->str .= $response;
}
else
{
return false;
}
}
}
$result = substr($this->str, $this->strpos, $n);
$this->strpos += $n;
return $result;
}
private function getByte()
{
$c = $this->getChars(1);
$b = unpack("C", $c);
return reset($b);
}
private function readInt($str)
{
$size = unpack("C*", $str);
return ($size[1] << 8) + $size[2];
}
public function __destruct()
{
$this->close();
}
}