MediaWiki  REL1_24
GIF.php
Go to the documentation of this file.
00001 <?php
00029 class GIFHandler extends BitmapHandler {
00030     const BROKEN_FILE = '0'; // value to store in img_metadata if error extracting metadata.
00031 
00032     function getMetadata( $image, $filename ) {
00033         try {
00034             $parsedGIFMetadata = BitmapMetadataHandler::GIF( $filename );
00035         } catch ( Exception $e ) {
00036             // Broken file?
00037             wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
00038 
00039             return self::BROKEN_FILE;
00040         }
00041 
00042         return serialize( $parsedGIFMetadata );
00043     }
00044 
00049     function formatMetadata( $image ) {
00050         $meta = $this->getCommonMetaArray( $image );
00051         if ( count( $meta ) === 0 ) {
00052             return false;
00053         }
00054 
00055         return $this->formatMetadataHelper( $meta );
00056     }
00057 
00063     public function getCommonMetaArray( File $image ) {
00064         $meta = $image->getMetadata();
00065 
00066         if ( !$meta ) {
00067             return array();
00068         }
00069         $meta = unserialize( $meta );
00070         if ( !isset( $meta['metadata'] ) ) {
00071             return array();
00072         }
00073         unset( $meta['metadata']['_MW_GIF_VERSION'] );
00074 
00075         return $meta['metadata'];
00076     }
00077 
00084     function getImageArea( $image ) {
00085         $ser = $image->getMetadata();
00086         if ( $ser ) {
00087             $metadata = unserialize( $ser );
00088 
00089             return $image->getWidth() * $image->getHeight() * $metadata['frameCount'];
00090         } else {
00091             return $image->getWidth() * $image->getHeight();
00092         }
00093     }
00094 
00099     function isAnimatedImage( $image ) {
00100         $ser = $image->getMetadata();
00101         if ( $ser ) {
00102             $metadata = unserialize( $ser );
00103             if ( $metadata['frameCount'] > 1 ) {
00104                 return true;
00105             }
00106         }
00107 
00108         return false;
00109     }
00110 
00116     function canAnimateThumbnail( $file ) {
00117         global $wgMaxAnimatedGifArea;
00118         $answer = $this->getImageArea( $file ) <= $wgMaxAnimatedGifArea;
00119 
00120         return $answer;
00121     }
00122 
00123     function getMetadataType( $image ) {
00124         return 'parsed-gif';
00125     }
00126 
00127     function isMetadataValid( $image, $metadata ) {
00128         if ( $metadata === self::BROKEN_FILE ) {
00129             // Do not repetitivly regenerate metadata on broken file.
00130             return self::METADATA_GOOD;
00131         }
00132 
00133         wfSuppressWarnings();
00134         $data = unserialize( $metadata );
00135         wfRestoreWarnings();
00136 
00137         if ( !$data || !is_array( $data ) ) {
00138             wfDebug( __METHOD__ . " invalid GIF metadata\n" );
00139 
00140             return self::METADATA_BAD;
00141         }
00142 
00143         if ( !isset( $data['metadata']['_MW_GIF_VERSION'] )
00144             || $data['metadata']['_MW_GIF_VERSION'] != GIFMetadataExtractor::VERSION
00145         ) {
00146             wfDebug( __METHOD__ . " old but compatible GIF metadata\n" );
00147 
00148             return self::METADATA_COMPATIBLE;
00149         }
00150 
00151         return self::METADATA_GOOD;
00152     }
00153 
00158     function getLongDesc( $image ) {
00159         global $wgLang;
00160 
00161         $original = parent::getLongDesc( $image );
00162 
00163         wfSuppressWarnings();
00164         $metadata = unserialize( $image->getMetadata() );
00165         wfRestoreWarnings();
00166 
00167         if ( !$metadata || $metadata['frameCount'] <= 1 ) {
00168             return $original;
00169         }
00170 
00171         /* Preserve original image info string, but strip the last char ')' so we can add even more */
00172         $info = array();
00173         $info[] = $original;
00174 
00175         if ( $metadata['looped'] ) {
00176             $info[] = wfMessage( 'file-info-gif-looped' )->parse();
00177         }
00178 
00179         if ( $metadata['frameCount'] > 1 ) {
00180             $info[] = wfMessage( 'file-info-gif-frames' )->numParams( $metadata['frameCount'] )->parse();
00181         }
00182 
00183         if ( $metadata['duration'] ) {
00184             $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
00185         }
00186 
00187         return $wgLang->commaList( $info );
00188     }
00189 }