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
{
35 static $gif_frame_sep;
37 static $gif_extension_sep;
43 // Each sub-block is less than or equal to 255 bytes.
44 // Most of the time its 255 bytes, except for in XMP
45 // blocks, where it's usually between 32-127 bytes each.
46 const MAX_SUBBLOCKS
= 262144; // 5mb divided by 20.
50 * @param $filename string
53 static function getMetadata( $filename ) {
54 self
::$gif_frame_sep = pack( "C", ord( "," ) );
55 self
::$gif_extension_sep = pack( "C", ord( "!" ) );
56 self
::$gif_term = pack( "C", ord( ";" ) );
65 throw new Exception( "No file name specified" );
66 } elseif ( !file_exists( $filename ) ||
is_dir( $filename ) ) {
67 throw new Exception( "File $filename does not exist" );
70 $fh = fopen( $filename, 'rb' );
73 throw new Exception( "Unable to open file $filename" );
76 // Check for the GIF header
77 $buf = fread( $fh, 6 );
78 if ( !( $buf == 'GIF87a' ||
$buf == 'GIF89a' ) ) {
79 throw new Exception( "Not a valid GIF file; header: $buf" );
82 // Skip over width and height.
86 $buf = fread( $fh, 1 );
87 $bpp = self
::decodeBPP( $buf );
89 // Skip over background and aspect ratio
93 self
::readGCT( $fh, $bpp );
95 while ( !feof( $fh ) ) {
96 $buf = fread( $fh, 1 );
98 if ( $buf == self
::$gif_frame_sep ) {
106 $buf = fread( $fh, 1 );
107 $bpp = self
::decodeBPP( $buf );
110 self
::readGCT( $fh, $bpp );
112 self
::skipBlock( $fh );
113 } elseif ( $buf == self
::$gif_extension_sep ) {
114 $buf = fread( $fh, 1 );
115 if ( strlen( $buf ) < 1 ) {
116 throw new Exception( "Ran out of input" );
118 $extension_code = unpack( 'C', $buf );
119 $extension_code = $extension_code[1];
121 if ( $extension_code == 0xF9 ) {
122 // Graphics Control Extension.
123 fread( $fh, 1 ); // Block size
125 fread( $fh, 1 ); // Transparency, disposal method, user input
127 $buf = fread( $fh, 2 ); // Delay, in hundredths of seconds.
128 if ( strlen( $buf ) < 2 ) {
129 throw new Exception( "Ran out of input" );
131 $delay = unpack( 'v', $buf );
133 $duration +
= $delay * 0.01;
135 fread( $fh, 1 ); // Transparent colour index
137 $term = fread( $fh, 1 ); // Should be a terminator
138 if ( strlen( $term ) < 1 ) {
139 throw new Exception( "Ran out of input" );
141 $term = unpack( 'C', $term );
144 throw new Exception( "Malformed Graphics Control Extension block" );
146 } elseif ( $extension_code == 0xFE ) {
148 $data = self
::readBlock( $fh );
149 if ( $data === "" ) {
150 throw new Exception( 'Read error, zero-length comment block' );
153 // The standard says this should be ASCII, however its unclear if
154 // thats true in practise. Check to see if its valid utf-8, if so
155 // assume its that, otherwise assume its windows-1252 (iso-8859-1)
157 // quickIsNFCVerify has the side effect of replacing any invalid characters
158 UtfNormal
::quickIsNFCVerify( $dataCopy );
160 if ( $dataCopy !== $data ) {
161 wfSuppressWarnings();
162 $data = iconv( 'windows-1252', 'UTF-8', $data );
166 $commentCount = count( $comment );
167 if ( $commentCount === 0
168 ||
$comment[$commentCount - 1] !== $data
170 // Some applications repeat the same comment on each
171 // frame of an animated GIF image, so if this comment
172 // is identical to the last, only extract once.
175 } elseif ( $extension_code == 0xFF ) {
176 // Application extension (Netscape info about the animated gif)
177 // or XMP (or theoretically any other type of extension block)
178 $blockLength = fread( $fh, 1 );
179 if ( strlen( $blockLength ) < 1 ) {
180 throw new Exception( "Ran out of input" );
182 $blockLength = unpack( 'C', $blockLength );
183 $blockLength = $blockLength[1];
184 $data = fread( $fh, $blockLength );
186 if ( $blockLength != 11 ) {
187 wfDebug( __METHOD__
. " GIF application block with wrong length\n" );
188 fseek( $fh, -( $blockLength +
1 ), SEEK_CUR
);
189 self
::skipBlock( $fh );
193 // NETSCAPE2.0 (application name for animated gif)
194 if ( $data == 'NETSCAPE2.0' ) {
195 $data = fread( $fh, 2 ); // Block length and introduction, should be 03 01
197 if ( $data != "\x03\x01" ) {
198 throw new Exception( "Expected \x03\x01, got $data" );
201 // Unsigned little-endian integer, loop count or zero for "forever"
202 $loopData = fread( $fh, 2 );
203 if ( strlen( $loopData ) < 2 ) {
204 throw new Exception( "Ran out of input" );
206 $loopData = unpack( 'v', $loopData );
207 $loopCount = $loopData[1];
209 if ( $loopCount != 1 ) {
213 // Read out terminator byte
215 } elseif ( $data == 'XMP DataXMP' ) {
216 // application name for XMP data.
217 // see pg 18 of XMP spec part 3.
219 $xmp = self
::readBlock( $fh, true );
221 if ( substr( $xmp, -257, 3 ) !== "\x01\xFF\xFE"
222 ||
substr( $xmp, -4 ) !== "\x03\x02\x01\x00"
224 // this is just a sanity check.
225 throw new Exception( "XMP does not have magic trailer!" );
228 // strip out trailer.
229 $xmp = substr( $xmp, 0, -257 );
231 // unrecognized extension block
232 fseek( $fh, -( $blockLength +
1 ), SEEK_CUR
);
233 self
::skipBlock( $fh );
237 self
::skipBlock( $fh );
239 } elseif ( $buf == self
::$gif_term ) {
242 if ( strlen( $buf ) < 1 ) {
243 throw new Exception( "Ran out of input" );
245 $byte = unpack( 'C', $buf );
247 throw new Exception( "At position: " . ftell( $fh ) . ", Unknown byte " . $byte );
252 'frameCount' => $frameCount,
253 'looped' => $isLooped,
254 'duration' => $duration,
256 'comment' => $comment,
265 static function readGCT( $fh, $bpp ) {
267 for ( $i = 1; $i <= pow( 2, $bpp ); ++
$i ) {
278 static function decodeBPP( $data ) {
279 if ( strlen( $data ) < 1 ) {
280 throw new Exception( "Ran out of input" );
282 $buf = unpack( 'C', $data );
284 $bpp = ( $buf & 7 ) +
1;
287 $have_map = $buf & 1;
289 return $have_map ?
$bpp : 0;
296 static function skipBlock( $fh ) {
297 while ( !feof( $fh ) ) {
298 $buf = fread( $fh, 1 );
299 if ( strlen( $buf ) < 1 ) {
300 throw new Exception( "Ran out of input" );
302 $block_len = unpack( 'C', $buf );
303 $block_len = $block_len[1];
304 if ( $block_len == 0 ) {
307 fread( $fh, $block_len );
312 * Read a block. In the GIF format, a block is made up of
313 * several sub-blocks. Each sub block starts with one byte
314 * saying how long the sub-block is, followed by the sub-block.
315 * The entire block is terminated by a sub-block of length
317 * @param $fh FileHandle
318 * @param $includeLengths Boolean Include the length bytes of the
319 * sub-blocks in the returned value. Normally this is false,
320 * except XMP is weird and does a hack where you need to keep
321 * these length bytes.
323 * @return string The data.
325 static function readBlock( $fh, $includeLengths = false ) {
327 $subLength = fread( $fh, 1 );
330 while ( $subLength !== "\0" ) {
332 if ( $blocks > self
::MAX_SUBBLOCKS
) {
333 throw new Exception( "MAX_SUBBLOCKS exceeded (over $blocks sub-blocks)" );
336 throw new Exception( "Read error: Unexpected EOF." );
338 if ( $includeLengths ) {
342 $data .= fread( $fh, ord( $subLength ) );
343 $subLength = fread( $fh, 1 );