Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / media / PNG.php
blob3cf8488d74040a9934ed8ce8cc09237b9be45434
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 {
30 const BROKEN_FILE = '0';
32 /**
33 * @param File $image
34 * @param string $filename
35 * @return string
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" );
44 return self::BROKEN_FILE;
47 return serialize( $metadata );
50 /**
51 * @param File $image
52 * @return array|bool
54 function formatMetadata( $image ) {
55 $meta = $this->getCommonMetaArray( $image );
56 if ( count( $meta ) === 0 ) {
57 return false;
60 return $this->formatMetadataHelper( $meta );
63 /**
64 * Get a file type independent array of metadata.
66 * @param File $image
67 * @return array The metadata array
69 public function getCommonMetaArray( File $image ) {
70 $meta = $image->getMetadata();
72 if ( !$meta ) {
73 return array();
75 $meta = unserialize( $meta );
76 if ( !isset( $meta['metadata'] ) ) {
77 return array();
79 unset( $meta['metadata']['_MW_PNG_VERSION'] );
81 return $meta['metadata'];
84 /**
85 * @param File $image
86 * @return bool
88 function isAnimatedImage( $image ) {
89 $ser = $image->getMetadata();
90 if ( $ser ) {
91 $metadata = unserialize( $ser );
92 if ( $metadata['frameCount'] > 1 ) {
93 return true;
97 return false;
101 * We do not support making APNG thumbnails, so always false
102 * @param File $image
103 * @return bool False
105 function canAnimateThumbnail( $image ) {
106 return false;
109 function getMetadataType( $image ) {
110 return 'parsed-png';
113 function isMetadataValid( $image, $metadata ) {
115 if ( $metadata === self::BROKEN_FILE ) {
116 // Do not repetitivly regenerate metadata on broken file.
117 return self::METADATA_GOOD;
120 wfSuppressWarnings();
121 $data = unserialize( $metadata );
122 wfRestoreWarnings();
124 if ( !$data || !is_array( $data ) ) {
125 wfDebug( __METHOD__ . " invalid png metadata\n" );
127 return self::METADATA_BAD;
130 if ( !isset( $data['metadata']['_MW_PNG_VERSION'] )
131 || $data['metadata']['_MW_PNG_VERSION'] != PNGMetadataExtractor::VERSION
133 wfDebug( __METHOD__ . " old but compatible png metadata\n" );
135 return self::METADATA_COMPATIBLE;
138 return self::METADATA_GOOD;
142 * @param File $image
143 * @return string
145 function getLongDesc( $image ) {
146 global $wgLang;
147 $original = parent::getLongDesc( $image );
149 wfSuppressWarnings();
150 $metadata = unserialize( $image->getMetadata() );
151 wfRestoreWarnings();
153 if ( !$metadata || $metadata['frameCount'] <= 0 ) {
154 return $original;
157 $info = array();
158 $info[] = $original;
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();
166 if ( $metadata['frameCount'] > 0 ) {
167 $info[] = wfMessage( 'file-info-png-frames' )->numParams( $metadata['frameCount'] )->parse();
170 if ( $metadata['duration'] ) {
171 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
174 return $wgLang->commaList( $info );
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
180 public function supportsBucketing() {
181 return false;