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