MediaWiki  master
GIFMetadataExtractor.php
Go to the documentation of this file.
1 <?php
36  private static $gifFrameSep;
37 
39  private static $gifExtensionSep;
40 
42  private static $gifTerm;
43 
44  public const VERSION = 1;
45 
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  private const MAX_SUBBLOCKS = 262144; // 5mb divided by 20.
50 
56  public static function getMetadata( $filename ) {
57  self::$gifFrameSep = pack( "C", ord( "," ) ); // 2C
58  self::$gifExtensionSep = pack( "C", ord( "!" ) ); // 21
59  self::$gifTerm = pack( "C", ord( ";" ) ); // 3B
60 
61  $frameCount = 0;
62  $duration = 0.0;
63  $isLooped = false;
64  $xmp = "";
65  $comment = [];
66 
67  if ( !$filename ) {
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" );
71  }
72 
73  $fh = fopen( $filename, 'rb' );
74 
75  if ( !$fh ) {
76  throw new Exception( "Unable to open file $filename" );
77  }
78 
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" );
83  }
84 
85  // Read width and height.
86  $buf = fread( $fh, 2 );
87  $width = unpack( 'v', $buf )[1];
88  $buf = fread( $fh, 2 );
89  $height = unpack( 'v', $buf )[1];
90 
91  // Read BPP
92  $buf = fread( $fh, 1 );
93  $bpp = self::decodeBPP( $buf );
94 
95  // Skip over background and aspect ratio
96  fread( $fh, 2 );
97 
98  // Skip over the GCT
99  self::readGCT( $fh, $bpp );
100 
101  while ( !feof( $fh ) ) {
102  $buf = fread( $fh, 1 );
103 
104  if ( $buf == self::$gifFrameSep ) {
105  // Found a frame
106  $frameCount++;
107 
108  # # Skip bounding box
109  fread( $fh, 8 );
110 
111  # # Read BPP
112  $buf = fread( $fh, 1 );
113  $bpp = self::decodeBPP( $buf );
114 
115  # # Read GCT
116  self::readGCT( $fh, $bpp );
117  fread( $fh, 1 );
118  self::skipBlock( $fh );
119  } elseif ( $buf == self::$gifExtensionSep ) {
120  $buf = fread( $fh, 1 );
121  if ( strlen( $buf ) < 1 ) {
122  throw new Exception( "Ran out of input" );
123  }
124  $extension_code = unpack( 'C', $buf )[1];
125 
126  if ( $extension_code == 0xF9 ) {
127  // Graphics Control Extension.
128  fread( $fh, 1 ); // Block size
129 
130  fread( $fh, 1 ); // Transparency, disposal method, user input
131 
132  $buf = fread( $fh, 2 ); // Delay, in hundredths of seconds.
133  if ( strlen( $buf ) < 2 ) {
134  throw new Exception( "Ran out of input" );
135  }
136  $delay = unpack( 'v', $buf )[1];
137  $duration += $delay * 0.01;
138 
139  fread( $fh, 1 ); // Transparent colour index
140 
141  $term = fread( $fh, 1 ); // Should be a terminator
142  if ( strlen( $term ) < 1 ) {
143  throw new Exception( "Ran out of input" );
144  }
145  $term = unpack( 'C', $term )[1];
146  if ( $term != 0 ) {
147  throw new Exception( "Malformed Graphics Control Extension block" );
148  }
149  } elseif ( $extension_code == 0xFE ) {
150  // Comment block(s).
151  $data = self::readBlock( $fh );
152  if ( $data === "" ) {
153  throw new Exception( 'Read error, zero-length comment block' );
154  }
155 
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)
159  $dataCopy = $data;
160  // quickIsNFCVerify has the side effect of replacing any invalid characters
161  UtfNormal\Validator::quickIsNFCVerify( $dataCopy );
162 
163  if ( $dataCopy !== $data ) {
164  Wikimedia\suppressWarnings();
165  $data = iconv( 'windows-1252', 'UTF-8', $data );
166  Wikimedia\restoreWarnings();
167  }
168 
169  $commentCount = count( $comment );
170  if ( $commentCount === 0
171  // @phan-suppress-next-line PhanTypeInvalidDimOffset
172  || $comment[$commentCount - 1] !== $data
173  ) {
174  // Some applications repeat the same comment on each
175  // frame of an animated GIF image, so if this comment
176  // is identical to the last, only extract once.
177  $comment[] = $data;
178  }
179  } elseif ( $extension_code == 0xFF ) {
180  // Application extension (Netscape info about the animated gif)
181  // or XMP (or theoretically any other type of extension block)
182  $blockLength = fread( $fh, 1 );
183  if ( strlen( $blockLength ) < 1 ) {
184  throw new Exception( "Ran out of input" );
185  }
186  $blockLength = unpack( 'C', $blockLength )[1];
187  $data = fread( $fh, $blockLength );
188 
189  if ( $blockLength != 11 ) {
190  wfDebug( __METHOD__ . " GIF application block with wrong length" );
191  fseek( $fh, -( $blockLength + 1 ), SEEK_CUR );
192  self::skipBlock( $fh );
193  continue;
194  }
195 
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
199 
200  if ( $data != "\x03\x01" ) {
201  throw new Exception( "Expected \x03\x01, got $data" );
202  }
203 
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" );
208  }
209  $loopCount = unpack( 'v', $loopData )[1];
210 
211  if ( $loopCount != 1 ) {
212  $isLooped = true;
213  }
214 
215  // Read out terminator byte
216  fread( $fh, 1 );
217  } elseif ( $data == 'XMP DataXMP' ) {
218  // application name for XMP data.
219  // see pg 18 of XMP spec part 3.
220 
221  $xmp = self::readBlock( $fh, true );
222 
223  if ( substr( $xmp, -257, 3 ) !== "\x01\xFF\xFE"
224  || substr( $xmp, -4 ) !== "\x03\x02\x01\x00"
225  ) {
226  // this is just a sanity check.
227  throw new Exception( "XMP does not have magic trailer!" );
228  }
229 
230  // strip out trailer.
231  $xmp = substr( $xmp, 0, -257 );
232  } else {
233  // unrecognized extension block
234  fseek( $fh, -( $blockLength + 1 ), SEEK_CUR );
235  self::skipBlock( $fh );
236  continue;
237  }
238  } else {
239  self::skipBlock( $fh );
240  }
241  } elseif ( $buf == self::$gifTerm ) {
242  break;
243  } else {
244  if ( strlen( $buf ) < 1 ) {
245  throw new Exception( "Ran out of input" );
246  }
247  $byte = unpack( 'C', $buf )[1];
248  throw new Exception( "At position: " . ftell( $fh ) . ", Unknown byte " . $byte );
249  }
250  }
251 
252  return [
253  'frameCount' => $frameCount,
254  'looped' => $isLooped,
255  'duration' => $duration,
256  'xmp' => $xmp,
257  'comment' => $comment,
258  ];
259  }
260 
266  private static function readGCT( $fh, $bpp ) {
267  if ( $bpp > 0 ) {
268  $max = 2 ** $bpp;
269  for ( $i = 1; $i <= $max; ++$i ) {
270  fread( $fh, 3 );
271  }
272  }
273  }
274 
280  private static function decodeBPP( $data ) {
281  if ( strlen( $data ) < 1 ) {
282  throw new Exception( "Ran out of input" );
283  }
284  $buf = unpack( 'C', $data )[1];
285  $bpp = ( $buf & 7 ) + 1;
286  // @phan-suppress-next-line PhanTypeInvalidLeftOperandOfIntegerOp
287  $buf >>= 7;
288 
289  $have_map = $buf & 1;
290 
291  return $have_map ? $bpp : 0;
292  }
293 
298  private static function skipBlock( $fh ) {
299  while ( !feof( $fh ) ) {
300  $buf = fread( $fh, 1 );
301  if ( strlen( $buf ) < 1 ) {
302  throw new Exception( "Ran out of input" );
303  }
304  $block_len = unpack( 'C', $buf )[1];
305  if ( $block_len == 0 ) {
306  return;
307  }
308  fread( $fh, $block_len );
309  }
310  }
311 
326  private static function readBlock( $fh, $includeLengths = false ) {
327  $data = '';
328  $subLength = fread( $fh, 1 );
329  $blocks = 0;
330 
331  while ( $subLength !== "\0" ) {
332  $blocks++;
333  if ( $blocks > self::MAX_SUBBLOCKS ) {
334  throw new Exception( "MAX_SUBBLOCKS exceeded (over $blocks sub-blocks)" );
335  }
336  if ( feof( $fh ) ) {
337  throw new Exception( "Read error: Unexpected EOF." );
338  }
339  if ( $includeLengths ) {
340  $data .= $subLength;
341  }
342 
343  $data .= fread( $fh, ord( $subLength ) );
344  $subLength = fread( $fh, 1 );
345  }
346 
347  return $data;
348  }
349 }
GIFMetadataExtractor
GIF frame counter.
Definition: GIFMetadataExtractor.php:34
GIFMetadataExtractor\MAX_SUBBLOCKS
const MAX_SUBBLOCKS
Definition: GIFMetadataExtractor.php:49
GIFMetadataExtractor\$gifFrameSep
static string $gifFrameSep
Definition: GIFMetadataExtractor.php:36
GIFMetadataExtractor\$gifTerm
static string $gifTerm
Definition: GIFMetadataExtractor.php:42
GIFMetadataExtractor\$gifExtensionSep
static string $gifExtensionSep
Definition: GIFMetadataExtractor.php:39
GIFMetadataExtractor\getMetadata
static getMetadata( $filename)
Definition: GIFMetadataExtractor.php:56
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:910
GIFMetadataExtractor\readBlock
static readBlock( $fh, $includeLengths=false)
Read a block.
Definition: GIFMetadataExtractor.php:326
GIFMetadataExtractor\decodeBPP
static decodeBPP( $data)
Definition: GIFMetadataExtractor.php:280
GIFMetadataExtractor\readGCT
static readGCT( $fh, $bpp)
Definition: GIFMetadataExtractor.php:266
GIFMetadataExtractor\skipBlock
static skipBlock( $fh)
Definition: GIFMetadataExtractor.php:298
GIFMetadataExtractor\VERSION
const VERSION
Definition: GIFMetadataExtractor.php:44