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