Few more comment/whitespace issues from r86169
[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 * @param File $image
20 * @param string $filename
21 * @return string
22 */
23 function getMetadata( $image, $filename ) {
24 try {
25 $metadata = BitmapMetadataHandler::PNG( $filename );
26 } catch( Exception $e ) {
27 // Broken file?
28 wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
29 return self::BROKEN_FILE;
30 }
31
32 return serialize($metadata);
33 }
34
35 function formatMetadata( $image ) {
36 $meta = $image->getMetadata();
37
38 if ( !$meta ) {
39 return false;
40 }
41 $meta = unserialize( $meta );
42 if ( !isset( $meta['metadata'] ) || count( $meta['metadata'] ) <= 1 ) {
43 return false;
44 }
45
46 if ( isset( $meta['metadata']['_MW_PNG_VERSION'] ) ) {
47 unset( $meta['metadata']['_MW_PNG_VERSION'] );
48 }
49 return $this->formatMetadataHelper( $meta['metadata'] );
50 }
51
52 /**
53 * @param $image File
54 * @return bool
55 */
56 function isAnimatedImage( $image ) {
57 $ser = $image->getMetadata();
58 if ($ser) {
59 $metadata = unserialize($ser);
60 if( $metadata['frameCount'] > 1 ) return true;
61 }
62 return false;
63 }
64
65 function getMetadataType( $image ) {
66 return 'parsed-png';
67 }
68
69 function isMetadataValid( $image, $metadata ) {
70
71 if ( $metadata === self::BROKEN_FILE ) {
72 // Do not repetitivly regenerate metadata on broken file.
73 return self::METADATA_GOOD;
74 }
75
76 wfSuppressWarnings();
77 $data = unserialize( $metadata );
78 wfRestoreWarnings();
79
80 if ( !$data || !is_array( $data ) ) {
81 wfDebug(__METHOD__ . ' invalid png metadata' );
82 return self::METADATA_BAD;
83 }
84
85 if ( !isset( $data['metadata']['_MW_PNG_VERSION'] )
86 || $data['metadata']['_MW_PNG_VERSION'] != PNGMetadataExtractor::VERSION ) {
87 wfDebug(__METHOD__ . ' old but compatible png metadata' );
88 return self::METADATA_COMPATIBLE;
89 }
90 return self::METADATA_GOOD;
91 }
92
93 /**
94 * @param $image File
95 * @return string
96 */
97 function getLongDesc( $image ) {
98 global $wgLang;
99 $original = parent::getLongDesc( $image );
100
101 wfSuppressWarnings();
102 $metadata = unserialize($image->getMetadata());
103 wfRestoreWarnings();
104
105 if( !$metadata || $metadata['frameCount'] <= 0 )
106 return $original;
107
108 $info = array();
109 $info[] = $original;
110
111 if ( $metadata['loopCount'] == 0 ) {
112 $info[] = wfMsgExt( 'file-info-png-looped', 'parseinline' );
113 } elseif ( $metadata['loopCount'] > 1 ) {
114 $info[] = wfMsgExt( 'file-info-png-repeat', 'parseinline', $metadata['loopCount'] );
115 }
116
117 if ( $metadata['frameCount'] > 0 ) {
118 $info[] = wfMsgExt( 'file-info-png-frames', 'parseinline', $metadata['frameCount'] );
119 }
120
121 if ( $metadata['duration'] ) {
122 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
123 }
124
125 return $wgLang->commaList( $info );
126 }
127
128 }