MediaWiki  master
GIFHandler.php
Go to the documentation of this file.
1 <?php
26 use Wikimedia\RequestTimeout\TimeoutException;
27 
33 class GIFHandler extends BitmapHandler {
37  private const BROKEN_FILE = '0';
38 
39  public function getSizeAndMetadata( $state, $filename ) {
40  try {
41  $parsedGIFMetadata = BitmapMetadataHandler::GIF( $filename );
42  } catch ( TimeoutException $e ) {
43  throw $e;
44  } catch ( Exception $e ) {
45  // Broken file?
46  wfDebug( __METHOD__ . ': ' . $e->getMessage() );
47 
48  return [ 'metadata' => [ '_error' => self::BROKEN_FILE ] ];
49  }
50 
51  return [
52  'width' => $parsedGIFMetadata['width'],
53  'height' => $parsedGIFMetadata['height'],
54  'bits' => $parsedGIFMetadata['bits'],
55  'metadata' => array_diff_key(
56  $parsedGIFMetadata,
57  [ 'width' => true, 'height' => true, 'bits' => true ]
58  )
59  ];
60  }
61 
67  public function formatMetadata( $image, $context = false ) {
68  $meta = $this->getCommonMetaArray( $image );
69  if ( !$meta ) {
70  return false;
71  }
72 
73  return $this->formatMetadataHelper( $meta, $context );
74  }
75 
81  public function getCommonMetaArray( File $image ) {
82  $meta = $image->getMetadataArray();
83  if ( !isset( $meta['metadata'] ) ) {
84  return [];
85  }
86  unset( $meta['metadata']['_MW_GIF_VERSION'] );
87 
88  return $meta['metadata'];
89  }
90 
97  public function getImageArea( $image ) {
98  $metadata = $image->getMetadataArray();
99  if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 0 ) {
100  return $image->getWidth() * $image->getHeight() * $metadata['frameCount'];
101  } else {
102  return $image->getWidth() * $image->getHeight();
103  }
104  }
105 
110  public function isAnimatedImage( $image ) {
111  $metadata = $image->getMetadataArray();
112  if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 1 ) {
113  return true;
114  }
115 
116  return false;
117  }
118 
124  public function canAnimateThumbnail( $file ) {
125  $maxAnimatedGifArea = MediaWikiServices::getInstance()->getMainConfig()
126  ->get( MainConfigNames::MaxAnimatedGifArea );
127 
128  return $this->getImageArea( $file ) <= $maxAnimatedGifArea;
129  }
130 
131  public function getMetadataType( $image ) {
132  return 'parsed-gif';
133  }
134 
135  public function isFileMetadataValid( $image ) {
136  $data = $image->getMetadataArray();
137  if ( $data === [ '_error' => self::BROKEN_FILE ] ) {
138  // Do not repetitively regenerate metadata on broken file.
139  return self::METADATA_GOOD;
140  }
141 
142  if ( !$data || isset( $data['_error'] ) ) {
143  wfDebug( __METHOD__ . " invalid GIF metadata" );
144 
145  return self::METADATA_BAD;
146  }
147 
148  if ( !isset( $data['metadata']['_MW_GIF_VERSION'] )
149  || $data['metadata']['_MW_GIF_VERSION'] != GIFMetadataExtractor::VERSION
150  ) {
151  wfDebug( __METHOD__ . " old but compatible GIF metadata" );
152 
154  }
155 
156  return self::METADATA_GOOD;
157  }
158 
163  public function getLongDesc( $image ) {
164  global $wgLang;
165 
166  $original = parent::getLongDesc( $image );
167 
168  $metadata = $image->getMetadataArray();
169 
170  if ( !$metadata || isset( $metadata['_error'] ) || $metadata['frameCount'] <= 0 ) {
171  return $original;
172  }
173 
174  /* Preserve original image info string, but strip the last char ')' so we can add even more */
175  $info = [];
176  $info[] = $original;
177 
178  if ( $metadata['looped'] ) {
179  $info[] = wfMessage( 'file-info-gif-looped' )->parse();
180  }
181 
182  if ( $metadata['frameCount'] > 1 ) {
183  $info[] = wfMessage( 'file-info-gif-frames' )->numParams( $metadata['frameCount'] )->parse();
184  }
185 
186  if ( $metadata['duration'] ) {
187  $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
188  }
189 
190  return $wgLang->commaList( $info );
191  }
192 
201  public function getLength( $file ) {
202  $metadata = $file->getMetadataArray();
203 
204  if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) {
205  return 0.0;
206  } else {
207  return (float)$metadata['duration'];
208  }
209  }
210 }
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(!defined( 'MW_NO_SESSION') &&! $wgCommandLineMode) $wgLang
Definition: Setup.php:497
Generic handler for bitmap images.
static GIF( $filename)
function for gif images.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:68
getMetadataArray()
Get the unserialized handler-specific metadata STUB.
Definition: File.php:747
Handler for GIF images.
Definition: GIFHandler.php:33
isFileMetadataValid( $image)
Check if the metadata is valid for this handler.
Definition: GIFHandler.php:135
getImageArea( $image)
Definition: GIFHandler.php:97
getLength( $file)
Return the duration of the GIF file.
Definition: GIFHandler.php:201
getLongDesc( $image)
Definition: GIFHandler.php:163
isAnimatedImage( $image)
Definition: GIFHandler.php:110
canAnimateThumbnail( $file)
We cannot animate thumbnails that are bigger than a particular size.
Definition: GIFHandler.php:124
formatMetadata( $image, $context=false)
Definition: GIFHandler.php:67
getSizeAndMetadata( $state, $filename)
Get image size information and metadata array.
Definition: GIFHandler.php:39
getMetadataType( $image)
Get a string describing the type of metadata, for display purposes.
Definition: GIFHandler.php:131
getCommonMetaArray(File $image)
Return the standard metadata elements for #filemetadata parser func.
Definition: GIFHandler.php:81
const METADATA_COMPATIBLE
const METADATA_BAD
formatMetadataHelper( $metadataArray, $context=false)
sorts the visible/invisible field.
const METADATA_GOOD
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42