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
25 * Handler for GIF images.
29 class GIFHandler
extends BitmapHandler
{
30 const BROKEN_FILE
= '0'; // value to store in img_metadata if error extracting metadata.
32 public function getMetadata( $image, $filename ) {
34 $parsedGIFMetadata = BitmapMetadataHandler
::GIF( $filename );
35 } catch ( Exception
$e ) {
37 wfDebug( __METHOD__
. ': ' . $e->getMessage() . "\n" );
39 return self
::BROKEN_FILE
;
42 return serialize( $parsedGIFMetadata );
47 * @param bool|IContextSource $context Context to use (optional)
50 public function formatMetadata( $image, $context = false ) {
51 $meta = $this->getCommonMetaArray( $image );
52 if ( count( $meta ) === 0 ) {
56 return $this->formatMetadataHelper( $meta, $context );
60 * Return the standard metadata elements for #filemetadata parser func.
64 public function getCommonMetaArray( File
$image ) {
65 $meta = $image->getMetadata();
70 $meta = unserialize( $meta );
71 if ( !isset( $meta['metadata'] ) ) {
74 unset( $meta['metadata']['_MW_GIF_VERSION'] );
76 return $meta['metadata'];
80 * @todo Add unit tests
85 function getImageArea( $image ) {
86 $ser = $image->getMetadata();
88 $metadata = unserialize( $ser );
89 if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 0 ) {
90 return $image->getWidth() * $image->getHeight() * $metadata['frameCount'];
92 return $image->getWidth() * $image->getHeight();
95 return $image->getWidth() * $image->getHeight();
103 function isAnimatedImage( $image ) {
104 $ser = $image->getMetadata();
106 $metadata = unserialize( $ser );
107 if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 1 ) {
116 * We cannot animate thumbnails that are bigger than a particular size
120 function canAnimateThumbnail( $file ) {
121 global $wgMaxAnimatedGifArea;
122 $answer = $this->getImageArea( $file ) <= $wgMaxAnimatedGifArea;
127 function getMetadataType( $image ) {
131 public function isMetadataValid( $image, $metadata ) {
132 if ( $metadata === self
::BROKEN_FILE
) {
133 // Do not repetitivly regenerate metadata on broken file.
134 return self
::METADATA_GOOD
;
137 Wikimedia\
suppressWarnings();
138 $data = unserialize( $metadata );
139 Wikimedia\restoreWarnings
();
141 if ( !$data ||
!is_array( $data ) ) {
142 wfDebug( __METHOD__
. " invalid GIF metadata\n" );
144 return self
::METADATA_BAD
;
147 if ( !isset( $data['metadata']['_MW_GIF_VERSION'] )
148 ||
$data['metadata']['_MW_GIF_VERSION'] != GIFMetadataExtractor
::VERSION
150 wfDebug( __METHOD__
. " old but compatible GIF metadata\n" );
152 return self
::METADATA_COMPATIBLE
;
155 return self
::METADATA_GOOD
;
162 public function getLongDesc( $image ) {
165 $original = parent
::getLongDesc( $image );
167 Wikimedia\
suppressWarnings();
168 $metadata = unserialize( $image->getMetadata() );
169 Wikimedia\restoreWarnings
();
171 if ( !$metadata ||
$metadata['frameCount'] <= 1 ) {
175 /* Preserve original image info string, but strip the last char ')' so we can add even more */
179 if ( $metadata['looped'] ) {
180 $info[] = wfMessage( 'file-info-gif-looped' )->parse();
183 if ( $metadata['frameCount'] > 1 ) {
184 $info[] = wfMessage( 'file-info-gif-frames' )->numParams( $metadata['frameCount'] )->parse();
187 if ( $metadata['duration'] ) {
188 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
191 return $wgLang->commaList( $info );
195 * Return the duration of the GIF file.
197 * Shown in the &query=imageinfo&iiprop=size api query.
200 * @return float The duration of the file.
202 public function getLength( $file ) {
203 $serMeta = $file->getMetadata();
204 Wikimedia\
suppressWarnings();
205 $metadata = unserialize( $serMeta );
206 Wikimedia\restoreWarnings
();
208 if ( !$metadata ||
!isset( $metadata['duration'] ) ||
!$metadata['duration'] ) {
211 return (float)$metadata['duration'];