MediaWiki  REL1_22
PNG.php
Go to the documentation of this file.
00001 <?php
00029 class PNGHandler extends BitmapHandler {
00030 
00031     const BROKEN_FILE = '0';
00032 
00038     function getMetadata( $image, $filename ) {
00039         try {
00040             $metadata = BitmapMetadataHandler::PNG( $filename );
00041         } catch ( Exception $e ) {
00042             // Broken file?
00043             wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
00044             return self::BROKEN_FILE;
00045         }
00046 
00047         return serialize( $metadata );
00048     }
00049 
00054     function formatMetadata( $image ) {
00055         $meta = $image->getMetadata();
00056 
00057         if ( !$meta ) {
00058             return false;
00059         }
00060         $meta = unserialize( $meta );
00061         if ( !isset( $meta['metadata'] ) || count( $meta['metadata'] ) <= 1 ) {
00062             return false;
00063         }
00064 
00065         if ( isset( $meta['metadata']['_MW_PNG_VERSION'] ) ) {
00066             unset( $meta['metadata']['_MW_PNG_VERSION'] );
00067         }
00068         return $this->formatMetadataHelper( $meta['metadata'] );
00069     }
00070 
00075     function isAnimatedImage( $image ) {
00076         $ser = $image->getMetadata();
00077         if ( $ser ) {
00078             $metadata = unserialize( $ser );
00079             if ( $metadata['frameCount'] > 1 ) {
00080                 return true;
00081             }
00082         }
00083         return false;
00084     }
00090     function canAnimateThumbnail( $image ) {
00091         return false;
00092     }
00093 
00094     function getMetadataType( $image ) {
00095         return 'parsed-png';
00096     }
00097 
00098     function isMetadataValid( $image, $metadata ) {
00099 
00100         if ( $metadata === self::BROKEN_FILE ) {
00101             // Do not repetitivly regenerate metadata on broken file.
00102             return self::METADATA_GOOD;
00103         }
00104 
00105         wfSuppressWarnings();
00106         $data = unserialize( $metadata );
00107         wfRestoreWarnings();
00108 
00109         if ( !$data || !is_array( $data ) ) {
00110             wfDebug( __METHOD__ . " invalid png metadata\n" );
00111             return self::METADATA_BAD;
00112         }
00113 
00114         if ( !isset( $data['metadata']['_MW_PNG_VERSION'] )
00115             || $data['metadata']['_MW_PNG_VERSION'] != PNGMetadataExtractor::VERSION ) {
00116             wfDebug( __METHOD__ . " old but compatible png metadata\n" );
00117             return self::METADATA_COMPATIBLE;
00118         }
00119         return self::METADATA_GOOD;
00120     }
00121 
00126     function getLongDesc( $image ) {
00127         global $wgLang;
00128         $original = parent::getLongDesc( $image );
00129 
00130         wfSuppressWarnings();
00131         $metadata = unserialize( $image->getMetadata() );
00132         wfRestoreWarnings();
00133 
00134         if ( !$metadata || $metadata['frameCount'] <= 0 ) {
00135             return $original;
00136         }
00137 
00138         $info = array();
00139         $info[] = $original;
00140 
00141         if ( $metadata['loopCount'] == 0 ) {
00142             $info[] = wfMessage( 'file-info-png-looped' )->parse();
00143         } elseif ( $metadata['loopCount'] > 1 ) {
00144             $info[] = wfMessage( 'file-info-png-repeat' )->numParams( $metadata['loopCount'] )->parse();
00145         }
00146 
00147         if ( $metadata['frameCount'] > 0 ) {
00148             $info[] = wfMessage( 'file-info-png-frames' )->numParams( $metadata['frameCount'] )->parse();
00149         }
00150 
00151         if ( $metadata['duration'] ) {
00152             $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
00153         }
00154 
00155         return $wgLang->commaList( $info );
00156     }
00157 
00158 }