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'] ) ) {
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 global $wgLang;
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' )->parse();
151 } elseif ( $metadata['loopCount'] > 1 ) {
152 $info[] = wfMessage( 'file-info-png-repeat' )->numParams( $metadata['loopCount'] )->parse();
153 }
154
155 if ( $metadata['frameCount'] > 0 ) {
156 $info[] = wfMessage( 'file-info-png-frames' )->numParams( $metadata['frameCount'] )->parse();
157 }
158
159 if ( $metadata['duration'] ) {
160 $info[] = htmlspecialchars( $wgLang->formatTimePeriod( $metadata['duration'] ), ENT_QUOTES );
161 }
162
163 return $wgLang->commaList( $info );
164 }
165
174 public function getLength( $file ) {
175 $metadata = $file->getMetadataArray();
176
177 if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) {
178 return 0.0;
179 }
180
181 return (float)$metadata['duration'];
182 }
183
189 public function supportsBucketing() {
190 return false;
191 }
192}
193
195class_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.
if(MW_ENTRY_POINT==='index') if(!defined( 'MW_NO_SESSION') &&MW_ENTRY_POINT !=='cli' $wgLang
Definition Setup.php:551
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:79
getMetadataArray()
Get the unserialized handler-specific metadata STUB.
Definition File.php:778
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 ...
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.