MediaWiki  REL1_19
PNG.php
Go to the documentation of this file.
00001 <?php
00014 class PNGHandler extends BitmapHandler {
00015 
00016         const BROKEN_FILE = '0';
00017 
00023         function getMetadata( $image, $filename ) {
00024                 try {
00025                         $metadata = BitmapMetadataHandler::PNG( $filename );
00026                 } catch( Exception $e ) {
00027                         // Broken file?
00028                         wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
00029                         return self::BROKEN_FILE;
00030                 }
00031 
00032                 return serialize($metadata);
00033         }
00034 
00039         function formatMetadata( $image ) {
00040                 $meta = $image->getMetadata();
00041 
00042                 if ( !$meta ) {
00043                         return false;
00044                 }
00045                 $meta = unserialize( $meta );
00046                 if ( !isset( $meta['metadata'] ) || count( $meta['metadata'] ) <= 1 ) {
00047                         return false;
00048                 }
00049 
00050                 if ( isset( $meta['metadata']['_MW_PNG_VERSION'] ) ) {
00051                         unset( $meta['metadata']['_MW_PNG_VERSION'] );
00052                 }
00053                 return $this->formatMetadataHelper( $meta['metadata'] );
00054         }
00055 
00060         function isAnimatedImage( $image ) {
00061                 $ser = $image->getMetadata();
00062                 if ($ser) {
00063                         $metadata = unserialize($ser);
00064                         if( $metadata['frameCount'] > 1 ) return true;
00065                 }
00066                 return false;
00067         }
00068         
00069         function getMetadataType( $image ) {
00070                 return 'parsed-png';
00071         }
00072         
00073         function isMetadataValid( $image, $metadata ) {
00074 
00075                 if ( $metadata === self::BROKEN_FILE ) {
00076                         // Do not repetitivly regenerate metadata on broken file.
00077                         return self::METADATA_GOOD;
00078                 }
00079 
00080                 wfSuppressWarnings();
00081                 $data = unserialize( $metadata );
00082                 wfRestoreWarnings();
00083 
00084                 if ( !$data || !is_array( $data ) ) {
00085                         wfDebug(__METHOD__ . ' invalid png metadata' );
00086                         return self::METADATA_BAD;
00087                 }
00088 
00089                 if ( !isset( $data['metadata']['_MW_PNG_VERSION'] )
00090                         || $data['metadata']['_MW_PNG_VERSION'] != PNGMetadataExtractor::VERSION ) {
00091                         wfDebug(__METHOD__ . ' old but compatible png metadata' );
00092                         return self::METADATA_COMPATIBLE;
00093                 }
00094                 return self::METADATA_GOOD;
00095         }
00096 
00101         function getLongDesc( $image ) {
00102                 global $wgLang;
00103                 $original = parent::getLongDesc( $image );
00104 
00105                 wfSuppressWarnings();
00106                 $metadata = unserialize($image->getMetadata());
00107                 wfRestoreWarnings();
00108 
00109                 if( !$metadata || $metadata['frameCount'] <= 0 )
00110                         return $original;
00111 
00112                 $info = array();
00113                 $info[] = $original;
00114                 
00115                 if ( $metadata['loopCount'] == 0 ) {
00116                         $info[] = wfMsgExt( 'file-info-png-looped', 'parseinline' );
00117                 } elseif ( $metadata['loopCount'] > 1 ) {
00118                         $info[] = wfMsgExt( 'file-info-png-repeat', 'parseinline', $metadata['loopCount'] );
00119                 }
00120                 
00121                 if ( $metadata['frameCount'] > 0 ) {
00122                         $info[] = wfMsgExt( 'file-info-png-frames', 'parseinline', $metadata['frameCount'] );
00123                 }
00124                 
00125                 if ( $metadata['duration'] ) {
00126                         $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
00127                 }
00128                 
00129                 return $wgLang->commaList( $info );
00130         }
00131 
00132 }