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
19 class GIFMetadataExtractor
{
20 static $gif_frame_sep;
21 static $gif_extension_sep;
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(";" ) );
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" );
39 $fh = fopen( $filename, 'r' );
42 throw new Exception( "Unable to open file $filename" );
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" );
51 // Skip over width and height.
55 $buf = fread( $fh, 1 );
56 $bpp = self
::decodeBPP( $buf );
58 // Skip over background and aspect ratio
62 self
::readGCT( $fh, $bpp );
64 while( !feof( $fh ) ) {
65 $buf = fread( $fh, 1 );
67 if ($buf == self
::$gif_frame_sep) {
75 $buf = fread( $fh, 1 );
76 $bpp = self
::decodeBPP( $buf );
79 self
::readGCT( $fh, $bpp );
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];
87 if ($extension_code == 0xF9) {
88 // Graphics Control Extension.
89 fread( $fh, 1 ); // Block size
91 fread( $fh, 1 ); // Transparency, disposal method, user input
93 $buf = fread( $fh, 2 ); // Delay, in hundredths of seconds.
94 $delay = unpack( 'v', $buf );
96 $duration +
= $delay * 0.01;
98 fread( $fh, 1 ); // Transparent colour index
100 $term = fread( $fh, 1 ); // Should be a terminator
101 $term = unpack( 'C', $term );
104 throw new Exception( "Malformed Graphics Control Extension block" );
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 );
113 // NETSCAPE2.0 (application name)
114 if ($blockLength != 11 ||
$data != 'NETSCAPE2.0') {
115 fseek( $fh, -($blockLength +
1), SEEK_CUR
);
116 self
::skipBlock( $fh );
120 $data = fread( $fh, 2 ); // Block length and introduction, should be 03 01
122 if ($data != "\x03\x01") {
123 throw new Exception( "Expected \x03\x01, got $data" );
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];
131 if ($loopCount != 1) {
135 // Read out terminator byte
138 self
::skipBlock( $fh );
140 } elseif ( $buf == self
::$gif_term ) {
143 $byte = unpack( 'C', $buf );
145 throw new Exception( "At position: ".ftell($fh). ", Unknown byte ".$byte );
150 'frameCount' => $frameCount,
151 'looped' => $isLooped,
152 'duration' => $duration
156 static function readGCT( $fh, $bpp ) {
158 for( $i=1; $i<=pow( 2, $bpp ); ++
$i ) {
164 static function decodeBPP( $data ) {
165 $buf = unpack( 'C', $data );
167 $bpp = ( $buf & 7 ) +
1;
170 $have_map = $buf & 1;
172 return $have_map ?
$bpp : 0;
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) {
183 fread( $fh, $block_len );