MediaWiki master
GIFHandler.php
Go to the documentation of this file.
1<?php
14use Wikimedia\RequestTimeout\TimeoutException;
15
25 private const BROKEN_FILE = '0';
26
28 public function getSizeAndMetadata( $state, $filename ) {
29 try {
30 $parsedGIFMetadata = BitmapMetadataHandler::GIF( $filename );
31 } catch ( TimeoutException $e ) {
32 throw $e;
33 } catch ( Exception $e ) {
34 // Broken file?
35 wfDebug( __METHOD__ . ': ' . $e->getMessage() );
36
37 return [ 'metadata' => [ '_error' => self::BROKEN_FILE ] ];
38 }
39
40 return [
41 'width' => $parsedGIFMetadata['width'],
42 'height' => $parsedGIFMetadata['height'],
43 'bits' => $parsedGIFMetadata['bits'],
44 'metadata' => array_diff_key(
45 $parsedGIFMetadata,
46 [ 'width' => true, 'height' => true, 'bits' => true ]
47 )
48 ];
49 }
50
56 public function formatMetadata( $image, $context = false ) {
57 $meta = $this->getCommonMetaArray( $image );
58 if ( !$meta ) {
59 return false;
60 }
61
62 return $this->formatMetadataHelper( $meta, $context );
63 }
64
70 public function getCommonMetaArray( File $image ) {
71 $meta = $image->getMetadataArray();
72 if ( !isset( $meta['metadata'] ) ) {
73 return [];
74 }
75 unset( $meta['metadata']['_MW_GIF_VERSION'] );
76
77 return $meta['metadata'];
78 }
79
86 public function getImageArea( $image ) {
87 $metadata = $image->getMetadataArray();
88 if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 0 ) {
89 return $image->getWidth() * $image->getHeight() * $metadata['frameCount'];
90 }
91 return $image->getWidth() * $image->getHeight();
92 }
93
98 public function isAnimatedImage( $image ) {
99 $metadata = $image->getMetadataArray();
100 if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 1 ) {
101 return true;
102 }
103
104 return false;
105 }
106
112 public function canAnimateThumbnail( $file ) {
113 $maxAnimatedGifArea = MediaWikiServices::getInstance()->getMainConfig()
114 ->get( MainConfigNames::MaxAnimatedGifArea );
115
116 return $this->getImageArea( $file ) <= $maxAnimatedGifArea;
117 }
118
120 public function getMetadataType( $image ) {
121 return 'parsed-gif';
122 }
123
125 public function isFileMetadataValid( $image ) {
126 $data = $image->getMetadataArray();
127 if ( $data === [ '_error' => self::BROKEN_FILE ] ) {
128 // Do not repetitively regenerate metadata on broken file.
129 return self::METADATA_GOOD;
130 }
131
132 if ( !$data || isset( $data['_error'] ) ) {
133 wfDebug( __METHOD__ . " invalid GIF metadata" );
134
135 return self::METADATA_BAD;
136 }
137
138 if ( !isset( $data['metadata']['_MW_GIF_VERSION'] )
139 || $data['metadata']['_MW_GIF_VERSION'] !== GIFMetadataExtractor::VERSION
140 ) {
141 wfDebug( __METHOD__ . " old but compatible GIF metadata" );
142
144 }
145
146 return self::METADATA_GOOD;
147 }
148
153 public function getLongDesc( $image ) {
154 global $wgLang;
155
156 $original = parent::getLongDesc( $image );
157
158 $metadata = $image->getMetadataArray();
159
160 if ( !$metadata || isset( $metadata['_error'] ) || $metadata['frameCount'] <= 0 ) {
161 return $original;
162 }
163
164 /* Preserve original image info string, but strip the last char ')' so we can add even more */
165 $info = [];
166 $info[] = $original;
167
168 if ( $metadata['looped'] ) {
169 $info[] = wfMessage( 'file-info-gif-looped' )->parse();
170 }
171
172 if ( $metadata['frameCount'] > 1 ) {
173 $info[] = wfMessage( 'file-info-gif-frames' )->numParams( $metadata['frameCount'] )->parse();
174 }
175
176 if ( $metadata['duration'] ) {
177 $info[] = htmlspecialchars( $wgLang->formatTimePeriod( $metadata['duration'] ), ENT_QUOTES );
178 }
179
180 return $wgLang->commaList( $info );
181 }
182
191 public function getLength( $file ) {
192 $metadata = $file->getMetadataArray();
193
194 if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) {
195 return 0.0;
196 }
197 return (float)$metadata['duration'];
198 }
199}
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
Generic handler for bitmap images.
static GIF( $filename)
function for gif images.
Handler for GIF images.
isFileMetadataValid( $image)
Check if the metadata is valid for this handler.If it returns MediaHandler::METADATA_BAD (or false),...
getImageArea( $image)
getLength( $file)
Return the duration of the GIF file.
getLongDesc( $image)
isAnimatedImage( $image)
canAnimateThumbnail( $file)
We cannot animate thumbnails that are bigger than a particular size.
formatMetadata( $image, $context=false)
getSizeAndMetadata( $state, $filename)
Get image size information and metadata array.If this returns null, the caller will fall back to getI...
getMetadataType( $image)
Get a string describing the type of metadata, for display purposes.to overrideThis method is currentl...
getCommonMetaArray(File $image)
Return the standard metadata elements for #filemetadata parser func.
const METADATA_COMPATIBLE
formatMetadataHelper( $metadataArray, $context=false)
sorts the visible/invisible field.
const METADATA_GOOD
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:778
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Interface for objects which can provide a MediaWiki context on request.