Merge "mw.language.convertPlural: Check if matching form exists"
[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 = $this->getCommonMetaArray( $image );
56 if ( count( $meta ) === 0 ) {
57 return false;
58 }
59
60 return $this->formatMetadataHelper( $meta );
61 }
62
63 /**
64 * Get a file type independent array of metadata.
65 *
66 * @param $image File
67 * @return array The metadata array
68 */
69 public function getCommonMetaArray( File $image ) {
70 $meta = $image->getMetadata();
71
72 if ( !$meta ) {
73 return array();
74 }
75 $meta = unserialize( $meta );
76 if ( !isset( $meta['metadata'] ) ) {
77 return array();
78 }
79 unset( $meta['metadata']['_MW_PNG_VERSION'] );
80 return $meta['metadata'];
81 }
82
83 /**
84 * @param $image File
85 * @return bool
86 */
87 function isAnimatedImage( $image ) {
88 $ser = $image->getMetadata();
89 if ( $ser ) {
90 $metadata = unserialize( $ser );
91 if ( $metadata['frameCount'] > 1 ) {
92 return true;
93 }
94 }
95 return false;
96 }
97 /**
98 * We do not support making APNG thumbnails, so always false
99 * @param $image File
100 * @return bool false
101 */
102 function canAnimateThumbnail( $image ) {
103 return false;
104 }
105
106 function getMetadataType( $image ) {
107 return 'parsed-png';
108 }
109
110 function isMetadataValid( $image, $metadata ) {
111
112 if ( $metadata === self::BROKEN_FILE ) {
113 // Do not repetitivly regenerate metadata on broken file.
114 return self::METADATA_GOOD;
115 }
116
117 wfSuppressWarnings();
118 $data = unserialize( $metadata );
119 wfRestoreWarnings();
120
121 if ( !$data || !is_array( $data ) ) {
122 wfDebug( __METHOD__ . " invalid png metadata\n" );
123 return self::METADATA_BAD;
124 }
125
126 if ( !isset( $data['metadata']['_MW_PNG_VERSION'] )
127 || $data['metadata']['_MW_PNG_VERSION'] != PNGMetadataExtractor::VERSION ) {
128 wfDebug( __METHOD__ . " old but compatible png metadata\n" );
129 return self::METADATA_COMPATIBLE;
130 }
131 return self::METADATA_GOOD;
132 }
133
134 /**
135 * @param $image File
136 * @return string
137 */
138 function getLongDesc( $image ) {
139 global $wgLang;
140 $original = parent::getLongDesc( $image );
141
142 wfSuppressWarnings();
143 $metadata = unserialize( $image->getMetadata() );
144 wfRestoreWarnings();
145
146 if ( !$metadata || $metadata['frameCount'] <= 0 ) {
147 return $original;
148 }
149
150 $info = array();
151 $info[] = $original;
152
153 if ( $metadata['loopCount'] == 0 ) {
154 $info[] = wfMessage( 'file-info-png-looped' )->parse();
155 } elseif ( $metadata['loopCount'] > 1 ) {
156 $info[] = wfMessage( 'file-info-png-repeat' )->numParams( $metadata['loopCount'] )->parse();
157 }
158
159 if ( $metadata['frameCount'] > 0 ) {
160 $info[] = wfMessage( 'file-info-png-frames' )->numParams( $metadata['frameCount'] )->parse();
161 }
162
163 if ( $metadata['duration'] ) {
164 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
165 }
166
167 return $wgLang->commaList( $info );
168 }
169
170 }