MediaWiki  master
GIFHandler.php
Go to the documentation of this file.
1 <?php
29 class GIFHandler extends BitmapHandler {
30  const BROKEN_FILE = '0'; // value to store in img_metadata if error extracting metadata.
31 
32  public function getMetadata( $image, $filename ) {
33  try {
34  $parsedGIFMetadata = BitmapMetadataHandler::GIF( $filename );
35  } catch ( Exception $e ) {
36  // Broken file?
37  wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
38 
39  return self::BROKEN_FILE;
40  }
41 
42  return serialize( $parsedGIFMetadata );
43  }
44 
50  public function formatMetadata( $image, $context = false ) {
51  $meta = $this->getCommonMetaArray( $image );
52  if ( count( $meta ) === 0 ) {
53  return false;
54  }
55 
56  return $this->formatMetadataHelper( $meta, $context );
57  }
58 
64  public function getCommonMetaArray( File $image ) {
65  $meta = $image->getMetadata();
66 
67  if ( !$meta ) {
68  return [];
69  }
70  $meta = unserialize( $meta );
71  if ( !isset( $meta['metadata'] ) ) {
72  return [];
73  }
74  unset( $meta['metadata']['_MW_GIF_VERSION'] );
75 
76  return $meta['metadata'];
77  }
78 
85  function getImageArea( $image ) {
86  $ser = $image->getMetadata();
87  if ( $ser ) {
88  $metadata = unserialize( $ser );
89  if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 0 ) {
90  return $image->getWidth() * $image->getHeight() * $metadata['frameCount'];
91  } else {
92  return $image->getWidth() * $image->getHeight();
93  }
94  } else {
95  return $image->getWidth() * $image->getHeight();
96  }
97  }
98 
103  function isAnimatedImage( $image ) {
104  $ser = $image->getMetadata();
105  if ( $ser ) {
106  $metadata = unserialize( $ser );
107  if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 1 ) {
108  return true;
109  }
110  }
111 
112  return false;
113  }
114 
121  global $wgMaxAnimatedGifArea;
122  $answer = $this->getImageArea( $file ) <= $wgMaxAnimatedGifArea;
123 
124  return $answer;
125  }
126 
127  function getMetadataType( $image ) {
128  return 'parsed-gif';
129  }
130 
131  public function isMetadataValid( $image, $metadata ) {
132  if ( $metadata === self::BROKEN_FILE ) {
133  // Do not repetitivly regenerate metadata on broken file.
134  return self::METADATA_GOOD;
135  }
136 
137  Wikimedia\suppressWarnings();
138  $data = unserialize( $metadata );
139  Wikimedia\restoreWarnings();
140 
141  if ( !$data || !is_array( $data ) ) {
142  wfDebug( __METHOD__ . " invalid GIF metadata\n" );
143 
144  return self::METADATA_BAD;
145  }
146 
147  if ( !isset( $data['metadata']['_MW_GIF_VERSION'] )
148  || $data['metadata']['_MW_GIF_VERSION'] != GIFMetadataExtractor::VERSION
149  ) {
150  wfDebug( __METHOD__ . " old but compatible GIF metadata\n" );
151 
152  return self::METADATA_COMPATIBLE;
153  }
154 
155  return self::METADATA_GOOD;
156  }
157 
162  public function getLongDesc( $image ) {
163  global $wgLang;
164 
165  $original = parent::getLongDesc( $image );
166 
167  Wikimedia\suppressWarnings();
168  $metadata = unserialize( $image->getMetadata() );
169  Wikimedia\restoreWarnings();
170 
171  if ( !$metadata || $metadata['frameCount'] <= 1 ) {
172  return $original;
173  }
174 
175  /* Preserve original image info string, but strip the last char ')' so we can add even more */
176  $info = [];
177  $info[] = $original;
178 
179  if ( $metadata['looped'] ) {
180  $info[] = wfMessage( 'file-info-gif-looped' )->parse();
181  }
182 
183  if ( $metadata['frameCount'] > 1 ) {
184  $info[] = wfMessage( 'file-info-gif-frames' )->numParams( $metadata['frameCount'] )->parse();
185  }
186 
187  if ( $metadata['duration'] ) {
188  $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
189  }
190 
191  return $wgLang->commaList( $info );
192  }
193 
202  public function getLength( $file ) {
203  $serMeta = $file->getMetadata();
204  Wikimedia\suppressWarnings();
205  $metadata = unserialize( $serMeta );
206  Wikimedia\restoreWarnings();
207 
208  if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) {
209  return 0.0;
210  } else {
211  return (float)$metadata['duration'];
212  }
213  }
214 }
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
$context
Definition: load.php:45
static GIF( $filename)
function for gif images.
serialize()
getMetadata( $image, $filename)
Definition: GIFHandler.php:32
getLength( $file)
Return the duration of the GIF file.
Definition: GIFHandler.php:202
getCommonMetaArray(File $image)
Return the standard metadata elements for #filemetadata parser func.
Definition: GIFHandler.php:64
isAnimatedImage( $image)
Definition: GIFHandler.php:103
$wgLang
Definition: Setup.php:857
getImageArea( $image)
Definition: GIFHandler.php:85
getMetadata()
Get handler-specific metadata Overridden by LocalFile, UnregisteredLocalFile STUB.
Definition: File.php:663
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
isMetadataValid( $image, $metadata)
Definition: GIFHandler.php:131
unserialize( $serialized)
getLongDesc( $image)
Definition: GIFHandler.php:162
const BROKEN_FILE
Definition: GIFHandler.php:30
getMetadataType( $image)
Definition: GIFHandler.php:127
$wgMaxAnimatedGifArea
Force thumbnailing of animated GIFs above this size to a single frame instead of an animated thumbnai...
Handler for GIF images.
Definition: GIFHandler.php:29
Generic handler for bitmap images.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
canAnimateThumbnail( $file)
We cannot animate thumbnails that are bigger than a particular size.
Definition: GIFHandler.php:120
formatMetadataHelper( $metadataArray, $context=false)
sorts the visible/invisible field.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:61
formatMetadata( $image, $context=false)
Definition: GIFHandler.php:50