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
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
34 class GIFMetadataExtractor
{
36 private static $gifFrameSep;
39 private static $gifExtensionSep;
42 private static $gifTerm;
46 // Each sub-block is less than or equal to 255 bytes.
47 // Most of the time its 255 bytes, except for in XMP
48 // blocks, where it's usually between 32-127 bytes each.
49 const MAX_SUBBLOCKS
= 262144; // 5mb divided by 20.
53 * @param string $filename
56 static function getMetadata( $filename ) {
57 self
::$gifFrameSep = pack( "C", ord( "," ) );
58 self
::$gifExtensionSep = pack( "C", ord( "!" ) );
59 self
::$gifTerm = pack( "C", ord( ";" ) );
68 throw new Exception( "No file name specified" );
69 } elseif ( !file_exists( $filename ) ||
is_dir( $filename ) ) {
70 throw new Exception( "File $filename does not exist" );
73 $fh = fopen( $filename, 'rb' );
76 throw new Exception( "Unable to open file $filename" );
79 // Check for the GIF header
80 $buf = fread( $fh, 6 );
81 if ( !( $buf == 'GIF87a' ||
$buf == 'GIF89a' ) ) {
82 throw new Exception( "Not a valid GIF file; header: $buf" );
85 // Skip over width and height.
89 $buf = fread( $fh, 1 );
90 $bpp = self
::decodeBPP( $buf );
92 // Skip over background and aspect ratio
96 self
::readGCT( $fh, $bpp );
98 while ( !feof( $fh ) ) {
99 $buf = fread( $fh, 1 );
101 if ( $buf == self
::$gifFrameSep ) {
105 # # Skip bounding box
109 $buf = fread( $fh, 1 );
110 $bpp = self
::decodeBPP( $buf );
113 self
::readGCT( $fh, $bpp );
115 self
::skipBlock( $fh );
116 } elseif ( $buf == self
::$gifExtensionSep ) {
117 $buf = fread( $fh, 1 );
118 if ( strlen( $buf ) < 1 ) {
119 throw new Exception( "Ran out of input" );
121 $extension_code = unpack( 'C', $buf );
122 $extension_code = $extension_code[1];
124 if ( $extension_code == 0xF9 ) {
125 // Graphics Control Extension.
126 fread( $fh, 1 ); // Block size
128 fread( $fh, 1 ); // Transparency, disposal method, user input
130 $buf = fread( $fh, 2 ); // Delay, in hundredths of seconds.
131 if ( strlen( $buf ) < 2 ) {
132 throw new Exception( "Ran out of input" );
134 $delay = unpack( 'v', $buf );
136 $duration +
= $delay * 0.01;
138 fread( $fh, 1 ); // Transparent colour index
140 $term = fread( $fh, 1 ); // Should be a terminator
141 if ( strlen( $term ) < 1 ) {
142 throw new Exception( "Ran out of input" );
144 $term = unpack( 'C', $term );
147 throw new Exception( "Malformed Graphics Control Extension block" );
149 } elseif ( $extension_code == 0xFE ) {
151 $data = self
::readBlock( $fh );
152 if ( $data === "" ) {
153 throw new Exception( 'Read error, zero-length comment block' );
156 // The standard says this should be ASCII, however its unclear if
157 // thats true in practise. Check to see if its valid utf-8, if so
158 // assume its that, otherwise assume its windows-1252 (iso-8859-1)
160 // quickIsNFCVerify has the side effect of replacing any invalid characters
161 UtfNormal\Validator
::quickIsNFCVerify( $dataCopy );
163 if ( $dataCopy !== $data ) {
164 MediaWiki\
suppressWarnings();
165 $data = iconv( 'windows-1252', 'UTF-8', $data );
166 MediaWiki\restoreWarnings
();
169 $commentCount = count( $comment );
170 if ( $commentCount === 0
171 ||
$comment[$commentCount - 1] !== $data
173 // Some applications repeat the same comment on each
174 // frame of an animated GIF image, so if this comment
175 // is identical to the last, only extract once.
178 } elseif ( $extension_code == 0xFF ) {
179 // Application extension (Netscape info about the animated gif)
180 // or XMP (or theoretically any other type of extension block)
181 $blockLength = fread( $fh, 1 );
182 if ( strlen( $blockLength ) < 1 ) {
183 throw new Exception( "Ran out of input" );
185 $blockLength = unpack( 'C', $blockLength );
186 $blockLength = $blockLength[1];
187 $data = fread( $fh, $blockLength );
189 if ( $blockLength != 11 ) {
190 wfDebug( __METHOD__
. " GIF application block with wrong length\n" );
191 fseek( $fh, -( $blockLength +
1 ), SEEK_CUR
);
192 self
::skipBlock( $fh );
196 // NETSCAPE2.0 (application name for animated gif)
197 if ( $data == 'NETSCAPE2.0' ) {
198 $data = fread( $fh, 2 ); // Block length and introduction, should be 03 01
200 if ( $data != "\x03\x01" ) {
201 throw new Exception( "Expected \x03\x01, got $data" );
204 // Unsigned little-endian integer, loop count or zero for "forever"
205 $loopData = fread( $fh, 2 );
206 if ( strlen( $loopData ) < 2 ) {
207 throw new Exception( "Ran out of input" );
209 $loopData = unpack( 'v', $loopData );
210 $loopCount = $loopData[1];
212 if ( $loopCount != 1 ) {
216 // Read out terminator byte
218 } elseif ( $data == 'XMP DataXMP' ) {
219 // application name for XMP data.
220 // see pg 18 of XMP spec part 3.
222 $xmp = self
::readBlock( $fh, true );
224 if ( substr( $xmp, -257, 3 ) !== "\x01\xFF\xFE"
225 ||
substr( $xmp, -4 ) !== "\x03\x02\x01\x00"
227 // this is just a sanity check.
228 throw new Exception( "XMP does not have magic trailer!" );
231 // strip out trailer.
232 $xmp = substr( $xmp, 0, -257 );
234 // unrecognized extension block
235 fseek( $fh, -( $blockLength +
1 ), SEEK_CUR
);
236 self
::skipBlock( $fh );
240 self
::skipBlock( $fh );
242 } elseif ( $buf == self
::$gifTerm ) {
245 if ( strlen( $buf ) < 1 ) {
246 throw new Exception( "Ran out of input" );
248 $byte = unpack( 'C', $buf );
250 throw new Exception( "At position: " . ftell( $fh ) . ", Unknown byte " . $byte );
255 'frameCount' => $frameCount,
256 'looped' => $isLooped,
257 'duration' => $duration,
259 'comment' => $comment,
264 * @param resource $fh
268 static function readGCT( $fh, $bpp ) {
270 $max = pow( 2, $bpp );
271 for ( $i = 1; $i <= $max; ++
$i ) {
278 * @param string $data
282 static function decodeBPP( $data ) {
283 if ( strlen( $data ) < 1 ) {
284 throw new Exception( "Ran out of input" );
286 $buf = unpack( 'C', $data );
288 $bpp = ( $buf & 7 ) +
1;
291 $have_map = $buf & 1;
293 return $have_map ?
$bpp : 0;
297 * @param resource $fh
300 static function skipBlock( $fh ) {
301 while ( !feof( $fh ) ) {
302 $buf = fread( $fh, 1 );
303 if ( strlen( $buf ) < 1 ) {
304 throw new Exception( "Ran out of input" );
306 $block_len = unpack( 'C', $buf );
307 $block_len = $block_len[1];
308 if ( $block_len == 0 ) {
311 fread( $fh, $block_len );
316 * Read a block. In the GIF format, a block is made up of
317 * several sub-blocks. Each sub block starts with one byte
318 * saying how long the sub-block is, followed by the sub-block.
319 * The entire block is terminated by a sub-block of length
321 * @param resource $fh File handle
322 * @param bool $includeLengths Include the length bytes of the
323 * sub-blocks in the returned value. Normally this is false,
324 * except XMP is weird and does a hack where you need to keep
325 * these length bytes.
327 * @return string The data.
329 static function readBlock( $fh, $includeLengths = false ) {
331 $subLength = fread( $fh, 1 );
334 while ( $subLength !== "\0" ) {
336 if ( $blocks > self
::MAX_SUBBLOCKS
) {
337 throw new Exception( "MAX_SUBBLOCKS exceeded (over $blocks sub-blocks)" );
340 throw new Exception( "Read error: Unexpected EOF." );
342 if ( $includeLengths ) {
346 $data .= fread( $fh, ord( $subLength ) );
347 $subLength = fread( $fh, 1 );