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;
26 // Each sub-block is less than or equal to 255 bytes.
27 // Most of the time its 255 bytes, except for in XMP
28 // blocks, where it's usually between 32-127 bytes each.
29 const MAX_SUBBLOCKS
= 262144; // 5mb divided by 20.
33 * @param $filename string
36 static function getMetadata( $filename ) {
37 self
::$gif_frame_sep = pack( "C", ord("," ) );
38 self
::$gif_extension_sep = pack( "C", ord("!" ) );
39 self
::$gif_term = pack( "C", ord(";" ) );
48 throw new Exception( "No file name specified" );
49 } elseif ( !file_exists( $filename ) ||
is_dir( $filename ) ) {
50 throw new Exception( "File $filename does not exist" );
53 $fh = fopen( $filename, 'rb' );
56 throw new Exception( "Unable to open file $filename" );
59 // Check for the GIF header
60 $buf = fread( $fh, 6 );
61 if ( !($buf == 'GIF87a' ||
$buf == 'GIF89a') ) {
62 throw new Exception( "Not a valid GIF file; header: $buf" );
65 // Skip over width and height.
69 $buf = fread( $fh, 1 );
70 $bpp = self
::decodeBPP( $buf );
72 // Skip over background and aspect ratio
76 self
::readGCT( $fh, $bpp );
78 while( !feof( $fh ) ) {
79 $buf = fread( $fh, 1 );
81 if ($buf == self
::$gif_frame_sep) {
89 $buf = fread( $fh, 1 );
90 $bpp = self
::decodeBPP( $buf );
93 self
::readGCT( $fh, $bpp );
95 self
::skipBlock( $fh );
96 } elseif ( $buf == self
::$gif_extension_sep ) {
97 $buf = fread( $fh, 1 );
98 if ( strlen( $buf ) < 1 ) throw new Exception( "Ran out of input" );
99 $extension_code = unpack( 'C', $buf );
100 $extension_code = $extension_code[1];
102 if ($extension_code == 0xF9) {
103 // Graphics Control Extension.
104 fread( $fh, 1 ); // Block size
106 fread( $fh, 1 ); // Transparency, disposal method, user input
108 $buf = fread( $fh, 2 ); // Delay, in hundredths of seconds.
109 if ( strlen( $buf ) < 2 ) throw new Exception( "Ran out of input" );
110 $delay = unpack( 'v', $buf );
112 $duration +
= $delay * 0.01;
114 fread( $fh, 1 ); // Transparent colour index
116 $term = fread( $fh, 1 ); // Should be a terminator
117 if ( strlen( $term ) < 1 ) throw new Exception( "Ran out of input" );
118 $term = unpack( 'C', $term );
121 throw new Exception( "Malformed Graphics Control Extension block" );
123 } elseif ($extension_code == 0xFE) {
125 $data = self
::readBlock( $fh );
126 if ( $data === "" ) {
127 throw new Exception( 'Read error, zero-length comment block' );
130 // The standard says this should be ASCII, however its unclear if
131 // thats true in practise. Check to see if its valid utf-8, if so
132 // assume its that, otherwise assume its windows-1252 (iso-8859-1)
134 // quickIsNFCVerify has the side effect of replacing any invalid characters
135 UtfNormal
::quickIsNFCVerify( $dataCopy );
137 if ( $dataCopy !== $data ) {
138 wfSuppressWarnings();
139 $data = iconv( 'windows-1252', 'UTF-8', $data );
143 $commentCount = count( $comment );
144 if ( $commentCount === 0
145 ||
$comment[$commentCount-1] !== $data )
147 // Some applications repeat the same comment on each
148 // frame of an animated GIF image, so if this comment
149 // is identical to the last, only extract once.
152 } elseif ($extension_code == 0xFF) {
153 // Application extension (Netscape info about the animated gif)
154 // or XMP (or theoretically any other type of extension block)
155 $blockLength = fread( $fh, 1 );
156 if ( strlen( $blockLength ) < 1 ) throw new Exception( "Ran out of input" );
157 $blockLength = unpack( 'C', $blockLength );
158 $blockLength = $blockLength[1];
159 $data = fread( $fh, $blockLength );
161 if ($blockLength != 11 ) {
162 wfDebug( __METHOD__
. ' GIF application block with wrong length' );
163 fseek( $fh, -($blockLength +
1), SEEK_CUR
);
164 self
::skipBlock( $fh );
168 // NETSCAPE2.0 (application name for animated gif)
169 if ( $data == 'NETSCAPE2.0' ) {
171 $data = fread( $fh, 2 ); // Block length and introduction, should be 03 01
173 if ($data != "\x03\x01") {
174 throw new Exception( "Expected \x03\x01, got $data" );
177 // Unsigned little-endian integer, loop count or zero for "forever"
178 $loopData = fread( $fh, 2 );
179 if ( strlen( $loopData ) < 2 ) throw new Exception( "Ran out of input" );
180 $loopData = unpack( 'v', $loopData );
181 $loopCount = $loopData[1];
183 if ($loopCount != 1) {
187 // Read out terminator byte
189 } elseif ( $data == 'XMP DataXMP' ) {
190 // application name for XMP data.
191 // see pg 18 of XMP spec part 3.
193 $xmp = self
::readBlock( $fh, true );
195 if ( substr( $xmp, -257, 3 ) !== "\x01\xFF\xFE"
196 ||
substr( $xmp, -4 ) !== "\x03\x02\x01\x00" )
198 // this is just a sanity check.
199 throw new Exception( "XMP does not have magic trailer!" );
202 // strip out trailer.
203 $xmp = substr( $xmp, 0, -257 );
206 // unrecognized extension block
207 fseek( $fh, -($blockLength +
1), SEEK_CUR
);
208 self
::skipBlock( $fh );
212 self
::skipBlock( $fh );
214 } elseif ( $buf == self
::$gif_term ) {
217 if ( strlen( $buf ) < 1 ) throw new Exception( "Ran out of input" );
218 $byte = unpack( 'C', $buf );
220 throw new Exception( "At position: ".ftell($fh). ", Unknown byte ".$byte );
225 'frameCount' => $frameCount,
226 'looped' => $isLooped,
227 'duration' => $duration,
229 'comment' => $comment,
238 static function readGCT( $fh, $bpp ) {
240 for( $i=1; $i<=pow( 2, $bpp ); ++
$i ) {
250 static function decodeBPP( $data ) {
251 if ( strlen( $data ) < 1 ) throw new Exception( "Ran out of input" );
252 $buf = unpack( 'C', $data );
254 $bpp = ( $buf & 7 ) +
1;
257 $have_map = $buf & 1;
259 return $have_map ?
$bpp : 0;
266 static function skipBlock( $fh ) {
267 while ( !feof( $fh ) ) {
268 $buf = fread( $fh, 1 );
269 if ( strlen( $buf ) < 1 ) throw new Exception( "Ran out of input" );
270 $block_len = unpack( 'C', $buf );
271 $block_len = $block_len[1];
272 if ($block_len == 0) {
275 fread( $fh, $block_len );
279 * Read a block. In the GIF format, a block is made up of
280 * several sub-blocks. Each sub block starts with one byte
281 * saying how long the sub-block is, followed by the sub-block.
282 * The entire block is terminated by a sub-block of length
284 * @param $fh FileHandle
285 * @param $includeLengths Boolean Include the length bytes of the
286 * sub-blocks in the returned value. Normally this is false,
287 * except XMP is weird and does a hack where you need to keep
288 * these length bytes.
289 * @return string The data.
291 static function readBlock( $fh, $includeLengths = false ) {
293 $subLength = fread( $fh, 1 );
296 while( $subLength !== "\0" ) {
298 if ( $blocks > self
::MAX_SUBBLOCKS
) {
299 throw new Exception( "MAX_SUBBLOCKS exceeded (over $blocks sub-blocks)" );
302 throw new Exception( "Read error: Unexpected EOF." );
304 if ( $includeLengths ) {
308 $data .= fread( $fh, ord( $subLength ) );
309 $subLength = fread( $fh, 1 );