MediaWiki  1.34.0
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 
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 }
GIFHandler\canAnimateThumbnail
canAnimateThumbnail( $file)
We cannot animate thumbnails that are bigger than a particular size.
Definition: GIFHandler.php:120
File\getMetadata
getMetadata()
Get handler-specific metadata Overridden by LocalFile, UnregisteredLocalFile STUB.
Definition: File.php:663
$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:32
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
BitmapMetadataHandler\GIF
static GIF( $filename)
function for gif images.
Definition: BitmapMetadataHandler.php:233
GIFHandler
Handler for GIF images.
Definition: GIFHandler.php:29
serialize
serialize()
Definition: ApiMessageTrait.php:138
GIFHandler\getLongDesc
getLongDesc( $image)
Definition: GIFHandler.php:162
MediaHandler\METADATA_COMPATIBLE
const METADATA_COMPATIBLE
Definition: MediaHandler.php:34
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:61
BitmapHandler
Generic handler for bitmap images.
Definition: BitmapHandler.php:31
$wgLang
$wgLang
Definition: Setup.php:881
GIFHandler\isAnimatedImage
isAnimatedImage( $image)
Definition: GIFHandler.php:103
GIFHandler\getCommonMetaArray
getCommonMetaArray(File $image)
Return the standard metadata elements for #filemetadata parser func.
Definition: GIFHandler.php:64
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:913
GIFHandler\getImageArea
getImageArea( $image)
Definition: GIFHandler.php:85
$wgMaxAnimatedGifArea
$wgMaxAnimatedGifArea
Force thumbnailing of animated GIFs above this size to a single frame instead of an animated thumbnai...
Definition: DefaultSettings.php:1265
GIFHandler\isMetadataValid
isMetadataValid( $image, $metadata)
Check if the metadata string is valid for this handler.
Definition: GIFHandler.php:131
GIFHandler\BROKEN_FILE
const BROKEN_FILE
Definition: GIFHandler.php:30
$context
$context
Definition: load.php:45
unserialize
unserialize( $serialized)
Definition: ApiMessageTrait.php:146
GIFHandler\getLength
getLength( $file)
Return the duration of the GIF file.
Definition: GIFHandler.php:202
GIFHandler\getMetadataType
getMetadataType( $image)
Get a string describing the type of metadata, for display purposes.
Definition: GIFHandler.php:127
MediaHandler\formatMetadataHelper
formatMetadataHelper( $metadataArray, $context=false)
sorts the visible/invisible field.
Definition: MediaHandler.php:494
MediaHandler\METADATA_BAD
const METADATA_BAD
Definition: MediaHandler.php:33
GIFMetadataExtractor\VERSION
const VERSION
Definition: GIFMetadataExtractor.php:44
MediaHandler\METADATA_GOOD
const METADATA_GOOD
Definition: MediaHandler.php:32
GIFHandler\formatMetadata
formatMetadata( $image, $context=false)
Definition: GIFHandler.php:50