MediaWiki fundraising/REL1_35
GIFHandler.php
Go to the documentation of this file.
1<?php
33 private const BROKEN_FILE = '0';
34
35 public function getMetadata( $image, $filename ) {
36 try {
37 $parsedGIFMetadata = BitmapMetadataHandler::GIF( $filename );
38 } catch ( Exception $e ) {
39 // Broken file?
40 wfDebug( __METHOD__ . ': ' . $e->getMessage() );
41
42 return self::BROKEN_FILE;
43 }
44
45 return serialize( $parsedGIFMetadata );
46 }
47
53 public function formatMetadata( $image, $context = false ) {
54 $meta = $this->getCommonMetaArray( $image );
55 if ( count( $meta ) === 0 ) {
56 return false;
57 }
58
59 return $this->formatMetadataHelper( $meta, $context );
60 }
61
67 public function getCommonMetaArray( File $image ) {
68 $meta = $image->getMetadata();
69
70 if ( !$meta ) {
71 return [];
72 }
73 $meta = unserialize( $meta );
74 if ( !isset( $meta['metadata'] ) ) {
75 return [];
76 }
77 unset( $meta['metadata']['_MW_GIF_VERSION'] );
78
79 return $meta['metadata'];
80 }
81
88 public function getImageArea( $image ) {
89 $ser = $image->getMetadata();
90 if ( $ser ) {
91 $metadata = unserialize( $ser );
92 if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 0 ) {
93 return $image->getWidth() * $image->getHeight() * $metadata['frameCount'];
94 } else {
95 return $image->getWidth() * $image->getHeight();
96 }
97 } else {
98 return $image->getWidth() * $image->getHeight();
99 }
100 }
101
106 public function isAnimatedImage( $image ) {
107 $ser = $image->getMetadata();
108 if ( $ser ) {
109 $metadata = unserialize( $ser );
110 if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 1 ) {
111 return true;
112 }
113 }
114
115 return false;
116 }
117
123 public function canAnimateThumbnail( $file ) {
125
126 return $this->getImageArea( $file ) <= $wgMaxAnimatedGifArea;
127 }
128
129 public function getMetadataType( $image ) {
130 return 'parsed-gif';
131 }
132
133 public function isMetadataValid( $image, $metadata ) {
134 if ( $metadata === self::BROKEN_FILE ) {
135 // Do not repetitivly regenerate metadata on broken file.
136 return self::METADATA_GOOD;
137 }
138
139 Wikimedia\suppressWarnings();
140 $data = unserialize( $metadata );
141 Wikimedia\restoreWarnings();
142
143 if ( !$data || !is_array( $data ) ) {
144 wfDebug( __METHOD__ . " invalid GIF metadata" );
145
146 return self::METADATA_BAD;
147 }
148
149 if ( !isset( $data['metadata']['_MW_GIF_VERSION'] )
150 || $data['metadata']['_MW_GIF_VERSION'] != GIFMetadataExtractor::VERSION
151 ) {
152 wfDebug( __METHOD__ . " old but compatible GIF metadata" );
153
155 }
156
157 return self::METADATA_GOOD;
158 }
159
164 public function getLongDesc( $image ) {
165 global $wgLang;
166
167 $original = parent::getLongDesc( $image );
168
169 Wikimedia\suppressWarnings();
170 $metadata = unserialize( $image->getMetadata() );
171 Wikimedia\restoreWarnings();
172
173 if ( !$metadata || $metadata['frameCount'] <= 1 ) {
174 return $original;
175 }
176
177 /* Preserve original image info string, but strip the last char ')' so we can add even more */
178 $info = [];
179 $info[] = $original;
180
181 if ( $metadata['looped'] ) {
182 $info[] = wfMessage( 'file-info-gif-looped' )->parse();
183 }
184
185 if ( $metadata['frameCount'] > 1 ) {
186 $info[] = wfMessage( 'file-info-gif-frames' )->numParams( $metadata['frameCount'] )->parse();
187 }
188
189 if ( $metadata['duration'] ) {
190 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
191 }
192
193 return $wgLang->commaList( $info );
194 }
195
204 public function getLength( $file ) {
205 $serMeta = $file->getMetadata();
206 Wikimedia\suppressWarnings();
207 $metadata = unserialize( $serMeta );
208 Wikimedia\restoreWarnings();
209
210 if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) {
211 return 0.0;
212 } else {
213 return (float)$metadata['duration'];
214 }
215 }
216}
serialize()
unserialize( $serialized)
$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:781
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:63
getMetadata()
Get handler-specific metadata Overridden by LocalFile, UnregisteredLocalFile STUB Stable to override.
Definition File.php:722
Handler for GIF images.
getImageArea( $image)
isMetadataValid( $image, $metadata)
Check if the metadata string is valid for this handler.
getLength( $file)
Return the duration of the GIF file.
getLongDesc( $image)
getMetadata( $image, $filename)
Get handler-specific metadata which will be saved in the img_metadata field.
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.
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