Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / media / PNG.php
blobb8a5b40b2d07211cdddab898b7d0f13c2895199b
1 <?php
2 /**
3 * Handler for PNG images.
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.
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.
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
20 * @file
21 * @ingroup Media
24 /**
25 * Handler for PNG images.
27 * @ingroup Media
29 class PNGHandler extends BitmapHandler {
31 const BROKEN_FILE = '0';
33 /**
34 * @param File $image
35 * @param string $filename
36 * @return string
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;
47 return serialize( $metadata );
50 /**
51 * @param $image File
52 * @return array|bool
54 function formatMetadata( $image ) {
55 $meta = $image->getMetadata();
57 if ( !$meta ) {
58 return false;
60 $meta = unserialize( $meta );
61 if ( !isset( $meta['metadata'] ) || count( $meta['metadata'] ) <= 1 ) {
62 return false;
65 if ( isset( $meta['metadata']['_MW_PNG_VERSION'] ) ) {
66 unset( $meta['metadata']['_MW_PNG_VERSION'] );
68 return $this->formatMetadataHelper( $meta['metadata'] );
71 /**
72 * @param $image File
73 * @return bool
75 function isAnimatedImage( $image ) {
76 $ser = $image->getMetadata();
77 if ( $ser ) {
78 $metadata = unserialize( $ser );
79 if( $metadata['frameCount'] > 1 ) return true;
81 return false;
83 /**
84 * We do not support making APNG thumbnails, so always false
85 * @param $image File
86 * @return bool false
88 function canAnimateThumbnail( $image ) {
89 return false;
92 function getMetadataType( $image ) {
93 return 'parsed-png';
96 function isMetadataValid( $image, $metadata ) {
98 if ( $metadata === self::BROKEN_FILE ) {
99 // Do not repetitivly regenerate metadata on broken file.
100 return self::METADATA_GOOD;
103 wfSuppressWarnings();
104 $data = unserialize( $metadata );
105 wfRestoreWarnings();
107 if ( !$data || !is_array( $data ) ) {
108 wfDebug( __METHOD__ . ' invalid png metadata' );
109 return self::METADATA_BAD;
112 if ( !isset( $data['metadata']['_MW_PNG_VERSION'] )
113 || $data['metadata']['_MW_PNG_VERSION'] != PNGMetadataExtractor::VERSION ) {
114 wfDebug( __METHOD__ . ' old but compatible png metadata' );
115 return self::METADATA_COMPATIBLE;
117 return self::METADATA_GOOD;
121 * @param $image File
122 * @return string
124 function getLongDesc( $image ) {
125 global $wgLang;
126 $original = parent::getLongDesc( $image );
128 wfSuppressWarnings();
129 $metadata = unserialize( $image->getMetadata() );
130 wfRestoreWarnings();
132 if( !$metadata || $metadata['frameCount'] <= 0 )
133 return $original;
135 $info = array();
136 $info[] = $original;
138 if ( $metadata['loopCount'] == 0 ) {
139 $info[] = wfMessage( 'file-info-png-looped' )->parse();
140 } elseif ( $metadata['loopCount'] > 1 ) {
141 $info[] = wfMessage( 'file-info-png-repeat' )->numParams( $metadata['loopCount'] )->parse();
144 if ( $metadata['frameCount'] > 0 ) {
145 $info[] = wfMessage( 'file-info-png-frames' )->numParams( $metadata['frameCount'] )->parse();
148 if ( $metadata['duration'] ) {
149 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
152 return $wgLang->commaList( $info );