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 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 );
49 function formatMetadata( $image ) {
50 $meta = $this->getCommonMetaArray( $image );
51 if ( count( $meta ) === 0 ) {
55 return $this->formatMetadataHelper( $meta );
59 * Return the standard metadata elements for #filemetadata parser func.
63 public function getCommonMetaArray( File
$image ) {
64 $meta = $image->getMetadata();
69 $meta = unserialize( $meta );
70 if ( !isset( $meta['metadata'] ) ) {
73 unset( $meta['metadata']['_MW_GIF_VERSION'] );
75 return $meta['metadata'];
83 function getImageArea( $image ) {
84 $ser = $image->getMetadata();
86 $metadata = unserialize( $ser );
88 return $image->getWidth() * $image->getHeight() * $metadata['frameCount'];
90 return $image->getWidth() * $image->getHeight();
98 function isAnimatedImage( $image ) {
99 $ser = $image->getMetadata();
101 $metadata = unserialize( $ser );
102 if ( $metadata['frameCount'] > 1 ) {
111 * We cannot animate thumbnails that are bigger than a particular size
115 function canAnimateThumbnail( $file ) {
116 global $wgMaxAnimatedGifArea;
117 $answer = $this->getImageArea( $file ) <= $wgMaxAnimatedGifArea;
122 function getMetadataType( $image ) {
126 function isMetadataValid( $image, $metadata ) {
127 if ( $metadata === self
::BROKEN_FILE
) {
128 // Do not repetitivly regenerate metadata on broken file.
129 return self
::METADATA_GOOD
;
132 wfSuppressWarnings();
133 $data = unserialize( $metadata );
136 if ( !$data ||
!is_array( $data ) ) {
137 wfDebug( __METHOD__
. " invalid GIF metadata\n" );
139 return self
::METADATA_BAD
;
142 if ( !isset( $data['metadata']['_MW_GIF_VERSION'] )
143 ||
$data['metadata']['_MW_GIF_VERSION'] != GIFMetadataExtractor
::VERSION
145 wfDebug( __METHOD__
. " old but compatible GIF metadata\n" );
147 return self
::METADATA_COMPATIBLE
;
150 return self
::METADATA_GOOD
;
157 function getLongDesc( $image ) {
160 $original = parent
::getLongDesc( $image );
162 wfSuppressWarnings();
163 $metadata = unserialize( $image->getMetadata() );
166 if ( !$metadata ||
$metadata['frameCount'] <= 1 ) {
170 /* Preserve original image info string, but strip the last char ')' so we can add even more */
174 if ( $metadata['looped'] ) {
175 $info[] = wfMessage( 'file-info-gif-looped' )->parse();
178 if ( $metadata['frameCount'] > 1 ) {
179 $info[] = wfMessage( 'file-info-gif-frames' )->numParams( $metadata['frameCount'] )->parse();
182 if ( $metadata['duration'] ) {
183 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
186 return $wgLang->commaList( $info );