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