3 * Handler for GIF images.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 use MediaWiki\Context\IContextSource
;
25 use MediaWiki\MainConfigNames
;
26 use MediaWiki\MediaWikiServices
;
27 use Wikimedia\RequestTimeout\TimeoutException
;
30 * Handler for GIF images.
34 class GIFHandler
extends BitmapHandler
{
36 * Value to store in img_metadata if there was error extracting metadata
38 private const BROKEN_FILE
= '0';
40 public function getSizeAndMetadata( $state, $filename ) {
42 $parsedGIFMetadata = BitmapMetadataHandler
::GIF( $filename );
43 } catch ( TimeoutException
$e ) {
45 } catch ( Exception
$e ) {
47 wfDebug( __METHOD__
. ': ' . $e->getMessage() );
49 return [ 'metadata' => [ '_error' => self
::BROKEN_FILE
] ];
53 'width' => $parsedGIFMetadata['width'],
54 'height' => $parsedGIFMetadata['height'],
55 'bits' => $parsedGIFMetadata['bits'],
56 'metadata' => array_diff_key(
58 [ 'width' => true, 'height' => true, 'bits' => true ]
65 * @param IContextSource|false $context
66 * @return array[]|false
68 public function formatMetadata( $image, $context = false ) {
69 $meta = $this->getCommonMetaArray( $image );
74 return $this->formatMetadataHelper( $meta, $context );
78 * Return the standard metadata elements for #filemetadata parser func.
82 public function getCommonMetaArray( File
$image ) {
83 $meta = $image->getMetadataArray();
84 if ( !isset( $meta['metadata'] ) ) {
87 unset( $meta['metadata']['_MW_GIF_VERSION'] );
89 return $meta['metadata'];
93 * @todo Add unit tests
98 public function getImageArea( $image ) {
99 $metadata = $image->getMetadataArray();
100 if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 0 ) {
101 return $image->getWidth() * $image->getHeight() * $metadata['frameCount'];
103 return $image->getWidth() * $image->getHeight();
110 public function isAnimatedImage( $image ) {
111 $metadata = $image->getMetadataArray();
112 if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 1 ) {
120 * We cannot animate thumbnails that are bigger than a particular size
124 public function canAnimateThumbnail( $file ) {
125 $maxAnimatedGifArea = MediaWikiServices
::getInstance()->getMainConfig()
126 ->get( MainConfigNames
::MaxAnimatedGifArea
);
128 return $this->getImageArea( $file ) <= $maxAnimatedGifArea;
131 public function getMetadataType( $image ) {
135 public function isFileMetadataValid( $image ) {
136 $data = $image->getMetadataArray();
137 if ( $data === [ '_error' => self
::BROKEN_FILE
] ) {
138 // Do not repetitively regenerate metadata on broken file.
139 return self
::METADATA_GOOD
;
142 if ( !$data ||
isset( $data['_error'] ) ) {
143 wfDebug( __METHOD__
. " invalid GIF metadata" );
145 return self
::METADATA_BAD
;
148 if ( !isset( $data['metadata']['_MW_GIF_VERSION'] )
149 ||
$data['metadata']['_MW_GIF_VERSION'] !== GIFMetadataExtractor
::VERSION
151 wfDebug( __METHOD__
. " old but compatible GIF metadata" );
153 return self
::METADATA_COMPATIBLE
;
156 return self
::METADATA_GOOD
;
163 public function getLongDesc( $image ) {
166 $original = parent
::getLongDesc( $image );
168 $metadata = $image->getMetadataArray();
170 if ( !$metadata ||
isset( $metadata['_error'] ) ||
$metadata['frameCount'] <= 0 ) {
174 /* Preserve original image info string, but strip the last char ')' so we can add even more */
178 if ( $metadata['looped'] ) {
179 $info[] = wfMessage( 'file-info-gif-looped' )->parse();
182 if ( $metadata['frameCount'] > 1 ) {
183 $info[] = wfMessage( 'file-info-gif-frames' )->numParams( $metadata['frameCount'] )->parse();
186 if ( $metadata['duration'] ) {
187 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
190 return $wgLang->commaList( $info );
194 * Return the duration of the GIF file.
196 * Shown in the &query=imageinfo&iiprop=size api query.
199 * @return float The duration of the file.
201 public function getLength( $file ) {
202 $metadata = $file->getMetadataArray();
204 if ( !$metadata ||
!isset( $metadata['duration'] ) ||
!$metadata['duration'] ) {
207 return (float)$metadata['duration'];