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 self
::$gif_frame_sep = pack( "C", ord("," ) );
17 self
::$gif_extension_sep = pack( "C", ord("!" ) );
18 self
::$gif_term = pack( "C", ord(";" ) );
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" );
29 $fh = fopen( $filename, 'r' );
32 throw new Exception( "Unable to open file $filename" );
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" );
40 // Skip over width and height.
44 $buf = fread( $fh, 1 );
45 $bpp = self
::decodeBPP( $buf );
47 // Skip over background and aspect ratio
51 self
::readGCT( $fh, $bpp );
53 while( !feof( $fh ) ) {
54 $buf = fread( $fh, 1 );
56 if ($buf == self
::$gif_frame_sep) {
64 $buf = fread( $fh, 1 );
65 $bpp = self
::decodeBPP( $buf );
68 self
::readGCT( $fh, $bpp );
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];
76 if ($extension_code == 0xF9) {
77 // Graphics Control Extension.
78 fread( $fh, 1 ); // Block size
80 fread( $fh, 1 ); // Transparency, disposal method, user input
82 $buf = fread( $fh, 2 ); // Delay, in hundredths of seconds.
83 $delay = unpack( 'v', $buf );
85 $duration +
= $delay * 0.01;
87 fread( $fh, 1 ); // Transparent colour index
89 $term = fread( $fh, 1 ); // Should be a terminator
90 $term = unpack( 'C', $term );
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 );
101 // NETSCAPE2.0 (application name)
102 if ($blockLength != 11 ||
$data != 'NETSCAPE2.0') {
103 fseek( $fh, -($blockLength +
1), SEEK_CUR
);
104 self
::skipBlock( $fh );
108 $data = fread( $fh, 2 ); // Block length and introduction, should be 03 01
110 if ($data != "\x03\x01") {
111 throw new Exception( "Expected \x03\x01, got $data" );
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];
119 if ($loopCount != 1) {
123 // Read out terminator byte
126 self
::skipBlock( $fh );
128 } elseif ( $buf == self
::$gif_term ) {
131 $byte = unpack( 'C', $buf );
133 throw new Exception( "At position: ".ftell($fh). ", Unknown byte ".$byte );
138 'frameCount' => $frameCount,
139 'looped' => $isLooped,
140 'duration' => $duration
145 static function readGCT( $fh, $bpp ) {
147 for( $i=1; $i<=pow(2,$bpp); ++
$i ) {
153 static function decodeBPP( $data ) {
154 $buf = unpack( 'C', $data );
156 $bpp = ( $buf & 7 ) +
1;
159 $have_map = $buf & 1;
161 return $have_map ?
$bpp : 0;
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];
171 fread( $fh, $block_len );