MediaWiki master
PNGHandler.php
Go to the documentation of this file.
1<?php
25use Wikimedia\RequestTimeout\TimeoutException;
26
33 private const BROKEN_FILE = '0';
34
40 public function getSizeAndMetadata( $state, $filename ) {
41 try {
42 $metadata = BitmapMetadataHandler::PNG( $filename );
43 } catch ( TimeoutException $e ) {
44 throw $e;
45 } catch ( Exception $e ) {
46 // Broken file?
47 wfDebug( __METHOD__ . ': ' . $e->getMessage() );
48
49 return [ 'metadata' => [ '_error' => self::BROKEN_FILE ] ];
50 }
51
52 return [
53 'width' => $metadata['width'],
54 'height' => $metadata['height'],
55 'bits' => $metadata['bitDepth'],
56 'metadata' => array_diff_key(
57 $metadata,
58 [ 'width' => true, 'height' => true, 'bits' => true ]
59 )
60 ];
61 }
62
68 public function formatMetadata( $image, $context = false ) {
69 $meta = $this->getCommonMetaArray( $image );
70 if ( !$meta ) {
71 return false;
72 }
73
74 return $this->formatMetadataHelper( $meta, $context );
75 }
76
83 public function getCommonMetaArray( File $image ) {
84 $meta = $image->getMetadataArray();
85
86 if ( !isset( $meta['metadata'] ) ) {
87 return [];
88 }
89 unset( $meta['metadata']['_MW_PNG_VERSION'] );
90
91 return $meta['metadata'];
92 }
93
98 public function isAnimatedImage( $image ) {
99 $metadata = $image->getMetadataArray();
100 return isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 1;
101 }
102
108 public function canAnimateThumbnail( $image ) {
109 return false;
110 }
111
112 public function getMetadataType( $image ) {
113 return 'parsed-png';
114 }
115
116 public function isFileMetadataValid( $image ) {
117 $data = $image->getMetadataArray();
118 if ( $data === [ '_error' => self::BROKEN_FILE ] ) {
119 // Do not repetitively regenerate metadata on broken file.
120 return self::METADATA_GOOD;
121 }
122
123 if ( !$data || isset( $data['_error'] ) ) {
124 wfDebug( __METHOD__ . " invalid png metadata" );
125
126 return self::METADATA_BAD;
127 }
128
129 if ( !isset( $data['metadata']['_MW_PNG_VERSION'] )
130 || $data['metadata']['_MW_PNG_VERSION'] !== PNGMetadataExtractor::VERSION
131 ) {
132 wfDebug( __METHOD__ . " old but compatible png metadata" );
133
135 }
136
137 return self::METADATA_GOOD;
138 }
139
144 public function getLongDesc( $image ) {
145 global $wgLang;
146 $original = parent::getLongDesc( $image );
147
148 $metadata = $image->getMetadataArray();
149
150 if ( !$metadata || isset( $metadata['_error'] ) || $metadata['frameCount'] <= 0 ) {
151 return $original;
152 }
153
154 $info = [];
155 $info[] = $original;
156
157 if ( $metadata['loopCount'] == 0 ) {
158 $info[] = wfMessage( 'file-info-png-looped' )->parse();
159 } elseif ( $metadata['loopCount'] > 1 ) {
160 $info[] = wfMessage( 'file-info-png-repeat' )->numParams( $metadata['loopCount'] )->parse();
161 }
162
163 if ( $metadata['frameCount'] > 0 ) {
164 $info[] = wfMessage( 'file-info-png-frames' )->numParams( $metadata['frameCount'] )->parse();
165 }
166
167 if ( $metadata['duration'] ) {
168 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
169 }
170
171 return $wgLang->commaList( $info );
172 }
173
182 public function getLength( $file ) {
183 $metadata = $file->getMetadataArray();
184
185 if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) {
186 return 0.0;
187 }
188
189 return (float)$metadata['duration'];
190 }
191
192 // PNGs should be easy to support, but it will need some sharpening applied
193 // and another user test to check if the perceived quality change is noticeable
194 public function supportsBucketing() {
195 return false;
196 }
197}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(!defined( 'MW_NO_SESSION') &&MW_ENTRY_POINT !=='cli' $wgLang
Definition Setup.php:538
Generic handler for bitmap images.
static PNG( $filename)
Entry point for png At some point in the future this might merge the png various tEXt chunks to that ...
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:74
getMetadataArray()
Get the unserialized handler-specific metadata STUB.
Definition File.php:760
const METADATA_COMPATIBLE
formatMetadataHelper( $metadataArray, $context=false)
sorts the visible/invisible field.
const METADATA_GOOD
Handler for PNG images.
isFileMetadataValid( $image)
Check if the metadata is valid for this handler.
isAnimatedImage( $image)
formatMetadata( $image, $context=false)
getCommonMetaArray(File $image)
Get a file type independent array of metadata.
supportsBucketing()
Returns whether or not this handler supports the chained generation of thumbnails according to bucket...
getLength( $file)
Return the duration of an APNG file.
getLongDesc( $image)
getMetadataType( $image)
Get a string describing the type of metadata, for display purposes.
getSizeAndMetadata( $state, $filename)
canAnimateThumbnail( $image)
We do not support making APNG thumbnails, so always false.
Interface for objects which can provide a MediaWiki context on request.