MediaWiki master
XCFHandler.php
Go to the documentation of this file.
1<?php
15namespace MediaWiki\Media;
16
20
32 public function mustRender( $file ) {
33 return true;
34 }
35
44 public function getThumbType( $ext, $mime, $params = null ) {
45 return [ 'png', 'image/png' ];
46 }
47
58 private static function getXCFMetaData( $filename ) {
59 # Decode master structure
60 $f = fopen( $filename, 'rb' );
61 if ( !$f ) {
62 return null;
63 }
64 # The image structure always starts at offset 0 in the XCF file.
65 # So we just read it :-)
66 $binaryHeader = fread( $f, 26 );
67 fclose( $f );
68
86 try {
87 $header = StringUtils::unpack(
88 "A9magic" . # A: space padded
89 "/a5version" . # a: zero padded
90 "/Nwidth" . # \
91 "/Nheight" . # N: unsigned long 32bit big endian
92 "/Nbase_type", # /
93 $binaryHeader
94 );
95 } catch ( UnpackFailedException ) {
96 return null;
97 }
98
99 # Check values
100 if ( $header['magic'] !== 'gimp xcf' ) {
101 wfDebug( __METHOD__ . " '$filename' has invalid magic signature." );
102
103 return null;
104 }
105 # TODO: we might want to check for correct values of width and height
106
107 wfDebug( __METHOD__ .
108 ": canvas size of '$filename' is {$header['width']} x {$header['height']} px" );
109
110 return $header;
111 }
112
114 public function getSizeAndMetadata( $state, $filename ) {
115 $header = self::getXCFMetaData( $filename );
116 $metadata = [];
117 if ( $header ) {
118 // Try to be consistent with the names used by PNG files.
119 // Unclear from base media type if it has an alpha layer,
120 // so just assume that it does since it "potentially" could.
121 switch ( $header['base_type'] ) {
122 case 0:
123 $metadata['colorType'] = 'truecolour-alpha';
124 break;
125 case 1:
126 $metadata['colorType'] = 'greyscale-alpha';
127 break;
128 case 2:
129 $metadata['colorType'] = 'index-coloured';
130 break;
131 default:
132 $metadata['colorType'] = 'unknown';
133 }
134 } else {
135 // Marker to prevent repeated attempted extraction
136 $metadata['error'] = true;
137 }
138 return [
139 'width' => $header['width'] ?? 0,
140 'height' => $header['height'] ?? 0,
141 'bits' => 8,
142 'metadata' => $metadata
143 ];
144 }
145
152 public function isFileMetadataValid( $file ) {
153 if ( !$file->getMetadataArray() ) {
154 // Old metadata when we just put an empty string in there
155 return self::METADATA_BAD;
156 }
157
158 return self::METADATA_GOOD;
159 }
160
162 protected function hasGDSupport() {
163 return false;
164 }
165
174 public function canRender( $file ) {
175 $xcfMeta = $file->getMetadataArray();
176 if ( isset( $xcfMeta['colorType'] ) && $xcfMeta['colorType'] === 'index-coloured' ) {
177 return false;
178 }
179 return parent::canRender( $file );
180 }
181}
182
184class_alias( XCFHandler::class, 'XCFHandler' );
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:79
Generic handler for bitmap images.
Handler for the Gimp's native file format; getimagesize() doesn't support these files.
isFileMetadataValid( $file)
Should we refresh the metadata.
hasGDSupport()
Whether the php-gd extension supports this type of file.to override bool
canRender( $file)
Can we render this file?
getSizeAndMetadata( $state, $filename)
Get image size information and metadata array.If this returns null, the caller will fall back to getI...
getThumbType( $ext, $mime, $params=null)
Render files as PNG.
A collection of static methods to play with strings.