3 * Handler for GIF images.
10 * Handler for GIF images.
14 class GIFHandler
extends BitmapHandler
{
16 const BROKEN_FILE
= '0'; // value to store in img_metadata if error extracting metadata.
18 function getMetadata( $image, $filename ) {
20 $parsedGIFMetadata = BitmapMetadataHandler
::GIF( $filename );
21 } catch( Exception
$e ) {
23 wfDebug( __METHOD__
. ': ' . $e->getMessage() . "\n" );
24 return self
::BROKEN_FILE
;
27 return serialize($parsedGIFMetadata);
34 function formatMetadata( $image ) {
35 $meta = $image->getMetadata();
40 $meta = unserialize( $meta );
41 if ( !isset( $meta['metadata'] ) ||
count( $meta['metadata'] ) <= 1 ) {
45 if ( isset( $meta['metadata']['_MW_GIF_VERSION'] ) ) {
46 unset( $meta['metadata']['_MW_GIF_VERSION'] );
48 return $this->formatMetadataHelper( $meta['metadata'] );
56 function getImageArea( $image ) {
57 $ser = $image->getMetadata();
59 $metadata = unserialize( $ser );
60 return $image->getWidth() * $image->getHeight() * $metadata['frameCount'];
62 return $image->getWidth() * $image->getHeight();
70 function isAnimatedImage( $image ) {
71 $ser = $image->getMetadata();
73 $metadata = unserialize($ser);
74 if( $metadata['frameCount'] > 1 ) {
81 function getMetadataType( $image ) {
85 function isMetadataValid( $image, $metadata ) {
86 if ( $metadata === self
::BROKEN_FILE
) {
87 // Do not repetitivly regenerate metadata on broken file.
88 return self
::METADATA_GOOD
;
92 $data = unserialize( $metadata );
95 if ( !$data ||
!is_array( $data ) ) {
96 wfDebug(__METHOD__
. ' invalid GIF metadata' );
97 return self
::METADATA_BAD
;
100 if ( !isset( $data['metadata']['_MW_GIF_VERSION'] )
101 ||
$data['metadata']['_MW_GIF_VERSION'] != GIFMetadataExtractor
::VERSION
) {
102 wfDebug(__METHOD__
. ' old but compatible GIF metadata' );
103 return self
::METADATA_COMPATIBLE
;
105 return self
::METADATA_GOOD
;
112 function getLongDesc( $image ) {
115 $original = parent
::getLongDesc( $image );
117 wfSuppressWarnings();
118 $metadata = unserialize($image->getMetadata());
121 if (!$metadata ||
$metadata['frameCount'] <= 1) {
125 /* Preserve original image info string, but strip the last char ')' so we can add even more */
129 if ( $metadata['looped'] ) {
130 $info[] = wfMsgExt( 'file-info-gif-looped', 'parseinline' );
133 if ( $metadata['frameCount'] > 1 ) {
134 $info[] = wfMsgExt( 'file-info-gif-frames', 'parseinline', $metadata['frameCount'] );
137 if ( $metadata['duration'] ) {
138 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
141 return $wgLang->commaList( $info );