Merge to trunk everything in img_metadata branch.
[lhc/web/wiklou.git] / includes / media / GIF.php
1 <?php
2 /**
3 * Handler for GIF images.
4 *
5 * @file
6 * @ingroup Media
7 */
8
9 /**
10 * Handler for GIF images.
11 *
12 * @ingroup Media
13 */
14 class GIFHandler extends BitmapHandler {
15
16 const BROKEN_FILE = '0'; // value to store in img_metadata if error extracting metadata.
17
18 function getMetadata( $image, $filename ) {
19 try {
20 $parsedGIFMetadata = BitmapMetadataHandler::GIF( $filename );
21 } catch( Exception $e ) {
22 // Broken file?
23 wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
24 return self::BROKEN_FILE;
25 }
26
27 return serialize($parsedGIFMetadata);
28 }
29
30 function formatMetadata( $image ) {
31 $meta = $image->getMetadata();
32
33 if ( !$meta ) {
34 return false;
35 }
36 $meta = unserialize( $meta );
37 if ( !isset( $meta['metadata'] ) || count( $meta['metadata'] ) <= 1 ) {
38 return false;
39 }
40
41 if ( isset( $meta['metadata']['_MW_GIF_VERSION'] ) ) {
42 unset( $meta['metadata']['_MW_GIF_VERSION'] );
43 }
44 return $this->formatMetadataHelper( $meta['metadata'] );
45 }
46
47 /**
48 * @param $image File
49 * @param $width
50 * @param $height
51 * @return
52 */
53 function getImageArea( $image, $width, $height ) {
54 $ser = $image->getMetadata();
55 if ( $ser ) {
56 $metadata = unserialize( $ser );
57 return $width * $height * $metadata['frameCount'];
58 } else {
59 return $width * $height;
60 }
61 }
62
63 /**
64 * @param $image File
65 * @return bool
66 */
67 function isAnimatedImage( $image ) {
68 $ser = $image->getMetadata();
69 if ( $ser ) {
70 $metadata = unserialize($ser);
71 if( $metadata['frameCount'] > 1 ) {
72 return true;
73 }
74 }
75 return false;
76 }
77
78 function getMetadataType( $image ) {
79 return 'parsed-gif';
80 }
81
82 function isMetadataValid( $image, $metadata ) {
83 if ( $metadata === self::BROKEN_FILE ) {
84 // Do not repetitivly regenerate metadata on broken file.
85 return self::METADATA_GOOD;
86 }
87
88 wfSuppressWarnings();
89 $data = unserialize( $metadata );
90 wfRestoreWarnings();
91
92 if ( !$data || !is_array( $data ) ) {
93 wfDebug(__METHOD__ . ' invalid GIF metadata' );
94 return self::METADATA_BAD;
95 }
96
97 if ( !isset( $data['metadata']['_MW_GIF_VERSION'] )
98 || $data['metadata']['_MW_GIF_VERSION'] != GIFMetadataExtractor::VERSION ) {
99 wfDebug(__METHOD__ . ' old but compatible GIF metadata' );
100 return self::METADATA_COMPATIBLE;
101 }
102 return self::METADATA_GOOD;
103 }
104
105 /**
106 * @param $image File
107 * @return string
108 */
109 function getLongDesc( $image ) {
110 global $wgLang;
111
112 $original = parent::getLongDesc( $image );
113
114 wfSuppressWarnings();
115 $metadata = unserialize($image->getMetadata());
116 wfRestoreWarnings();
117
118 if (!$metadata || $metadata['frameCount'] <= 1) {
119 return $original;
120 }
121
122 /* Preserve original image info string, but strip the last char ')' so we can add even more */
123 $info = array();
124 $info[] = $original;
125
126 if ( $metadata['looped'] ) {
127 $info[] = wfMsgExt( 'file-info-gif-looped', 'parseinline' );
128 }
129
130 if ( $metadata['frameCount'] > 1 ) {
131 $info[] = wfMsgExt( 'file-info-gif-frames', 'parseinline', $metadata['frameCount'] );
132 }
133
134 if ( $metadata['duration'] ) {
135 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
136 }
137
138 return $wgLang->commaList( $info );
139 }
140 }