a3d3ced39a1e26f8b2deb0cf6ebd27110bd370fd
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, 'r' );
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 $extension_code = unpack( 'C', $buf );
99 $extension_code = $extension_code[1];
101 if ($extension_code == 0xF9) {
102 // Graphics Control Extension.
103 fread( $fh, 1 ); // Block size
105 fread( $fh, 1 ); // Transparency, disposal method, user input
107 $buf = fread( $fh, 2 ); // Delay, in hundredths of seconds.
108 $delay = unpack( 'v', $buf );
110 $duration +
= $delay * 0.01;
112 fread( $fh, 1 ); // Transparent colour index
114 $term = fread( $fh, 1 ); // Should be a terminator
115 $term = unpack( 'C', $term );
118 throw new Exception( "Malformed Graphics Control Extension block" );
120 } elseif ($extension_code == 0xFE) {
122 $data = self
::readBlock( $fh );
123 if ( $data === "" ) {
124 throw new Exception( 'Read error, zero-length comment block' );
127 // The standard says this should be ASCII, however its unclear if
128 // thats true in practise. Check to see if its valid utf-8, if so
129 // assume its that, otherwise assume its iso-8859-1
131 // quickIsNFCVerify has the side effect of replacing any invalid characters
132 UtfNormal
::quickIsNFCVerify( $dataCopy );
134 if ( $dataCopy !== $data ) {
135 wfSuppressWarnings();
136 $data = iconv( 'ISO-8859-1', 'UTF-8', $data );
140 $commentCount = count( $comment );
141 if ( $commentCount === 0
142 ||
$comment[$commentCount-1] !== $data )
144 // Some applications repeat the same comment on each
145 // frame of an animated GIF image, so if this comment
146 // is identical to the last, only extract once.
149 } elseif ($extension_code == 0xFF) {
150 // Application extension (Netscape info about the animated gif)
151 // or XMP (or theoretically any other type of extension block)
152 $blockLength = fread( $fh, 1 );
153 $blockLength = unpack( 'C', $blockLength );
154 $blockLength = $blockLength[1];
155 $data = fread( $fh, $blockLength );
157 if ($blockLength != 11 ) {
158 wfDebug( __METHOD__
. ' GIF application block with wrong length' );
159 fseek( $fh, -($blockLength +
1), SEEK_CUR
);
160 self
::skipBlock( $fh );
164 // NETSCAPE2.0 (application name for animated gif)
165 if ( $data == 'NETSCAPE2.0' ) {
167 $data = fread( $fh, 2 ); // Block length and introduction, should be 03 01
169 if ($data != "\x03\x01") {
170 throw new Exception( "Expected \x03\x01, got $data" );
173 // Unsigned little-endian integer, loop count or zero for "forever"
174 $loopData = fread( $fh, 2 );
175 $loopData = unpack( 'v', $loopData );
176 $loopCount = $loopData[1];
178 if ($loopCount != 1) {
182 // Read out terminator byte
184 } elseif ( $data == 'XMP DataXMP' ) {
185 // application name for XMP data.
186 // see pg 18 of XMP spec part 3.
188 $xmp = self
::readBlock( $fh, true );
190 if ( substr( $xmp, -257, 3 ) !== "\x01\xFF\xFE"
191 ||
substr( $xmp, -4 ) !== "\x03\x02\x01\x00" )
193 // this is just a sanity check.
194 throw new Exception( "XMP does not have magic trailer!" );
197 // strip out trailer.
198 $xmp = substr( $xmp, 0, -257 );
201 // unrecognized extension block
202 fseek( $fh, -($blockLength +
1), SEEK_CUR
);
203 self
::skipBlock( $fh );
207 self
::skipBlock( $fh );
209 } elseif ( $buf == self
::$gif_term ) {
212 $byte = unpack( 'C', $buf );
214 throw new Exception( "At position: ".ftell($fh). ", Unknown byte ".$byte );
219 'frameCount' => $frameCount,
220 'looped' => $isLooped,
221 'duration' => $duration,
223 'comment' => $comment,
232 static function readGCT( $fh, $bpp ) {
234 for( $i=1; $i<=pow( 2, $bpp ); ++
$i ) {
244 static function decodeBPP( $data ) {
245 $buf = unpack( 'C', $data );
247 $bpp = ( $buf & 7 ) +
1;
250 $have_map = $buf & 1;
252 return $have_map ?
$bpp : 0;
259 static function skipBlock( $fh ) {
260 while ( !feof( $fh ) ) {
261 $buf = fread( $fh, 1 );
262 $block_len = unpack( 'C', $buf );
263 $block_len = $block_len[1];
264 if ($block_len == 0) {
267 fread( $fh, $block_len );
271 * Read a block. In the GIF format, a block is made up of
272 * several sub-blocks. Each sub block starts with one byte
273 * saying how long the sub-block is, followed by the sub-block.
274 * The entire block is terminated by a sub-block of length
276 * @param $fh FileHandle
277 * @param $includeLengths Boolean Include the length bytes of the
278 * sub-blocks in the returned value. Normally this is false,
279 * except XMP is weird and does a hack where you need to keep
280 * these length bytes.
283 static function readBlock( $fh, $includeLengths = false ) {
285 $subLength = fread( $fh, 1 );
288 while( $subLength !== "\0" ) {
290 if ( $blocks > self
::MAX_SUBBLOCKS
) {
291 throw new Exception( "MAX_SUBBLOCKS exceeded (over $blocks sub-blocks)" );
294 throw new Exception( "Read error: Unexpected EOF." );
296 if ( $includeLengths ) {
300 $data .= fread( $fh, ord( $subLength ) );
301 $subLength = fread( $fh, 1 );