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