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 );
54 function formatMetadata( $image ) {
55 $meta = $this->getCommonMetaArray( $image );
56 if ( count( $meta ) === 0 ) {
60 return $this->formatMetadataHelper( $meta );
64 * Get a file type independent array of metadata.
67 * @return array The metadata array
69 public function getCommonMetaArray( File
$image ) {
70 $meta = $image->getMetadata();
75 $meta = unserialize( $meta );
76 if ( !isset( $meta['metadata'] ) ) {
79 unset( $meta['metadata']['_MW_PNG_VERSION'] );
81 return $meta['metadata'];
88 function isAnimatedImage( $image ) {
89 $ser = $image->getMetadata();
91 $metadata = unserialize( $ser );
92 if ( $metadata['frameCount'] > 1 ) {
101 * We do not support making APNG thumbnails, so always false
105 function canAnimateThumbnail( $image ) {
109 function getMetadataType( $image ) {
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 );
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
;
145 function getLongDesc( $image ) {
147 $original = parent
::getLongDesc( $image );
149 wfSuppressWarnings();
150 $metadata = unserialize( $image->getMetadata() );
153 if ( !$metadata ||
$metadata['frameCount'] <= 0 ) {
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() {