MediaWiki master
GIFHandler.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\Media;
11
12use Exception;
17use Wikimedia\RequestTimeout\TimeoutException;
18
28 private const BROKEN_FILE = '0';
29
31 public function getSizeAndMetadata( $state, $filename ) {
32 try {
33 $parsedGIFMetadata = BitmapMetadataHandler::GIF( $filename );
34 } catch ( TimeoutException $e ) {
35 throw $e;
36 } catch ( Exception $e ) {
37 // Broken file?
38 wfDebug( __METHOD__ . ': ' . $e->getMessage() );
39
40 return [ 'metadata' => [ '_error' => self::BROKEN_FILE ] ];
41 }
42
43 return [
44 'width' => $parsedGIFMetadata['width'],
45 'height' => $parsedGIFMetadata['height'],
46 'bits' => $parsedGIFMetadata['bits'],
47 'metadata' => array_diff_key(
48 $parsedGIFMetadata,
49 [ 'width' => true, 'height' => true, 'bits' => true ]
50 )
51 ];
52 }
53
59 public function formatMetadata( $image, $context = false ) {
60 $meta = $this->getCommonMetaArray( $image );
61 if ( !$meta ) {
62 return false;
63 }
64
65 return $this->formatMetadataHelper( $meta, $context );
66 }
67
73 public function getCommonMetaArray( File $image ) {
74 $meta = $image->getMetadataArray();
75 if ( !isset( $meta['metadata'] ) ) {
76 return [];
77 }
78 unset( $meta['metadata']['_MW_GIF_VERSION'] );
79
80 return $meta['metadata'];
81 }
82
89 public function getImageArea( $image ) {
90 $metadata = $image->getMetadataArray();
91 if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 0 ) {
92 return $image->getWidth() * $image->getHeight() * $metadata['frameCount'];
93 }
94 return $image->getWidth() * $image->getHeight();
95 }
96
101 public function isAnimatedImage( $image ) {
102 $metadata = $image->getMetadataArray();
103 if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 1 ) {
104 return true;
105 }
106
107 return false;
108 }
109
115 public function canAnimateThumbnail( $file ) {
116 $maxAnimatedGifArea = MediaWikiServices::getInstance()->getMainConfig()
118
119 return $this->getImageArea( $file ) <= $maxAnimatedGifArea;
120 }
121
123 public function getMetadataType( $image ) {
124 return 'parsed-gif';
125 }
126
128 public function isFileMetadataValid( $image ) {
129 $data = $image->getMetadataArray();
130 if ( $data === [ '_error' => self::BROKEN_FILE ] ) {
131 // Do not repetitively regenerate metadata on broken file.
132 return self::METADATA_GOOD;
133 }
134
135 if ( !$data || isset( $data['_error'] ) ) {
136 wfDebug( __METHOD__ . " invalid GIF metadata" );
137
138 return self::METADATA_BAD;
139 }
140
141 if ( !isset( $data['metadata']['_MW_GIF_VERSION'] )
142 || $data['metadata']['_MW_GIF_VERSION'] !== GIFMetadataExtractor::VERSION
143 ) {
144 wfDebug( __METHOD__ . " old but compatible GIF metadata" );
145
147 }
148
149 return self::METADATA_GOOD;
150 }
151
156 public function getLongDesc( $image ) {
157 global $wgLang;
158
159 $original = parent::getLongDesc( $image );
160
161 $metadata = $image->getMetadataArray();
162
163 if ( !$metadata || isset( $metadata['_error'] ) || $metadata['frameCount'] <= 0 ) {
164 return $original;
165 }
166
167 /* Preserve original image info string, but strip the last char ')' so we can add even more */
168 $info = [];
169 $info[] = $original;
170
171 if ( $metadata['looped'] ) {
172 $info[] = wfMessage( 'file-info-gif-looped' )->parse();
173 }
174
175 if ( $metadata['frameCount'] > 1 ) {
176 $info[] = wfMessage( 'file-info-gif-frames' )->numParams( $metadata['frameCount'] )->parse();
177 }
178
179 if ( $metadata['duration'] ) {
180 $info[] = htmlspecialchars( $wgLang->formatTimePeriod( $metadata['duration'] ), ENT_QUOTES );
181 }
182
183 return $wgLang->commaList( $info );
184 }
185
194 public function getLength( $file ) {
195 $metadata = $file->getMetadataArray();
196
197 if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) {
198 return 0.0;
199 }
200 return (float)$metadata['duration'];
201 }
202}
203
205class_alias( GIFHandler::class, 'GIFHandler' );
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(MW_ENTRY_POINT==='index') if(!defined( 'MW_NO_SESSION') &&MW_ENTRY_POINT !=='cli' $wgLang
Definition Setup.php:551
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:79
getMetadataArray()
Get the unserialized handler-specific metadata STUB.
Definition File.php:828
A class containing constants representing the names of configuration variables.
const MaxAnimatedGifArea
Name constant for the MaxAnimatedGifArea setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Generic handler for bitmap images.
static GIF( $filename)
function for gif images.
Handler for GIF images.
getSizeAndMetadata( $state, $filename)
Get image size information and metadata array.If this returns null, the caller will fall back to getI...
getCommonMetaArray(File $image)
Return the standard metadata elements for #filemetadata parser func.
isFileMetadataValid( $image)
Check if the metadata is valid for this handler.If it returns MediaHandler::METADATA_BAD (or false),...
getMetadataType( $image)
Get a string describing the type of metadata, for display purposes.to overrideThis method is currentl...
formatMetadata( $image, $context=false)
getLength( $file)
Return the duration of the GIF file.
canAnimateThumbnail( $file)
We cannot animate thumbnails that are bigger than a particular size.
formatMetadataHelper( $metadataArray, $context=false)
sorts the visible/invisible field.
Interface for objects which can provide a MediaWiki context on request.