MediaWiki REL1_37
GIFHandler.php
Go to the documentation of this file.
1<?php
24use Wikimedia\RequestTimeout\TimeoutException;
25
35 private const BROKEN_FILE = '0';
36
37 public function getSizeAndMetadata( $state, $filename ) {
38 try {
39 $parsedGIFMetadata = BitmapMetadataHandler::GIF( $filename );
40 } catch ( TimeoutException $e ) {
41 throw $e;
42 } catch ( Exception $e ) {
43 // Broken file?
44 wfDebug( __METHOD__ . ': ' . $e->getMessage() );
45
46 return [ 'metadata' => [ '_error' => self::BROKEN_FILE ] ];
47 }
48
49 return [
50 'width' => $parsedGIFMetadata['width'],
51 'height' => $parsedGIFMetadata['height'],
52 'bits' => $parsedGIFMetadata['bits'],
53 'metadata' => array_diff_key(
54 $parsedGIFMetadata,
55 [ 'width' => true, 'height' => true, 'bits' => true ]
56 )
57 ];
58 }
59
65 public function formatMetadata( $image, $context = false ) {
66 $meta = $this->getCommonMetaArray( $image );
67 if ( !$meta ) {
68 return false;
69 }
70
71 return $this->formatMetadataHelper( $meta, $context );
72 }
73
79 public function getCommonMetaArray( File $image ) {
80 $meta = $image->getMetadataArray();
81 if ( !isset( $meta['metadata'] ) ) {
82 return [];
83 }
84 unset( $meta['metadata']['_MW_GIF_VERSION'] );
85
86 return $meta['metadata'];
87 }
88
95 public function getImageArea( $image ) {
96 $metadata = $image->getMetadataArray();
97 if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 0 ) {
98 return $image->getWidth() * $image->getHeight() * $metadata['frameCount'];
99 } else {
100 return $image->getWidth() * $image->getHeight();
101 }
102 }
103
108 public function isAnimatedImage( $image ) {
109 $metadata = $image->getMetadataArray();
110 if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 1 ) {
111 return true;
112 }
113
114 return false;
115 }
116
122 public function canAnimateThumbnail( $file ) {
124
125 return $this->getImageArea( $file ) <= $wgMaxAnimatedGifArea;
126 }
127
128 public function getMetadataType( $image ) {
129 return 'parsed-gif';
130 }
131
132 public function isFileMetadataValid( $image ) {
133 $data = $image->getMetadataArray();
134 if ( $data === [ '_error' => self::BROKEN_FILE ] ) {
135 // Do not repetitively regenerate metadata on broken file.
136 return self::METADATA_GOOD;
137 }
138
139 if ( !$data || isset( $data['_error'] ) ) {
140 wfDebug( __METHOD__ . " invalid GIF metadata" );
141
142 return self::METADATA_BAD;
143 }
144
145 if ( !isset( $data['metadata']['_MW_GIF_VERSION'] )
146 || $data['metadata']['_MW_GIF_VERSION'] != GIFMetadataExtractor::VERSION
147 ) {
148 wfDebug( __METHOD__ . " old but compatible GIF metadata" );
149
151 }
152
153 return self::METADATA_GOOD;
154 }
155
160 public function getLongDesc( $image ) {
161 global $wgLang;
162
163 $original = parent::getLongDesc( $image );
164
165 $metadata = $image->getMetadataArray();
166
167 if ( !$metadata || isset( $metadata['_error'] ) || $metadata['frameCount'] <= 0 ) {
168 return $original;
169 }
170
171 /* Preserve original image info string, but strip the last char ')' so we can add even more */
172 $info = [];
173 $info[] = $original;
174
175 if ( $metadata['looped'] ) {
176 $info[] = wfMessage( 'file-info-gif-looped' )->parse();
177 }
178
179 if ( $metadata['frameCount'] > 1 ) {
180 $info[] = wfMessage( 'file-info-gif-frames' )->numParams( $metadata['frameCount'] )->parse();
181 }
182
183 if ( $metadata['duration'] ) {
184 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
185 }
186
187 return $wgLang->commaList( $info );
188 }
189
198 public function getLength( $file ) {
199 $metadata = $file->getMetadataArray();
200
201 if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) {
202 return 0.0;
203 } else {
204 return (float)$metadata['duration'];
205 }
206 }
207}
$wgMaxAnimatedGifArea
Force thumbnailing of animated GIFs above this size to a single frame instead of an animated thumbnai...
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.
$wgLang
Definition Setup.php:831
Generic handler for bitmap images.
static GIF( $filename)
function for gif images.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:66
getMetadataArray()
Get the unserialized handler-specific metadata STUB.
Definition File.php:764
Handler for GIF images.
isFileMetadataValid( $image)
Check if the metadata is valid for this handler.
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)
const BROKEN_FILE
Value to store in img_metadata if there was error extracting metadata.
getSizeAndMetadata( $state, $filename)
Get image size information and metadata array.
getMetadataType( $image)
Get a string describing the type of metadata, for display purposes.
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
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42