Merge "Add CollationFa"
[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 const BROKEN_FILE = '0';
31
32 /**
33 * @param File|FSFile $image
34 * @param string $filename
35 * @return string
36 */
37 function getMetadata( $image, $filename ) {
38 try {
39 $metadata = BitmapMetadataHandler::PNG( $filename );
40 } catch ( Exception $e ) {
41 // Broken file?
42 wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
43
44 return self::BROKEN_FILE;
45 }
46
47 return serialize( $metadata );
48 }
49
50 /**
51 * @param File $image
52 * @param bool|IContextSource $context Context to use (optional)
53 * @return array|bool
54 */
55 function formatMetadata( $image, $context = false ) {
56 $meta = $this->getCommonMetaArray( $image );
57 if ( count( $meta ) === 0 ) {
58 return false;
59 }
60
61 return $this->formatMetadataHelper( $meta, $context );
62 }
63
64 /**
65 * Get a file type independent array of metadata.
66 *
67 * @param File $image
68 * @return array The metadata array
69 */
70 public function getCommonMetaArray( File $image ) {
71 $meta = $image->getMetadata();
72
73 if ( !$meta ) {
74 return [];
75 }
76 $meta = unserialize( $meta );
77 if ( !isset( $meta['metadata'] ) ) {
78 return [];
79 }
80 unset( $meta['metadata']['_MW_PNG_VERSION'] );
81
82 return $meta['metadata'];
83 }
84
85 /**
86 * @param File $image
87 * @return bool
88 */
89 function isAnimatedImage( $image ) {
90 $ser = $image->getMetadata();
91 if ( $ser ) {
92 $metadata = unserialize( $ser );
93 if ( $metadata['frameCount'] > 1 ) {
94 return true;
95 }
96 }
97
98 return false;
99 }
100
101 /**
102 * We do not support making APNG thumbnails, so always false
103 * @param File $image
104 * @return bool False
105 */
106 function canAnimateThumbnail( $image ) {
107 return false;
108 }
109
110 function getMetadataType( $image ) {
111 return 'parsed-png';
112 }
113
114 function isMetadataValid( $image, $metadata ) {
115
116 if ( $metadata === self::BROKEN_FILE ) {
117 // Do not repetitivly regenerate metadata on broken file.
118 return self::METADATA_GOOD;
119 }
120
121 MediaWiki\suppressWarnings();
122 $data = unserialize( $metadata );
123 MediaWiki\restoreWarnings();
124
125 if ( !$data || !is_array( $data ) ) {
126 wfDebug( __METHOD__ . " invalid png metadata\n" );
127
128 return self::METADATA_BAD;
129 }
130
131 if ( !isset( $data['metadata']['_MW_PNG_VERSION'] )
132 || $data['metadata']['_MW_PNG_VERSION'] != PNGMetadataExtractor::VERSION
133 ) {
134 wfDebug( __METHOD__ . " old but compatible png metadata\n" );
135
136 return self::METADATA_COMPATIBLE;
137 }
138
139 return self::METADATA_GOOD;
140 }
141
142 /**
143 * @param File $image
144 * @return string
145 */
146 function getLongDesc( $image ) {
147 global $wgLang;
148 $original = parent::getLongDesc( $image );
149
150 MediaWiki\suppressWarnings();
151 $metadata = unserialize( $image->getMetadata() );
152 MediaWiki\restoreWarnings();
153
154 if ( !$metadata || $metadata['frameCount'] <= 0 ) {
155 return $original;
156 }
157
158 $info = [];
159 $info[] = $original;
160
161 if ( $metadata['loopCount'] == 0 ) {
162 $info[] = wfMessage( 'file-info-png-looped' )->parse();
163 } elseif ( $metadata['loopCount'] > 1 ) {
164 $info[] = wfMessage( 'file-info-png-repeat' )->numParams( $metadata['loopCount'] )->parse();
165 }
166
167 if ( $metadata['frameCount'] > 0 ) {
168 $info[] = wfMessage( 'file-info-png-frames' )->numParams( $metadata['frameCount'] )->parse();
169 }
170
171 if ( $metadata['duration'] ) {
172 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
173 }
174
175 return $wgLang->commaList( $info );
176 }
177
178 /**
179 * Return the duration of an APNG file.
180 *
181 * Shown in the &query=imageinfo&iiprop=size api query.
182 *
183 * @param File $file
184 * @return float The duration of the file.
185 */
186 public function getLength( $file ) {
187 $serMeta = $file->getMetadata();
188 MediaWiki\suppressWarnings();
189 $metadata = unserialize( $serMeta );
190 MediaWiki\restoreWarnings();
191
192 if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) {
193 return 0.0;
194 } else {
195 return (float)$metadata['duration'];
196 }
197 }
198
199 // PNGs should be easy to support, but it will need some sharpening applied
200 // and another user test to check if the perceived quality change is noticeable
201 public function supportsBucketing() {
202 return false;
203 }
204 }