Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / media / GIF.php
blob5992be11ede7e436db5e732f5290759999a5492e
1 <?php
2 /**
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
20 * @file
21 * @ingroup Media
24 /**
25 * Handler for GIF images.
27 * @ingroup Media
29 class GIFHandler extends BitmapHandler {
30 const BROKEN_FILE = '0'; // value to store in img_metadata if error extracting metadata.
32 function getMetadata( $image, $filename ) {
33 try {
34 $parsedGIFMetadata = BitmapMetadataHandler::GIF( $filename );
35 } catch ( Exception $e ) {
36 // Broken file?
37 wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
39 return self::BROKEN_FILE;
42 return serialize( $parsedGIFMetadata );
45 /**
46 * @param File $image
47 * @return array|bool
49 function formatMetadata( $image ) {
50 $meta = $this->getCommonMetaArray( $image );
51 if ( count( $meta ) === 0 ) {
52 return false;
55 return $this->formatMetadataHelper( $meta );
58 /**
59 * Return the standard metadata elements for #filemetadata parser func.
60 * @param File $image
61 * @return array|bool
63 public function getCommonMetaArray( File $image ) {
64 $meta = $image->getMetadata();
66 if ( !$meta ) {
67 return array();
69 $meta = unserialize( $meta );
70 if ( !isset( $meta['metadata'] ) ) {
71 return array();
73 unset( $meta['metadata']['_MW_GIF_VERSION'] );
75 return $meta['metadata'];
78 /**
79 * @todo Add unit tests
81 * @param File $image
82 * @return bool
84 function getImageArea( $image ) {
85 $ser = $image->getMetadata();
86 if ( $ser ) {
87 $metadata = unserialize( $ser );
89 return $image->getWidth() * $image->getHeight() * $metadata['frameCount'];
90 } else {
91 return $image->getWidth() * $image->getHeight();
95 /**
96 * @param File $image
97 * @return bool
99 function isAnimatedImage( $image ) {
100 $ser = $image->getMetadata();
101 if ( $ser ) {
102 $metadata = unserialize( $ser );
103 if ( $metadata['frameCount'] > 1 ) {
104 return true;
108 return false;
112 * We cannot animate thumbnails that are bigger than a particular size
113 * @param File $file
114 * @return bool
116 function canAnimateThumbnail( $file ) {
117 global $wgMaxAnimatedGifArea;
118 $answer = $this->getImageArea( $file ) <= $wgMaxAnimatedGifArea;
120 return $answer;
123 function getMetadataType( $image ) {
124 return 'parsed-gif';
127 function isMetadataValid( $image, $metadata ) {
128 if ( $metadata === self::BROKEN_FILE ) {
129 // Do not repetitivly regenerate metadata on broken file.
130 return self::METADATA_GOOD;
133 wfSuppressWarnings();
134 $data = unserialize( $metadata );
135 wfRestoreWarnings();
137 if ( !$data || !is_array( $data ) ) {
138 wfDebug( __METHOD__ . " invalid GIF metadata\n" );
140 return self::METADATA_BAD;
143 if ( !isset( $data['metadata']['_MW_GIF_VERSION'] )
144 || $data['metadata']['_MW_GIF_VERSION'] != GIFMetadataExtractor::VERSION
146 wfDebug( __METHOD__ . " old but compatible GIF metadata\n" );
148 return self::METADATA_COMPATIBLE;
151 return self::METADATA_GOOD;
155 * @param File $image
156 * @return string
158 function getLongDesc( $image ) {
159 global $wgLang;
161 $original = parent::getLongDesc( $image );
163 wfSuppressWarnings();
164 $metadata = unserialize( $image->getMetadata() );
165 wfRestoreWarnings();
167 if ( !$metadata || $metadata['frameCount'] <= 1 ) {
168 return $original;
171 /* Preserve original image info string, but strip the last char ')' so we can add even more */
172 $info = array();
173 $info[] = $original;
175 if ( $metadata['looped'] ) {
176 $info[] = wfMessage( 'file-info-gif-looped' )->parse();
179 if ( $metadata['frameCount'] > 1 ) {
180 $info[] = wfMessage( 'file-info-gif-frames' )->numParams( $metadata['frameCount'] )->parse();
183 if ( $metadata['duration'] ) {
184 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
187 return $wgLang->commaList( $info );