MediaWiki master
GIFHandler.php
Go to the documentation of this file.
1<?php
9
10namespace MediaWiki\Media;
11
12use Exception;
13use MediaWiki\Context\IContextSource;
14use MediaWiki\FileRepo\File\File;
15use MediaWiki\MainConfigNames;
16use MediaWiki\MediaWikiServices;
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'] ) || !is_array( $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
146 return self::METADATA_COMPATIBLE;
147 }
148
149 return self::METADATA_GOOD;
150 }
151
156 public function getLongDesc( $image ) {
157 $lang = $this->getLanguage();
158 $original = parent::getLongDesc( $image );
159
160 $metadata = $image->getMetadataArray();
161
162 if ( !$metadata || isset( $metadata['_error'] ) || $metadata['frameCount'] <= 0 ) {
163 return $original;
164 }
165
166 /* Preserve original image info string, but strip the last char ')' so we can add even more */
167 $info = [];
168 $info[] = $original;
169
170 if ( $metadata['looped'] ) {
171 $info[] = wfMessage( 'file-info-gif-looped' )->inLanguage( $lang )->parse();
172 }
173
174 if ( $metadata['frameCount'] > 1 ) {
175 $info[] = wfMessage( 'file-info-gif-frames' )->numParams( $metadata['frameCount'] )
176 ->inLanguage( $lang )->parse();
177 }
178
179 if ( $metadata['duration'] ) {
180 $info[] = htmlspecialchars( $lang->formatTimePeriod( $metadata['duration'] ), ENT_QUOTES );
181 }
182
183 return $lang->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.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:80
getMetadataArray()
Get the unserialized handler-specific metadata STUB.
Definition File.php:817
const MaxAnimatedGifArea
Name constant for the MaxAnimatedGifArea setting, for use with Config\get().
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.
getLanguage()
Get the language to be used by this media handler for text formatting.
formatMetadataHelper( $metadataArray, $context=false)
sorts the visible/invisible field.