]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/media/GIFMetadataExtractor.php
MediaWiki 1.16.0
[autoinstalls/mediawiki.git] / includes / media / GIFMetadataExtractor.php
1 <?php
2 /**
3   * GIF frame counter.
4   * Originally written in Perl by Steve Sanbeg.
5   * Ported to PHP by Andrew Garrett
6   * Deliberately not using MWExceptions to avoid external dependencies, encouraging
7   * redistribution.
8   */
9
10 class GIFMetadataExtractor {
11         static $gif_frame_sep;
12         static $gif_extension_sep;
13         static $gif_term;
14
15         static function getMetadata( $filename ) {
16                 self::$gif_frame_sep = pack( "C", ord("," ) );
17                 self::$gif_extension_sep = pack( "C", ord("!" ) );
18                 self::$gif_term = pack( "C", ord(";" ) );
19                 
20                 $frameCount = 0;
21                 $duration = 0.0;
22                 $isLooped = false;
23                 
24                 if (!$filename)
25                         throw new Exception( "No file name specified" );
26                 elseif ( !file_exists($filename) || is_dir($filename) )
27                         throw new Exception( "File $filename does not exist" );
28                 
29                 $fh = fopen( $filename, 'r' );
30                 
31                 if (!$fh)
32                         throw new Exception( "Unable to open file $filename" );
33                 
34                 // Check for the GIF header
35                 $buf = fread( $fh, 6 );
36                 if ( !($buf == 'GIF87a' || $buf == 'GIF89a') ) {
37                         throw new Exception( "Not a valid GIF file; header: $buf" );
38                 }
39                 
40                 // Skip over width and height.
41                 fread( $fh, 4 );
42                 
43                 // Read BPP
44                 $buf = fread( $fh, 1 );
45                 $bpp = self::decodeBPP( $buf );
46                 
47                 // Skip over background and aspect ratio
48                 fread( $fh, 2 );
49                 
50                 // Skip over the GCT
51                 self::readGCT( $fh, $bpp );
52                 
53                 while( !feof( $fh ) ) {
54                         $buf = fread( $fh, 1 );
55                         
56                         if ($buf == self::$gif_frame_sep) {
57                                 // Found a frame
58                                 $frameCount++;
59                                 
60                                 ## Skip bounding box
61                                 fread( $fh, 8 );
62                                 
63                                 ## Read BPP
64                                 $buf = fread( $fh, 1 );
65                                 $bpp = self::decodeBPP( $buf );
66                                 
67                                 ## Read GCT
68                                 self::readGCT( $fh, $bpp );
69                                 fread( $fh, 1 );
70                                 self::skipBlock( $fh ); 
71                         } elseif ( $buf == self::$gif_extension_sep ) {
72                                 $buf = fread( $fh, 1 );
73                                 $extension_code = unpack( 'C', $buf );
74                                 $extension_code = $extension_code[1];
75                                 
76                                 if ($extension_code == 0xF9) {
77                                         // Graphics Control Extension.
78                                         fread( $fh, 1 ); // Block size
79                                         
80                                         fread( $fh, 1 ); // Transparency, disposal method, user input
81                                         
82                                         $buf = fread( $fh, 2 ); // Delay, in hundredths of seconds.
83                                         $delay = unpack( 'v', $buf );
84                                         $delay = $delay[1];
85                                         $duration += $delay * 0.01;
86                                         
87                                         fread( $fh, 1 ); // Transparent colour index
88                                         
89                                         $term = fread( $fh, 1 ); // Should be a terminator
90                                         $term = unpack( 'C', $term );
91                                         $term = $term[1];
92                                         if ($term != 0 )
93                                                 throw new Exception( "Malformed Graphics Control Extension block" );
94                                 } elseif ($extension_code == 0xFF) {
95                                         // Application extension (Netscape info about the animated gif)
96                                         $blockLength = fread( $fh, 1 );
97                                         $blockLength = unpack( 'C', $blockLength );
98                                         $blockLength = $blockLength[1];
99                                         $data = fread( $fh, $blockLength );
100                                         
101                                         // NETSCAPE2.0 (application name)
102                                         if ($blockLength != 11 || $data != 'NETSCAPE2.0') {
103                                                 fseek( $fh, -($blockLength + 1), SEEK_CUR );
104                                                 self::skipBlock( $fh );
105                                                 continue;
106                                         }
107                                         
108                                         $data = fread( $fh, 2 ); // Block length and introduction, should be 03 01
109                                         
110                                         if ($data != "\x03\x01") {
111                                                 throw new Exception( "Expected \x03\x01, got $data" );
112                                         }
113                                         
114                                         // Unsigned little-endian integer, loop count or zero for "forever"
115                                         $loopData = fread( $fh, 2 );
116                                         $loopData = unpack( 'v', $loopData );
117                                         $loopCount = $loopData[1];
118                                         
119                                         if ($loopCount != 1) {
120                                                 $isLooped = true;
121                                         }
122                                         
123                                         // Read out terminator byte
124                                         fread( $fh, 1 );
125                                 } else {
126                                         self::skipBlock( $fh );
127                                 }
128                         } elseif ( $buf == self::$gif_term ) {
129                                 break;
130                         } else {
131                                 $byte = unpack( 'C', $buf );
132                                 $byte = $byte[1];
133                                 throw new Exception( "At position: ".ftell($fh). ", Unknown byte ".$byte );
134                         }
135                 }
136                 
137                 return array(
138                         'frameCount' => $frameCount,
139                         'looped' => $isLooped,
140                         'duration' => $duration
141                 );
142                 
143         }
144         
145         static function readGCT( $fh, $bpp ) {
146                 if ($bpp > 0) {
147                         for( $i=1; $i<=pow(2,$bpp); ++$i ) {
148                                 fread( $fh, 3 );
149                         }
150                 }
151         }
152         
153         static function decodeBPP( $data ) {
154                 $buf = unpack( 'C', $data );
155                 $buf = $buf[1];
156                 $bpp = ( $buf & 7 ) + 1;
157                 $buf >>= 7;
158                 
159                 $have_map = $buf & 1;
160                 
161                 return $have_map ? $bpp : 0;
162         }
163         
164         static function skipBlock( $fh ) {
165                 while ( !feof( $fh ) ) {
166                         $buf = fread( $fh, 1 );
167                         $block_len = unpack( 'C', $buf );
168                         $block_len = $block_len[1];
169                         if ($block_len == 0)
170                                 return;
171                         fread( $fh, $block_len );
172                 }
173         }
174
175 }