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