MediaWiki master
PNGHandler.php
Go to the documentation of this file.
1<?php
26use Wikimedia\RequestTimeout\TimeoutException;
27
34 private const BROKEN_FILE = '0';
35
41 public function getSizeAndMetadata( $state, $filename ) {
42 try {
43 $metadata = BitmapMetadataHandler::PNG( $filename );
44 } catch ( TimeoutException $e ) {
45 throw $e;
46 } catch ( Exception $e ) {
47 // Broken file?
48 wfDebug( __METHOD__ . ': ' . $e->getMessage() );
49
50 return [ 'metadata' => [ '_error' => self::BROKEN_FILE ] ];
51 }
52
53 return [
54 'width' => $metadata['width'],
55 'height' => $metadata['height'],
56 'bits' => $metadata['bitDepth'],
57 'metadata' => array_diff_key(
58 $metadata,
59 [ 'width' => true, 'height' => true, 'bits' => true ]
60 )
61 ];
62 }
63
69 public function formatMetadata( $image, $context = false ) {
70 $meta = $this->getCommonMetaArray( $image );
71 if ( !$meta ) {
72 return false;
73 }
74
75 return $this->formatMetadataHelper( $meta, $context );
76 }
77
84 public function getCommonMetaArray( File $image ) {
85 $meta = $image->getMetadataArray();
86
87 if ( !isset( $meta['metadata'] ) ) {
88 return [];
89 }
90 unset( $meta['metadata']['_MW_PNG_VERSION'] );
91
92 return $meta['metadata'];
93 }
94
99 public function isAnimatedImage( $image ) {
100 $metadata = $image->getMetadataArray();
101 return isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 1;
102 }
103
109 public function canAnimateThumbnail( $image ) {
110 return false;
111 }
112
113 public function getMetadataType( $image ) {
114 return 'parsed-png';
115 }
116
117 public function isFileMetadataValid( $image ) {
118 $data = $image->getMetadataArray();
119 if ( $data === [ '_error' => self::BROKEN_FILE ] ) {
120 // Do not repetitively regenerate metadata on broken file.
121 return self::METADATA_GOOD;
122 }
123
124 if ( !$data || isset( $data['_error'] ) ) {
125 wfDebug( __METHOD__ . " invalid png metadata" );
126
127 return self::METADATA_BAD;
128 }
129
130 if ( !isset( $data['metadata']['_MW_PNG_VERSION'] )
131 || $data['metadata']['_MW_PNG_VERSION'] !== PNGMetadataExtractor::VERSION
132 ) {
133 wfDebug( __METHOD__ . " old but compatible png metadata" );
134
136 }
137
138 return self::METADATA_GOOD;
139 }
140
145 public function getLongDesc( $image ) {
146 global $wgLang;
147 $original = parent::getLongDesc( $image );
148
149 $metadata = $image->getMetadataArray();
150
151 if ( !$metadata || isset( $metadata['_error'] ) || $metadata['frameCount'] <= 0 ) {
152 return $original;
153 }
154
155 $info = [];
156 $info[] = $original;
157
158 if ( $metadata['loopCount'] == 0 ) {
159 $info[] = wfMessage( 'file-info-png-looped' )->parse();
160 } elseif ( $metadata['loopCount'] > 1 ) {
161 $info[] = wfMessage( 'file-info-png-repeat' )->numParams( $metadata['loopCount'] )->parse();
162 }
163
164 if ( $metadata['frameCount'] > 0 ) {
165 $info[] = wfMessage( 'file-info-png-frames' )->numParams( $metadata['frameCount'] )->parse();
166 }
167
168 if ( $metadata['duration'] ) {
169 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
170 }
171
172 return $wgLang->commaList( $info );
173 }
174
183 public function getLength( $file ) {
184 $metadata = $file->getMetadataArray();
185
186 if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) {
187 return 0.0;
188 }
189
190 return (float)$metadata['duration'];
191 }
192
193 // PNGs should be easy to support, but it will need some sharpening applied
194 // and another user test to check if the perceived quality change is noticeable
195 public function supportsBucketing() {
196 return false;
197 }
198}
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:562
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 ...
const METADATA_COMPATIBLE
formatMetadataHelper( $metadataArray, $context=false)
sorts the visible/invisible field.
const METADATA_GOOD
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:93
getMetadataArray()
Get the unserialized handler-specific metadata STUB.
Definition File.php:791
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.