Merge "Change loading order of Chinese conversion tables"
[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 $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 * @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 File $image
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
81 return $meta['metadata'];
82 }
83
84 /**
85 * @param File $image
86 * @return bool
87 */
88 function isAnimatedImage( $image ) {
89 $ser = $image->getMetadata();
90 if ( $ser ) {
91 $metadata = unserialize( $ser );
92 if ( $metadata['frameCount'] > 1 ) {
93 return true;
94 }
95 }
96
97 return false;
98 }
99
100 /**
101 * We do not support making APNG thumbnails, so always false
102 * @param File $image
103 * @return bool False
104 */
105 function canAnimateThumbnail( $image ) {
106 return false;
107 }
108
109 function getMetadataType( $image ) {
110 return 'parsed-png';
111 }
112
113 function isMetadataValid( $image, $metadata ) {
114
115 if ( $metadata === self::BROKEN_FILE ) {
116 // Do not repetitivly regenerate metadata on broken file.
117 return self::METADATA_GOOD;
118 }
119
120 wfSuppressWarnings();
121 $data = unserialize( $metadata );
122 wfRestoreWarnings();
123
124 if ( !$data || !is_array( $data ) ) {
125 wfDebug( __METHOD__ . " invalid png metadata\n" );
126
127 return self::METADATA_BAD;
128 }
129
130 if ( !isset( $data['metadata']['_MW_PNG_VERSION'] )
131 || $data['metadata']['_MW_PNG_VERSION'] != PNGMetadataExtractor::VERSION
132 ) {
133 wfDebug( __METHOD__ . " old but compatible png metadata\n" );
134
135 return self::METADATA_COMPATIBLE;
136 }
137
138 return self::METADATA_GOOD;
139 }
140
141 /**
142 * @param File $image
143 * @return string
144 */
145 function getLongDesc( $image ) {
146 global $wgLang;
147 $original = parent::getLongDesc( $image );
148
149 wfSuppressWarnings();
150 $metadata = unserialize( $image->getMetadata() );
151 wfRestoreWarnings();
152
153 if ( !$metadata || $metadata['frameCount'] <= 0 ) {
154 return $original;
155 }
156
157 $info = array();
158 $info[] = $original;
159
160 if ( $metadata['loopCount'] == 0 ) {
161 $info[] = wfMessage( 'file-info-png-looped' )->parse();
162 } elseif ( $metadata['loopCount'] > 1 ) {
163 $info[] = wfMessage( 'file-info-png-repeat' )->numParams( $metadata['loopCount'] )->parse();
164 }
165
166 if ( $metadata['frameCount'] > 0 ) {
167 $info[] = wfMessage( 'file-info-png-frames' )->numParams( $metadata['frameCount'] )->parse();
168 }
169
170 if ( $metadata['duration'] ) {
171 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
172 }
173
174 return $wgLang->commaList( $info );
175 }
176
177 // PNGs should be easy to support, but it will need some sharpening applied
178 // and another user test to check if the perceived quality change is noticeable
179
180 public function supportsBucketing() {
181 return false;
182 }
183 }