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