MediaWiki master
XCFHandler.php
Go to the documentation of this file.
1<?php
30
42 public function mustRender( $file ) {
43 return true;
44 }
45
54 public function getThumbType( $ext, $mime, $params = null ) {
55 return [ 'png', 'image/png' ];
56 }
57
68 private static function getXCFMetaData( $filename ) {
69 # Decode master structure
70 $f = fopen( $filename, 'rb' );
71 if ( !$f ) {
72 return null;
73 }
74 # The image structure always starts at offset 0 in the XCF file.
75 # So we just read it :-)
76 $binaryHeader = fread( $f, 26 );
77 fclose( $f );
78
96 try {
98 "A9magic" . # A: space padded
99 "/a5version" . # a: zero padded
100 "/Nwidth" . # \
101 "/Nheight" . # N: unsigned long 32bit big endian
102 "/Nbase_type", # /
103 $binaryHeader
104 );
105 } catch ( UnpackFailedException $_ ) {
106 return null;
107 }
108
109 # Check values
110 if ( $header['magic'] !== 'gimp xcf' ) {
111 wfDebug( __METHOD__ . " '$filename' has invalid magic signature." );
112
113 return null;
114 }
115 # TODO: we might want to check for correct values of width and height
116
117 wfDebug( __METHOD__ .
118 ": canvas size of '$filename' is {$header['width']} x {$header['height']} px" );
119
120 return $header;
121 }
122
123 public function getSizeAndMetadata( $state, $filename ) {
124 $header = self::getXCFMetaData( $filename );
125 $metadata = [];
126 if ( $header ) {
127 // Try to be consistent with the names used by PNG files.
128 // Unclear from base media type if it has an alpha layer,
129 // so just assume that it does since it "potentially" could.
130 switch ( $header['base_type'] ) {
131 case 0:
132 $metadata['colorType'] = 'truecolour-alpha';
133 break;
134 case 1:
135 $metadata['colorType'] = 'greyscale-alpha';
136 break;
137 case 2:
138 $metadata['colorType'] = 'index-coloured';
139 break;
140 default:
141 $metadata['colorType'] = 'unknown';
142 }
143 } else {
144 // Marker to prevent repeated attempted extraction
145 $metadata['error'] = true;
146 }
147 return [
148 'width' => $header['width'] ?? 0,
149 'height' => $header['height'] ?? 0,
150 'bits' => 8,
151 'metadata' => $metadata
152 ];
153 }
154
161 public function isFileMetadataValid( $file ) {
162 if ( !$file->getMetadataArray() ) {
163 // Old metadata when we just put an empty string in there
164 return self::METADATA_BAD;
165 }
166
167 return self::METADATA_GOOD;
168 }
169
170 protected function hasGDSupport() {
171 return false;
172 }
173
182 public function canRender( $file ) {
183 $xcfMeta = $file->getMetadataArray();
184 if ( isset( $xcfMeta['colorType'] ) && $xcfMeta['colorType'] === 'index-coloured' ) {
185 return false;
186 }
187 return parent::canRender( $file );
188 }
189}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
array $params
The job parameters.
Generic handler for bitmap images.
const METADATA_GOOD
static unpack(string $format, string $data, $length=false)
Wrapper around php's unpack.
Handler for the Gimp's native file format; getimagesize() doesn't support these files.
canRender( $file)
Can we render this file?
getThumbType( $ext, $mime, $params=null)
Render files as PNG.
isFileMetadataValid( $file)
Should we refresh the metadata.
hasGDSupport()
Whether the php-gd extension supports this type of file.
mustRender( $file)
getSizeAndMetadata( $state, $filename)
Get image size information and metadata array.
$header