MediaWiki  REL1_24
PNG.php
Go to the documentation of this file.
00001 <?php
00029 class PNGHandler extends BitmapHandler {
00030     const BROKEN_FILE = '0';
00031 
00037     function getMetadata( $image, $filename ) {
00038         try {
00039             $metadata = BitmapMetadataHandler::PNG( $filename );
00040         } catch ( Exception $e ) {
00041             // Broken file?
00042             wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
00043 
00044             return self::BROKEN_FILE;
00045         }
00046 
00047         return serialize( $metadata );
00048     }
00049 
00054     function formatMetadata( $image ) {
00055         $meta = $this->getCommonMetaArray( $image );
00056         if ( count( $meta ) === 0 ) {
00057             return false;
00058         }
00059 
00060         return $this->formatMetadataHelper( $meta );
00061     }
00062 
00069     public function getCommonMetaArray( File $image ) {
00070         $meta = $image->getMetadata();
00071 
00072         if ( !$meta ) {
00073             return array();
00074         }
00075         $meta = unserialize( $meta );
00076         if ( !isset( $meta['metadata'] ) ) {
00077             return array();
00078         }
00079         unset( $meta['metadata']['_MW_PNG_VERSION'] );
00080 
00081         return $meta['metadata'];
00082     }
00083 
00088     function isAnimatedImage( $image ) {
00089         $ser = $image->getMetadata();
00090         if ( $ser ) {
00091             $metadata = unserialize( $ser );
00092             if ( $metadata['frameCount'] > 1 ) {
00093                 return true;
00094             }
00095         }
00096 
00097         return false;
00098     }
00099 
00105     function canAnimateThumbnail( $image ) {
00106         return false;
00107     }
00108 
00109     function getMetadataType( $image ) {
00110         return 'parsed-png';
00111     }
00112 
00113     function isMetadataValid( $image, $metadata ) {
00114 
00115         if ( $metadata === self::BROKEN_FILE ) {
00116             // Do not repetitivly regenerate metadata on broken file.
00117             return self::METADATA_GOOD;
00118         }
00119 
00120         wfSuppressWarnings();
00121         $data = unserialize( $metadata );
00122         wfRestoreWarnings();
00123 
00124         if ( !$data || !is_array( $data ) ) {
00125             wfDebug( __METHOD__ . " invalid png metadata\n" );
00126 
00127             return self::METADATA_BAD;
00128         }
00129 
00130         if ( !isset( $data['metadata']['_MW_PNG_VERSION'] )
00131             || $data['metadata']['_MW_PNG_VERSION'] != PNGMetadataExtractor::VERSION
00132         ) {
00133             wfDebug( __METHOD__ . " old but compatible png metadata\n" );
00134 
00135             return self::METADATA_COMPATIBLE;
00136         }
00137 
00138         return self::METADATA_GOOD;
00139     }
00140 
00145     function getLongDesc( $image ) {
00146         global $wgLang;
00147         $original = parent::getLongDesc( $image );
00148 
00149         wfSuppressWarnings();
00150         $metadata = unserialize( $image->getMetadata() );
00151         wfRestoreWarnings();
00152 
00153         if ( !$metadata || $metadata['frameCount'] <= 0 ) {
00154             return $original;
00155         }
00156 
00157         $info = array();
00158         $info[] = $original;
00159 
00160         if ( $metadata['loopCount'] == 0 ) {
00161             $info[] = wfMessage( 'file-info-png-looped' )->parse();
00162         } elseif ( $metadata['loopCount'] > 1 ) {
00163             $info[] = wfMessage( 'file-info-png-repeat' )->numParams( $metadata['loopCount'] )->parse();
00164         }
00165 
00166         if ( $metadata['frameCount'] > 0 ) {
00167             $info[] = wfMessage( 'file-info-png-frames' )->numParams( $metadata['frameCount'] )->parse();
00168         }
00169 
00170         if ( $metadata['duration'] ) {
00171             $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
00172         }
00173 
00174         return $wgLang->commaList( $info );
00175     }
00176 
00177     public function supportsBucketing() {
00178         return true;
00179     }
00180 }