MediaWiki master
PNGHandler.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\Media;
11
12use Exception;
15use Wikimedia\RequestTimeout\TimeoutException;
16
23 private const BROKEN_FILE = '0';
24
30 public function getSizeAndMetadata( $state, $filename ) {
31 try {
32 $metadata = BitmapMetadataHandler::PNG( $filename );
33 } catch ( TimeoutException $e ) {
34 throw $e;
35 } catch ( Exception $e ) {
36 // Broken file?
37 wfDebug( __METHOD__ . ': ' . $e->getMessage() );
38
39 return [ 'metadata' => [ '_error' => self::BROKEN_FILE ] ];
40 }
41
42 return [
43 'width' => $metadata['width'],
44 'height' => $metadata['height'],
45 'bits' => $metadata['bitDepth'],
46 'metadata' => array_diff_key(
47 $metadata,
48 [ 'width' => true, 'height' => true, 'bits' => true ]
49 )
50 ];
51 }
52
58 public function formatMetadata( $image, $context = false ) {
59 $meta = $this->getCommonMetaArray( $image );
60 if ( !$meta ) {
61 return false;
62 }
63
64 return $this->formatMetadataHelper( $meta, $context );
65 }
66
73 public function getCommonMetaArray( File $image ) {
74 $meta = $image->getMetadataArray();
75
76 if ( !isset( $meta['metadata'] ) || !is_array( $meta['metadata'] ) ) {
77 return [];
78 }
79 unset( $meta['metadata']['_MW_PNG_VERSION'] );
80
81 return $meta['metadata'];
82 }
83
88 public function isAnimatedImage( $image ) {
89 $metadata = $image->getMetadataArray();
90 return isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 1;
91 }
92
98 public function canAnimateThumbnail( $image ) {
99 return false;
100 }
101
103 public function getMetadataType( $image ) {
104 return 'parsed-png';
105 }
106
108 public function isFileMetadataValid( $image ) {
109 $data = $image->getMetadataArray();
110 if ( $data === [ '_error' => self::BROKEN_FILE ] ) {
111 // Do not repetitively regenerate metadata on broken file.
112 return self::METADATA_GOOD;
113 }
114
115 if ( !$data || isset( $data['_error'] ) ) {
116 wfDebug( __METHOD__ . " invalid png metadata" );
117
118 return self::METADATA_BAD;
119 }
120
121 if ( !isset( $data['metadata']['_MW_PNG_VERSION'] )
122 || $data['metadata']['_MW_PNG_VERSION'] !== PNGMetadataExtractor::VERSION
123 ) {
124 wfDebug( __METHOD__ . " old but compatible png metadata" );
125
127 }
128
129 return self::METADATA_GOOD;
130 }
131
136 public function getLongDesc( $image ) {
137 $lang = $this->getLanguage();
138 $original = parent::getLongDesc( $image );
139
140 $metadata = $image->getMetadataArray();
141
142 if (
143 !$metadata || isset( $metadata['_error'] ) ||
144 !isset( $metadata['frameCount'] ) || $metadata['frameCount'] <= 0
145 ) {
146 return $original;
147 }
148
149 $info = [];
150 $info[] = $original;
151
152 if ( $metadata['loopCount'] == 0 ) {
153 $info[] = wfMessage( 'file-info-png-looped' )->inLanguage( $lang )->parse();
154 } elseif ( $metadata['loopCount'] > 1 ) {
155 $info[] = wfMessage( 'file-info-png-repeat' )->numParams( $metadata['loopCount'] )
156 ->inLanguage( $lang )->parse();
157 }
158
159 if ( $metadata['frameCount'] > 0 ) {
160 $info[] = wfMessage( 'file-info-png-frames' )->numParams( $metadata['frameCount'] )
161 ->inLanguage( $lang )->parse();
162 }
163
164 if ( $metadata['duration'] ) {
165 $info[] = htmlspecialchars( $lang->formatTimePeriod( $metadata['duration'] ), ENT_QUOTES );
166 }
167
168 return $lang->commaList( $info );
169 }
170
179 public function getLength( $file ) {
180 $metadata = $file->getMetadataArray();
181
182 if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) {
183 return 0.0;
184 }
185
186 return (float)$metadata['duration'];
187 }
188
194 public function supportsBucketing() {
195 return false;
196 }
197}
198
200class_alias( PNGHandler::class, 'PNGHandler' );
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.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:80
getMetadataArray()
Get the unserialized handler-specific metadata STUB.
Definition File.php:830
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 ...
getLanguage()
Get the language to be used by this media handler for text formatting.
formatMetadataHelper( $metadataArray, $context=false)
sorts the visible/invisible field.
Handler for PNG images.
getMetadataType( $image)
Get a string describing the type of metadata, for display purposes.to overrideThis method is currentl...
isFileMetadataValid( $image)
Check if the metadata is valid for this handler.If it returns MediaHandler::METADATA_BAD (or false),...
canAnimateThumbnail( $image)
We do not support making APNG thumbnails, so always false.
formatMetadata( $image, $context=false)
getLength( $file)
Return the duration of an APNG file.
getSizeAndMetadata( $state, $filename)
getCommonMetaArray(File $image)
Get a file type independent array of metadata.
supportsBucketing()
PNGs should be easy to support, but it will need some sharpening applied and another user test to che...
Interface for objects which can provide a MediaWiki context on request.