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
25 * Handler for PNG images.
29 class PNGHandler
extends BitmapHandler
{
30 const BROKEN_FILE
= '0';
34 * @param string $filename
37 function getMetadata( $image, $filename ) {
39 $metadata = BitmapMetadataHandler
::PNG( $filename );
40 } catch ( Exception
$e ) {
42 wfDebug( __METHOD__
. ': ' . $e->getMessage() . "\n" );
44 return self
::BROKEN_FILE
;
47 return serialize( $metadata );
52 * @param bool|IContextSource $context Context to use (optional)
55 function formatMetadata( $image, $context = false ) {
56 $meta = $this->getCommonMetaArray( $image );
57 if ( count( $meta ) === 0 ) {
61 return $this->formatMetadataHelper( $meta, $context );
65 * Get a file type independent array of metadata.
68 * @return array The metadata array
70 public function getCommonMetaArray( File
$image ) {
71 $meta = $image->getMetadata();
76 $meta = unserialize( $meta );
77 if ( !isset( $meta['metadata'] ) ) {
80 unset( $meta['metadata']['_MW_PNG_VERSION'] );
82 return $meta['metadata'];
89 function isAnimatedImage( $image ) {
90 $ser = $image->getMetadata();
92 $metadata = unserialize( $ser );
93 if ( $metadata['frameCount'] > 1 ) {
102 * We do not support making APNG thumbnails, so always false
106 function canAnimateThumbnail( $image ) {
110 function getMetadataType( $image ) {
114 function isMetadataValid( $image, $metadata ) {
116 if ( $metadata === self
::BROKEN_FILE
) {
117 // Do not repetitivly regenerate metadata on broken file.
118 return self
::METADATA_GOOD
;
121 MediaWiki\
suppressWarnings();
122 $data = unserialize( $metadata );
123 MediaWiki\restoreWarnings
();
125 if ( !$data ||
!is_array( $data ) ) {
126 wfDebug( __METHOD__
. " invalid png metadata\n" );
128 return self
::METADATA_BAD
;
131 if ( !isset( $data['metadata']['_MW_PNG_VERSION'] )
132 ||
$data['metadata']['_MW_PNG_VERSION'] != PNGMetadataExtractor
::VERSION
134 wfDebug( __METHOD__
. " old but compatible png metadata\n" );
136 return self
::METADATA_COMPATIBLE
;
139 return self
::METADATA_GOOD
;
146 function getLongDesc( $image ) {
148 $original = parent
::getLongDesc( $image );
150 MediaWiki\
suppressWarnings();
151 $metadata = unserialize( $image->getMetadata() );
152 MediaWiki\restoreWarnings
();
154 if ( !$metadata ||
$metadata['frameCount'] <= 0 ) {
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();
167 if ( $metadata['frameCount'] > 0 ) {
168 $info[] = wfMessage( 'file-info-png-frames' )->numParams( $metadata['frameCount'] )->parse();
171 if ( $metadata['duration'] ) {
172 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
175 return $wgLang->commaList( $info );
179 * Return the duration of an APNG file.
181 * Shown in the &query=imageinfo&iiprop=size api query.
184 * @return float The duration of the file.
186 public function getLength( $file ) {
187 $serMeta = $file->getMetadata();
188 MediaWiki\
suppressWarnings();
189 $metadata = unserialize( $serMeta );
190 MediaWiki\restoreWarnings
();
192 if ( !$metadata ||
!isset( $metadata['duration'] ) ||
!$metadata['duration'] ) {
195 return (float)$metadata['duration'];
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() {