Merge changes Ic0eaac33,Iaa81af5b
[lhc/web/wiklou.git] / includes / media / GIFMetadataExtractor.php
1 <?php
2 /**
3 * GIF frame counter.
4 *
5 * Originally written in Perl by Steve Sanbeg.
6 * Ported to PHP by Andrew Garrett
7 * Deliberately not using MWExceptions to avoid external dependencies, encouraging
8 * redistribution.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 *
25 * @file
26 * @ingroup Media
27 */
28
29 /**
30 * GIF frame counter.
31 *
32 * @ingroup Media
33 */
34 class GIFMetadataExtractor {
35 static $gif_frame_sep;
36
37 static $gif_extension_sep;
38
39 static $gif_term;
40
41 const VERSION = 1;
42
43 // Each sub-block is less than or equal to 255 bytes.
44 // Most of the time its 255 bytes, except for in XMP
45 // blocks, where it's usually between 32-127 bytes each.
46 const MAX_SUBBLOCKS = 262144; // 5mb divided by 20.
47
48 /**
49 * @throws Exception
50 * @param $filename string
51 * @return array
52 */
53 static function getMetadata( $filename ) {
54 self::$gif_frame_sep = pack( "C", ord( "," ) );
55 self::$gif_extension_sep = pack( "C", ord( "!" ) );
56 self::$gif_term = pack( "C", ord( ";" ) );
57
58 $frameCount = 0;
59 $duration = 0.0;
60 $isLooped = false;
61 $xmp = "";
62 $comment = array();
63
64 if ( !$filename ) {
65 throw new Exception( "No file name specified" );
66 } elseif ( !file_exists( $filename ) || is_dir( $filename ) ) {
67 throw new Exception( "File $filename does not exist" );
68 }
69
70 $fh = fopen( $filename, 'rb' );
71
72 if ( !$fh ) {
73 throw new Exception( "Unable to open file $filename" );
74 }
75
76 // Check for the GIF header
77 $buf = fread( $fh, 6 );
78 if ( !( $buf == 'GIF87a' || $buf == 'GIF89a' ) ) {
79 throw new Exception( "Not a valid GIF file; header: $buf" );
80 }
81
82 // Skip over width and height.
83 fread( $fh, 4 );
84
85 // Read BPP
86 $buf = fread( $fh, 1 );
87 $bpp = self::decodeBPP( $buf );
88
89 // Skip over background and aspect ratio
90 fread( $fh, 2 );
91
92 // Skip over the GCT
93 self::readGCT( $fh, $bpp );
94
95 while ( !feof( $fh ) ) {
96 $buf = fread( $fh, 1 );
97
98 if ( $buf == self::$gif_frame_sep ) {
99 // Found a frame
100 $frameCount++;
101
102 ## Skip bounding box
103 fread( $fh, 8 );
104
105 ## Read BPP
106 $buf = fread( $fh, 1 );
107 $bpp = self::decodeBPP( $buf );
108
109 ## Read GCT
110 self::readGCT( $fh, $bpp );
111 fread( $fh, 1 );
112 self::skipBlock( $fh );
113 } elseif ( $buf == self::$gif_extension_sep ) {
114 $buf = fread( $fh, 1 );
115 if ( strlen( $buf ) < 1 ) {
116 throw new Exception( "Ran out of input" );
117 }
118 $extension_code = unpack( 'C', $buf );
119 $extension_code = $extension_code[1];
120
121 if ( $extension_code == 0xF9 ) {
122 // Graphics Control Extension.
123 fread( $fh, 1 ); // Block size
124
125 fread( $fh, 1 ); // Transparency, disposal method, user input
126
127 $buf = fread( $fh, 2 ); // Delay, in hundredths of seconds.
128 if ( strlen( $buf ) < 2 ) {
129 throw new Exception( "Ran out of input" );
130 }
131 $delay = unpack( 'v', $buf );
132 $delay = $delay[1];
133 $duration += $delay * 0.01;
134
135 fread( $fh, 1 ); // Transparent colour index
136
137 $term = fread( $fh, 1 ); // Should be a terminator
138 if ( strlen( $term ) < 1 ) {
139 throw new Exception( "Ran out of input" );
140 }
141 $term = unpack( 'C', $term );
142 $term = $term[1];
143 if ( $term != 0 ) {
144 throw new Exception( "Malformed Graphics Control Extension block" );
145 }
146 } elseif ( $extension_code == 0xFE ) {
147 // Comment block(s).
148 $data = self::readBlock( $fh );
149 if ( $data === "" ) {
150 throw new Exception( 'Read error, zero-length comment block' );
151 }
152
153 // The standard says this should be ASCII, however its unclear if
154 // thats true in practise. Check to see if its valid utf-8, if so
155 // assume its that, otherwise assume its windows-1252 (iso-8859-1)
156 $dataCopy = $data;
157 // quickIsNFCVerify has the side effect of replacing any invalid characters
158 UtfNormal::quickIsNFCVerify( $dataCopy );
159
160 if ( $dataCopy !== $data ) {
161 wfSuppressWarnings();
162 $data = iconv( 'windows-1252', 'UTF-8', $data );
163 wfRestoreWarnings();
164 }
165
166 $commentCount = count( $comment );
167 if ( $commentCount === 0
168 || $comment[$commentCount - 1] !== $data
169 ) {
170 // Some applications repeat the same comment on each
171 // frame of an animated GIF image, so if this comment
172 // is identical to the last, only extract once.
173 $comment[] = $data;
174 }
175 } elseif ( $extension_code == 0xFF ) {
176 // Application extension (Netscape info about the animated gif)
177 // or XMP (or theoretically any other type of extension block)
178 $blockLength = fread( $fh, 1 );
179 if ( strlen( $blockLength ) < 1 ) {
180 throw new Exception( "Ran out of input" );
181 }
182 $blockLength = unpack( 'C', $blockLength );
183 $blockLength = $blockLength[1];
184 $data = fread( $fh, $blockLength );
185
186 if ( $blockLength != 11 ) {
187 wfDebug( __METHOD__ . " GIF application block with wrong length\n" );
188 fseek( $fh, -( $blockLength + 1 ), SEEK_CUR );
189 self::skipBlock( $fh );
190 continue;
191 }
192
193 // NETSCAPE2.0 (application name for animated gif)
194 if ( $data == 'NETSCAPE2.0' ) {
195 $data = fread( $fh, 2 ); // Block length and introduction, should be 03 01
196
197 if ( $data != "\x03\x01" ) {
198 throw new Exception( "Expected \x03\x01, got $data" );
199 }
200
201 // Unsigned little-endian integer, loop count or zero for "forever"
202 $loopData = fread( $fh, 2 );
203 if ( strlen( $loopData ) < 2 ) {
204 throw new Exception( "Ran out of input" );
205 }
206 $loopData = unpack( 'v', $loopData );
207 $loopCount = $loopData[1];
208
209 if ( $loopCount != 1 ) {
210 $isLooped = true;
211 }
212
213 // Read out terminator byte
214 fread( $fh, 1 );
215 } elseif ( $data == 'XMP DataXMP' ) {
216 // application name for XMP data.
217 // see pg 18 of XMP spec part 3.
218
219 $xmp = self::readBlock( $fh, true );
220
221 if ( substr( $xmp, -257, 3 ) !== "\x01\xFF\xFE"
222 || substr( $xmp, -4 ) !== "\x03\x02\x01\x00"
223 ) {
224 // this is just a sanity check.
225 throw new Exception( "XMP does not have magic trailer!" );
226 }
227
228 // strip out trailer.
229 $xmp = substr( $xmp, 0, -257 );
230 } else {
231 // unrecognized extension block
232 fseek( $fh, -( $blockLength + 1 ), SEEK_CUR );
233 self::skipBlock( $fh );
234 continue;
235 }
236 } else {
237 self::skipBlock( $fh );
238 }
239 } elseif ( $buf == self::$gif_term ) {
240 break;
241 } else {
242 if ( strlen( $buf ) < 1 ) {
243 throw new Exception( "Ran out of input" );
244 }
245 $byte = unpack( 'C', $buf );
246 $byte = $byte[1];
247 throw new Exception( "At position: " . ftell( $fh ) . ", Unknown byte " . $byte );
248 }
249 }
250
251 return array(
252 'frameCount' => $frameCount,
253 'looped' => $isLooped,
254 'duration' => $duration,
255 'xmp' => $xmp,
256 'comment' => $comment,
257 );
258 }
259
260 /**
261 * @param $fh
262 * @param $bpp
263 * @return void
264 */
265 static function readGCT( $fh, $bpp ) {
266 if ( $bpp > 0 ) {
267 for ( $i = 1; $i <= pow( 2, $bpp ); ++$i ) {
268 fread( $fh, 3 );
269 }
270 }
271 }
272
273 /**
274 * @param $data
275 * @throws Exception
276 * @return int
277 */
278 static function decodeBPP( $data ) {
279 if ( strlen( $data ) < 1 ) {
280 throw new Exception( "Ran out of input" );
281 }
282 $buf = unpack( 'C', $data );
283 $buf = $buf[1];
284 $bpp = ( $buf & 7 ) + 1;
285 $buf >>= 7;
286
287 $have_map = $buf & 1;
288
289 return $have_map ? $bpp : 0;
290 }
291
292 /**
293 * @param $fh
294 * @throws Exception
295 */
296 static function skipBlock( $fh ) {
297 while ( !feof( $fh ) ) {
298 $buf = fread( $fh, 1 );
299 if ( strlen( $buf ) < 1 ) {
300 throw new Exception( "Ran out of input" );
301 }
302 $block_len = unpack( 'C', $buf );
303 $block_len = $block_len[1];
304 if ( $block_len == 0 ) {
305 return;
306 }
307 fread( $fh, $block_len );
308 }
309 }
310
311 /**
312 * Read a block. In the GIF format, a block is made up of
313 * several sub-blocks. Each sub block starts with one byte
314 * saying how long the sub-block is, followed by the sub-block.
315 * The entire block is terminated by a sub-block of length
316 * 0.
317 * @param $fh FileHandle
318 * @param $includeLengths Boolean Include the length bytes of the
319 * sub-blocks in the returned value. Normally this is false,
320 * except XMP is weird and does a hack where you need to keep
321 * these length bytes.
322 * @throws Exception
323 * @return string The data.
324 */
325 static function readBlock( $fh, $includeLengths = false ) {
326 $data = '';
327 $subLength = fread( $fh, 1 );
328 $blocks = 0;
329
330 while ( $subLength !== "\0" ) {
331 $blocks++;
332 if ( $blocks > self::MAX_SUBBLOCKS ) {
333 throw new Exception( "MAX_SUBBLOCKS exceeded (over $blocks sub-blocks)" );
334 }
335 if ( feof( $fh ) ) {
336 throw new Exception( "Read error: Unexpected EOF." );
337 }
338 if ( $includeLengths ) {
339 $data .= $subLength;
340 }
341
342 $data .= fread( $fh, ord( $subLength ) );
343 $subLength = fread( $fh, 1 );
344 }
345
346 return $data;
347 }
348 }