MediaWiki
REL1_19
|
00001 <?php 00014 class GIFHandler extends BitmapHandler { 00015 00016 const BROKEN_FILE = '0'; // value to store in img_metadata if error extracting metadata. 00017 00018 function getMetadata( $image, $filename ) { 00019 try { 00020 $parsedGIFMetadata = BitmapMetadataHandler::GIF( $filename ); 00021 } catch( Exception $e ) { 00022 // Broken file? 00023 wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" ); 00024 return self::BROKEN_FILE; 00025 } 00026 00027 return serialize($parsedGIFMetadata); 00028 } 00029 00034 function formatMetadata( $image ) { 00035 $meta = $image->getMetadata(); 00036 00037 if ( !$meta ) { 00038 return false; 00039 } 00040 $meta = unserialize( $meta ); 00041 if ( !isset( $meta['metadata'] ) || count( $meta['metadata'] ) <= 1 ) { 00042 return false; 00043 } 00044 00045 if ( isset( $meta['metadata']['_MW_GIF_VERSION'] ) ) { 00046 unset( $meta['metadata']['_MW_GIF_VERSION'] ); 00047 } 00048 return $this->formatMetadataHelper( $meta['metadata'] ); 00049 } 00050 00056 function getImageArea( $image ) { 00057 $ser = $image->getMetadata(); 00058 if ( $ser ) { 00059 $metadata = unserialize( $ser ); 00060 return $image->getWidth() * $image->getHeight() * $metadata['frameCount']; 00061 } else { 00062 return $image->getWidth() * $image->getHeight(); 00063 } 00064 } 00065 00070 function isAnimatedImage( $image ) { 00071 $ser = $image->getMetadata(); 00072 if ( $ser ) { 00073 $metadata = unserialize($ser); 00074 if( $metadata['frameCount'] > 1 ) { 00075 return true; 00076 } 00077 } 00078 return false; 00079 } 00080 00081 function getMetadataType( $image ) { 00082 return 'parsed-gif'; 00083 } 00084 00085 function isMetadataValid( $image, $metadata ) { 00086 if ( $metadata === self::BROKEN_FILE ) { 00087 // Do not repetitivly regenerate metadata on broken file. 00088 return self::METADATA_GOOD; 00089 } 00090 00091 wfSuppressWarnings(); 00092 $data = unserialize( $metadata ); 00093 wfRestoreWarnings(); 00094 00095 if ( !$data || !is_array( $data ) ) { 00096 wfDebug(__METHOD__ . ' invalid GIF metadata' ); 00097 return self::METADATA_BAD; 00098 } 00099 00100 if ( !isset( $data['metadata']['_MW_GIF_VERSION'] ) 00101 || $data['metadata']['_MW_GIF_VERSION'] != GIFMetadataExtractor::VERSION ) { 00102 wfDebug(__METHOD__ . ' old but compatible GIF metadata' ); 00103 return self::METADATA_COMPATIBLE; 00104 } 00105 return self::METADATA_GOOD; 00106 } 00107 00112 function getLongDesc( $image ) { 00113 global $wgLang; 00114 00115 $original = parent::getLongDesc( $image ); 00116 00117 wfSuppressWarnings(); 00118 $metadata = unserialize($image->getMetadata()); 00119 wfRestoreWarnings(); 00120 00121 if (!$metadata || $metadata['frameCount'] <= 1) { 00122 return $original; 00123 } 00124 00125 /* Preserve original image info string, but strip the last char ')' so we can add even more */ 00126 $info = array(); 00127 $info[] = $original; 00128 00129 if ( $metadata['looped'] ) { 00130 $info[] = wfMsgExt( 'file-info-gif-looped', 'parseinline' ); 00131 } 00132 00133 if ( $metadata['frameCount'] > 1 ) { 00134 $info[] = wfMsgExt( 'file-info-gif-frames', 'parseinline', $metadata['frameCount'] ); 00135 } 00136 00137 if ( $metadata['duration'] ) { 00138 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] ); 00139 } 00140 00141 return $wgLang->commaList( $info ); 00142 } 00143 }