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
10 class GIFMetadataExtractor
{
11 static $gif_frame_sep;
12 static $gif_extension_sep;
15 static function getMetadata( $filename ) {
16 wfProfileIn( __METHOD__
);
18 self
::$gif_frame_sep = pack( "C", ord("," ) );
19 self
::$gif_extension_sep = pack( "C", ord("!" ) );
20 self
::$gif_term = pack( "C", ord(";" ) );
27 throw new Exception( "No file name specified" );
28 elseif ( !file_exists($filename) ||
is_dir($filename) )
29 throw new Exception( "File $filename does not exist" );
31 $fh = fopen( $filename, 'r' );
34 throw new Exception( "Unable to open file $filename" );
36 // Check for the GIF header
37 $buf = fread( $fh, 6 );
38 if ( !($buf == 'GIF87a' ||
$buf == 'GIF89a') ) {
39 throw new Exception( "Not a valid GIF file; header: $buf" );
42 // Skip over width and height.
46 $buf = fread( $fh, 1 );
47 $bpp = self
::decodeBPP( $buf );
49 // Skip over background and aspect ratio
53 self
::readGCT( $fh, $bpp );
55 while( !feof( $fh ) ) {
56 $buf = fread( $fh, 1 );
58 if ($buf == self
::$gif_frame_sep) {
62 ## Skip dimensions (Why is this 8 bytes and not 4?)
66 $buf = fread( $fh, 1 );
67 $bpp = self
::decodeBPP( $buf );
70 self
::readGCT( $fh, $bpp );
72 self
::skipBlock( $fh );
73 } elseif ( $buf == self
::$gif_extension_sep ) {
74 $buf = fread( $fh, 1 );
75 $extension_code = unpack( 'C', $buf );
76 $extension_code = $extension_code[1];
78 if ($extension_code == 0xF9) {
79 // Graphics Control Extension.
80 fread( $fh, 1 ); // Block size
81 fread( $fh, 1 ); // Transparency, disposal method, user input
83 $buf = fread( $fh, 2 ); // Delay, in hundredths of seconds.
84 $delay = unpack( 'v', $buf );
86 $duration +
= $delay * 0.01;
88 fread( $fh, 1 ); // Transparent colour index
90 $term = fread( $fh, 1 ); // Should be a terminator
91 $term = unpack( 'C', $term );
94 throw new Exception( "Malformed Graphics Control Extension block" );
95 } elseif ($extension_code == 0xFF) {
96 // Application extension (Netscape info about the animated gif)
97 $blockLength = fread( $fh, 1 );
98 $blockLength = unpack( 'C', $blockLength );
99 $blockLength = $blockLength[1];
100 $data = fread( $fh, $blockLength );
102 // NETSCAPE2.0 (application name)
103 if ($blockLength != 11 ||
$data != 'NETSCAPE2.0') {
108 fread( $fh, 2 ); // Block length and introduction, should be 03 01
110 // Unsigned little-endian integer, loop count or zero for "forever"
111 $loopData = fread( $fh, 2 );
112 $loopData = unpack( 'v', $loopData );
113 $loopCount = $loopData[1];
115 if ($loopCount != 1) {
119 // Read out terminator byte
122 } elseif ( $buf == self
::$gif_term ) {
125 $byte = unpack( 'C', $buf );
127 throw new Exception( "At position: ".ftell($fh). ", Unknown byte ".$byte );
131 wfProfileOut( __METHOD__
);
134 'frameCount' => $frameCount,
135 'looped' => $isLooped,
136 'duration' => $duration
141 static function readGCT( $fh, $bpp ) {
143 for( $i=1; $i<=pow(2,$bpp); ++
$i ) {
149 static function decodeBPP( $data ) {
150 $buf = unpack( 'C', $data );
152 $bpp = ( $buf & 7 ) +
1;
155 $have_map = $buf & 1;
157 return $have_map ?
$bpp : 0;
160 static function skipBlock( $fh ) {
161 while ( !feof( $fh ) ) {
162 $buf = fread( $fh, 1 );
163 $block_len = unpack( 'C', $buf );
164 $block_len = $block_len[1];
167 fread( $fh, $block_len );