Fixing documentation for memcached.
[lhc/web/wiklou.git] / includes / media / PNG.php
1 <?php
2 /**
3 * Handler for PNG images.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Media
22 */
23
24 /**
25 * Handler for PNG images.
26 *
27 * @ingroup Media
28 */
29 class PNGHandler extends BitmapHandler {
30
31 const BROKEN_FILE = '0';
32
33 /**
34 * @param File $image
35 * @param string $filename
36 * @return string
37 */
38 function getMetadata( $image, $filename ) {
39 try {
40 $metadata = BitmapMetadataHandler::PNG( $filename );
41 } catch ( Exception $e ) {
42 // Broken file?
43 wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
44 return self::BROKEN_FILE;
45 }
46
47 return serialize( $metadata );
48 }
49
50 /**
51 * @param $image File
52 * @return array|bool
53 */
54 function formatMetadata( $image ) {
55 $meta = $image->getMetadata();
56
57 if ( !$meta ) {
58 return false;
59 }
60 $meta = unserialize( $meta );
61 if ( !isset( $meta['metadata'] ) || count( $meta['metadata'] ) <= 1 ) {
62 return false;
63 }
64
65 if ( isset( $meta['metadata']['_MW_PNG_VERSION'] ) ) {
66 unset( $meta['metadata']['_MW_PNG_VERSION'] );
67 }
68 return $this->formatMetadataHelper( $meta['metadata'] );
69 }
70
71 /**
72 * @param $image File
73 * @return bool
74 */
75 function isAnimatedImage( $image ) {
76 $ser = $image->getMetadata();
77 if ( $ser ) {
78 $metadata = unserialize( $ser );
79 if ( $metadata['frameCount'] > 1 ) {
80 return true;
81 }
82 }
83 return false;
84 }
85 /**
86 * We do not support making APNG thumbnails, so always false
87 * @param $image File
88 * @return bool false
89 */
90 function canAnimateThumbnail( $image ) {
91 return false;
92 }
93
94 function getMetadataType( $image ) {
95 return 'parsed-png';
96 }
97
98 function isMetadataValid( $image, $metadata ) {
99
100 if ( $metadata === self::BROKEN_FILE ) {
101 // Do not repetitivly regenerate metadata on broken file.
102 return self::METADATA_GOOD;
103 }
104
105 wfSuppressWarnings();
106 $data = unserialize( $metadata );
107 wfRestoreWarnings();
108
109 if ( !$data || !is_array( $data ) ) {
110 wfDebug( __METHOD__ . " invalid png metadata\n" );
111 return self::METADATA_BAD;
112 }
113
114 if ( !isset( $data['metadata']['_MW_PNG_VERSION'] )
115 || $data['metadata']['_MW_PNG_VERSION'] != PNGMetadataExtractor::VERSION ) {
116 wfDebug( __METHOD__ . " old but compatible png metadata\n" );
117 return self::METADATA_COMPATIBLE;
118 }
119 return self::METADATA_GOOD;
120 }
121
122 /**
123 * @param $image File
124 * @return string
125 */
126 function getLongDesc( $image ) {
127 global $wgLang;
128 $original = parent::getLongDesc( $image );
129
130 wfSuppressWarnings();
131 $metadata = unserialize( $image->getMetadata() );
132 wfRestoreWarnings();
133
134 if ( !$metadata || $metadata['frameCount'] <= 0 ) {
135 return $original;
136 }
137
138 $info = array();
139 $info[] = $original;
140
141 if ( $metadata['loopCount'] == 0 ) {
142 $info[] = wfMessage( 'file-info-png-looped' )->parse();
143 } elseif ( $metadata['loopCount'] > 1 ) {
144 $info[] = wfMessage( 'file-info-png-repeat' )->numParams( $metadata['loopCount'] )->parse();
145 }
146
147 if ( $metadata['frameCount'] > 0 ) {
148 $info[] = wfMessage( 'file-info-png-frames' )->numParams( $metadata['frameCount'] )->parse();
149 }
150
151 if ( $metadata['duration'] ) {
152 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
153 }
154
155 return $wgLang->commaList( $info );
156 }
157
158 }