MediaWiki  master
GIFHandler.php
Go to the documentation of this file.
1 <?php
29 class GIFHandler extends BitmapHandler {
33  private const BROKEN_FILE = '0';
34 
35  public function getMetadata( $image, $filename ) {
36  try {
37  $parsedGIFMetadata = BitmapMetadataHandler::GIF( $filename );
38  } catch ( Exception $e ) {
39  // Broken file?
40  wfDebug( __METHOD__ . ': ' . $e->getMessage() );
41 
42  return self::BROKEN_FILE;
43  }
44 
45  return serialize( $parsedGIFMetadata );
46  }
47 
53  public function formatMetadata( $image, $context = false ) {
54  $meta = $this->getCommonMetaArray( $image );
55  if ( count( $meta ) === 0 ) {
56  return false;
57  }
58 
59  return $this->formatMetadataHelper( $meta, $context );
60  }
61 
67  public function getCommonMetaArray( File $image ) {
68  $meta = $image->getMetadata();
69 
70  if ( !$meta ) {
71  return [];
72  }
73  $meta = unserialize( $meta );
74  if ( !isset( $meta['metadata'] ) ) {
75  return [];
76  }
77  unset( $meta['metadata']['_MW_GIF_VERSION'] );
78 
79  return $meta['metadata'];
80  }
81 
88  public function getImageArea( $image ) {
89  $ser = $image->getMetadata();
90  if ( $ser ) {
91  $metadata = unserialize( $ser );
92  if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 0 ) {
93  return $image->getWidth() * $image->getHeight() * $metadata['frameCount'];
94  } else {
95  return $image->getWidth() * $image->getHeight();
96  }
97  } else {
98  return $image->getWidth() * $image->getHeight();
99  }
100  }
101 
106  public function isAnimatedImage( $image ) {
107  $ser = $image->getMetadata();
108  if ( $ser ) {
109  $metadata = unserialize( $ser );
110  if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 1 ) {
111  return true;
112  }
113  }
114 
115  return false;
116  }
117 
123  public function canAnimateThumbnail( $file ) {
124  global $wgMaxAnimatedGifArea;
125 
126  return $this->getImageArea( $file ) <= $wgMaxAnimatedGifArea;
127  }
128 
129  public function getMetadataType( $image ) {
130  return 'parsed-gif';
131  }
132 
133  public function isMetadataValid( $image, $metadata ) {
134  if ( $metadata === self::BROKEN_FILE ) {
135  // Do not repetitivly regenerate metadata on broken file.
136  return self::METADATA_GOOD;
137  }
138 
139  Wikimedia\suppressWarnings();
140  $data = unserialize( $metadata );
141  Wikimedia\restoreWarnings();
142 
143  if ( !$data || !is_array( $data ) ) {
144  wfDebug( __METHOD__ . " invalid GIF metadata" );
145 
146  return self::METADATA_BAD;
147  }
148 
149  if ( !isset( $data['metadata']['_MW_GIF_VERSION'] )
150  || $data['metadata']['_MW_GIF_VERSION'] != GIFMetadataExtractor::VERSION
151  ) {
152  wfDebug( __METHOD__ . " old but compatible GIF metadata" );
153 
155  }
156 
157  return self::METADATA_GOOD;
158  }
159 
164  public function getLongDesc( $image ) {
165  global $wgLang;
166 
167  $original = parent::getLongDesc( $image );
168 
169  Wikimedia\suppressWarnings();
170  $metadata = unserialize( $image->getMetadata() );
171  Wikimedia\restoreWarnings();
172 
173  if ( !$metadata || $metadata['frameCount'] <= 1 ) {
174  return $original;
175  }
176 
177  /* Preserve original image info string, but strip the last char ')' so we can add even more */
178  $info = [];
179  $info[] = $original;
180 
181  if ( $metadata['looped'] ) {
182  $info[] = wfMessage( 'file-info-gif-looped' )->parse();
183  }
184 
185  if ( $metadata['frameCount'] > 1 ) {
186  $info[] = wfMessage( 'file-info-gif-frames' )->numParams( $metadata['frameCount'] )->parse();
187  }
188 
189  if ( $metadata['duration'] ) {
190  $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
191  }
192 
193  return $wgLang->commaList( $info );
194  }
195 
204  public function getLength( $file ) {
205  $serMeta = $file->getMetadata();
206  Wikimedia\suppressWarnings();
207  $metadata = unserialize( $serMeta );
208  Wikimedia\restoreWarnings();
209 
210  if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) {
211  return 0.0;
212  } else {
213  return (float)$metadata['duration'];
214  }
215  }
216 }
GIFHandler\canAnimateThumbnail
canAnimateThumbnail( $file)
We cannot animate thumbnails that are bigger than a particular size.
Definition: GIFHandler.php:123
File\getMetadata
getMetadata()
Get handler-specific metadata Overridden by LocalFile, UnregisteredLocalFile STUB Stable to override.
Definition: File.php:722
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
GIFHandler\getMetadata
getMetadata( $image, $filename)
Get handler-specific metadata which will be saved in the img_metadata field.
Definition: GIFHandler.php:35
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1219
BitmapMetadataHandler\GIF
static GIF( $filename)
function for gif images.
Definition: BitmapMetadataHandler.php:237
GIFHandler
Handler for GIF images.
Definition: GIFHandler.php:29
serialize
serialize()
Definition: ApiMessageTrait.php:138
GIFHandler\getLongDesc
getLongDesc( $image)
Definition: GIFHandler.php:164
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:63
BitmapHandler
Generic handler for bitmap images.
Definition: BitmapHandler.php:32
$wgLang
$wgLang
Definition: Setup.php:776
GIFHandler\isAnimatedImage
isAnimatedImage( $image)
Definition: GIFHandler.php:106
GIFHandler\getCommonMetaArray
getCommonMetaArray(File $image)
Return the standard metadata elements for #filemetadata parser func.
Definition: GIFHandler.php:67
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:909
GIFHandler\getImageArea
getImageArea( $image)
Definition: GIFHandler.php:88
$wgMaxAnimatedGifArea
$wgMaxAnimatedGifArea
Force thumbnailing of animated GIFs above this size to a single frame instead of an animated thumbnai...
Definition: DefaultSettings.php:1344
GIFHandler\isMetadataValid
isMetadataValid( $image, $metadata)
Check if the metadata string is valid for this handler.
Definition: GIFHandler.php:133
GIFHandler\BROKEN_FILE
const BROKEN_FILE
Value to store in img_metadata if there was error extracting metadata.
Definition: GIFHandler.php:33
unserialize
unserialize( $serialized)
Definition: ApiMessageTrait.php:146
GIFHandler\getLength
getLength( $file)
Return the duration of the GIF file.
Definition: GIFHandler.php:204
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:543
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:53