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