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