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 ( !$metadata || isset( $metadata['_error'] ) || $metadata['frameCount'] <= 0 ) {
143 return $original;
144 }
145
146 $info = [];
147 $info[] = $original;
148
149 if ( $metadata['loopCount'] == 0 ) {
150 $info[] = wfMessage( 'file-info-png-looped' )->inLanguage( $lang )->parse();
151 } elseif ( $metadata['loopCount'] > 1 ) {
152 $info[] = wfMessage( 'file-info-png-repeat' )->numParams( $metadata['loopCount'] )
153 ->inLanguage( $lang )->parse();
154 }
155
156 if ( $metadata['frameCount'] > 0 ) {
157 $info[] = wfMessage( 'file-info-png-frames' )->numParams( $metadata['frameCount'] )
158 ->inLanguage( $lang )->parse();
159 }
160
161 if ( $metadata['duration'] ) {
162 $info[] = htmlspecialchars( $lang->formatTimePeriod( $metadata['duration'] ), ENT_QUOTES );
163 }
164
165 return $lang->commaList( $info );
166 }
167
176 public function getLength( $file ) {
177 $metadata = $file->getMetadataArray();
178
179 if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) {
180 return 0.0;
181 }
182
183 return (float)$metadata['duration'];
184 }
185
191 public function supportsBucketing() {
192 return false;
193 }
194}
195
197class_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:829
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.