follow-up r86195 (Sort of). Make sure the string there can really be exploded before...
[lhc/web/wiklou.git] / includes / media / PNG.php
1 <?php
2 /**
3 * Handler for PNG images.
4 *
5 * @file
6 * @ingroup Media
7 */
8
9 /**
10 * Handler for PNG images.
11 *
12 * @ingroup Media
13 */
14 class PNGHandler extends BitmapHandler {
15
16 const BROKEN_FILE = '0';
17
18
19 /**
20 * @param File $image
21 * @param string $filename
22 * @return string
23 */
24 function getMetadata( $image, $filename ) {
25 try {
26 $metadata = BitmapMetadataHandler::PNG( $filename );
27 } catch( Exception $e ) {
28 // Broken file?
29 wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
30 return self::BROKEN_FILE;
31 }
32
33 return serialize($metadata);
34 }
35
36 function formatMetadata( $image ) {
37 $meta = $image->getMetadata();
38
39 if ( !$meta ) {
40 return false;
41 }
42 $meta = unserialize( $meta );
43 if ( !isset( $meta['metadata'] ) || count( $meta['metadata'] ) <= 1 ) {
44 return false;
45 }
46
47 if ( isset( $meta['metadata']['_MW_PNG_VERSION'] ) ) {
48 unset( $meta['metadata']['_MW_PNG_VERSION'] );
49 }
50 return $this->formatMetadataHelper( $meta['metadata'] );
51 }
52
53 /**
54 * @param $image File
55 * @return bool
56 */
57 function isAnimatedImage( $image ) {
58 $ser = $image->getMetadata();
59 if ($ser) {
60 $metadata = unserialize($ser);
61 if( $metadata['frameCount'] > 1 ) return true;
62 }
63 return false;
64 }
65
66 function getMetadataType( $image ) {
67 return 'parsed-png';
68 }
69
70 function isMetadataValid( $image, $metadata ) {
71
72 if ( $metadata === self::BROKEN_FILE ) {
73 // Do not repetitivly regenerate metadata on broken file.
74 return self::METADATA_GOOD;
75 }
76
77 wfSuppressWarnings();
78 $data = unserialize( $metadata );
79 wfRestoreWarnings();
80
81 if ( !$data || !is_array( $data ) ) {
82 wfDebug(__METHOD__ . ' invalid png metadata' );
83 return self::METADATA_BAD;
84 }
85
86 if ( !isset( $data['metadata']['_MW_PNG_VERSION'] )
87 || $data['metadata']['_MW_PNG_VERSION'] != PNGMetadataExtractor::VERSION ) {
88 wfDebug(__METHOD__ . ' old but compatible png metadata' );
89 return self::METADATA_COMPATIBLE;
90 }
91 return self::METADATA_GOOD;
92 }
93
94 /**
95 * @param $image File
96 * @return string
97 */
98 function getLongDesc( $image ) {
99 global $wgLang;
100 $original = parent::getLongDesc( $image );
101
102 wfSuppressWarnings();
103 $metadata = unserialize($image->getMetadata());
104 wfRestoreWarnings();
105
106 if( !$metadata || $metadata['frameCount'] <= 0 )
107 return $original;
108
109 $info = array();
110 $info[] = $original;
111
112 if ($metadata['loopCount'] == 0)
113 $info[] = wfMsgExt( 'file-info-png-looped', 'parseinline' );
114 elseif ($metadata['loopCount'] > 1)
115 $info[] = wfMsgExt( 'file-info-png-repeat', 'parseinline', $metadata['loopCount'] );
116
117 if ($metadata['frameCount'] > 0)
118 $info[] = wfMsgExt( 'file-info-png-frames', 'parseinline', $metadata['frameCount'] );
119
120 if ($metadata['duration'])
121 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
122
123 return $wgLang->commaList( $info );
124 }
125
126 }