86d624b768848ba56988b77d5e14f06b691b8921
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.
31 static function getMetadata( $filename ) {
32 self
::$gif_frame_sep = pack( "C", ord("," ) );
33 self
::$gif_extension_sep = pack( "C", ord("!" ) );
34 self
::$gif_term = pack( "C", ord(";" ) );
43 throw new Exception( "No file name specified" );
44 } elseif ( !file_exists( $filename ) ||
is_dir( $filename ) ) {
45 throw new Exception( "File $filename does not exist" );
48 $fh = fopen( $filename, 'r' );
51 throw new Exception( "Unable to open file $filename" );
54 // Check for the GIF header
55 $buf = fread( $fh, 6 );
56 if ( !($buf == 'GIF87a' ||
$buf == 'GIF89a') ) {
57 throw new Exception( "Not a valid GIF file; header: $buf" );
60 // Skip over width and height.
64 $buf = fread( $fh, 1 );
65 $bpp = self
::decodeBPP( $buf );
67 // Skip over background and aspect ratio
71 self
::readGCT( $fh, $bpp );
73 while( !feof( $fh ) ) {
74 $buf = fread( $fh, 1 );
76 if ($buf == self
::$gif_frame_sep) {
84 $buf = fread( $fh, 1 );
85 $bpp = self
::decodeBPP( $buf );
88 self
::readGCT( $fh, $bpp );
90 self
::skipBlock( $fh );
91 } elseif ( $buf == self
::$gif_extension_sep ) {
92 $buf = fread( $fh, 1 );
93 $extension_code = unpack( 'C', $buf );
94 $extension_code = $extension_code[1];
96 if ($extension_code == 0xF9) {
97 // Graphics Control Extension.
98 fread( $fh, 1 ); // Block size
100 fread( $fh, 1 ); // Transparency, disposal method, user input
102 $buf = fread( $fh, 2 ); // Delay, in hundredths of seconds.
103 $delay = unpack( 'v', $buf );
105 $duration +
= $delay * 0.01;
107 fread( $fh, 1 ); // Transparent colour index
109 $term = fread( $fh, 1 ); // Should be a terminator
110 $term = unpack( 'C', $term );
113 throw new Exception( "Malformed Graphics Control Extension block" );
115 } elseif ($extension_code == 0xFE) {
117 $data = self
::readBlock( $fh );
118 if ( $data === "" ) {
119 throw new Exception( 'Read error, zero-length comment block' );
122 // The standard says this should be ASCII, however its unclear if
123 // thats true in practise. Check to see if its valid utf-8, if so
124 // assume its that, otherwise assume its iso-8859-1
126 // quickIsNFCVerify has the side effect of replacing any invalid characters
127 UtfNormal
::quickIsNFCVerify( $dataCopy );
129 if ( $dataCopy !== $data ) {
130 wfSuppressWarnings();
131 $data = iconv( 'ISO-8859-1', 'UTF-8', $data );
135 $commentCount = count( $comment );
136 if ( $commentCount === 0
137 ||
$comment[$commentCount-1] !== $data )
139 // Some applications repeat the same comment on each
140 // frame of an animated GIF image, so if this comment
141 // is identical to the last, only extract once.
144 } elseif ($extension_code == 0xFF) {
145 // Application extension (Netscape info about the animated gif)
146 // or XMP (or theoretically any other type of extension block)
147 $blockLength = fread( $fh, 1 );
148 $blockLength = unpack( 'C', $blockLength );
149 $blockLength = $blockLength[1];
150 $data = fread( $fh, $blockLength );
152 if ($blockLength != 11 ) {
153 wfDebug( __METHOD__
. ' GIF application block with wrong length' );
154 fseek( $fh, -($blockLength +
1), SEEK_CUR
);
155 self
::skipBlock( $fh );
159 // NETSCAPE2.0 (application name for animated gif)
160 if ( $data == 'NETSCAPE2.0' ) {
162 $data = fread( $fh, 2 ); // Block length and introduction, should be 03 01
164 if ($data != "\x03\x01") {
165 throw new Exception( "Expected \x03\x01, got $data" );
168 // Unsigned little-endian integer, loop count or zero for "forever"
169 $loopData = fread( $fh, 2 );
170 $loopData = unpack( 'v', $loopData );
171 $loopCount = $loopData[1];
173 if ($loopCount != 1) {
177 // Read out terminator byte
179 } elseif ( $data == 'XMP DataXMP' ) {
180 // application name for XMP data.
181 // see pg 18 of XMP spec part 3.
183 $xmp = self
::readBlock( $fh, true );
185 if ( substr( $xmp, -257, 3 ) !== "\x01\xFF\xFE"
186 ||
substr( $xmp, -4 ) !== "\x03\x02\x01\x00" )
188 // this is just a sanity check.
189 throw new Exception( "XMP does not have magic trailer!" );
192 // strip out trailer.
193 $xmp = substr( $xmp, 0, -257 );
196 // unrecognized extension block
197 fseek( $fh, -($blockLength +
1), SEEK_CUR
);
198 self
::skipBlock( $fh );
202 self
::skipBlock( $fh );
204 } elseif ( $buf == self
::$gif_term ) {
207 $byte = unpack( 'C', $buf );
209 throw new Exception( "At position: ".ftell($fh). ", Unknown byte ".$byte );
214 'frameCount' => $frameCount,
215 'looped' => $isLooped,
216 'duration' => $duration,
218 'comment' => $comment,
222 static function readGCT( $fh, $bpp ) {
224 for( $i=1; $i<=pow( 2, $bpp ); ++
$i ) {
230 static function decodeBPP( $data ) {
231 $buf = unpack( 'C', $data );
233 $bpp = ( $buf & 7 ) +
1;
236 $have_map = $buf & 1;
238 return $have_map ?
$bpp : 0;
241 static function skipBlock( $fh ) {
242 while ( !feof( $fh ) ) {
243 $buf = fread( $fh, 1 );
244 $block_len = unpack( 'C', $buf );
245 $block_len = $block_len[1];
246 if ($block_len == 0) {
249 fread( $fh, $block_len );
253 * Read a block. In the GIF format, a block is made up of
254 * several sub-blocks. Each sub block starts with one byte
255 * saying how long the sub-block is, followed by the sub-block.
256 * The entire block is terminated by a sub-block of length
258 * @param $fh FileHandle
259 * @param $includeLengths Boolean Include the length bytes of the
260 * sub-blocks in the returned value. Normally this is false,
261 * except XMP is weird and does a hack where you need to keep
262 * these length bytes.
265 static function readBlock( $fh, $includeLengths = false ) {
267 $subLength = fread( $fh, 1 );
270 while( $subLength !== "\0" ) {
272 if ( $blocks > self
::MAX_SUBBLOCKS
) {
273 throw new Exception( "MAX_SUBBLOCKS exceeded (over $blocks sub-blocks)" );
276 throw new Exception( "Read error: Unexpected EOF." );
278 if ( $includeLengths ) {
282 $data .= fread( $fh, ord( $subLength ) );
283 $subLength = fread( $fh, 1 );