MediaWiki master
XCFHandler.php
Go to the documentation of this file.
1<?php
32
44 public function mustRender( $file ) {
45 return true;
46 }
47
56 public function getThumbType( $ext, $mime, $params = null ) {
57 return [ 'png', 'image/png' ];
58 }
59
70 private static function getXCFMetaData( $filename ) {
71 # Decode master structure
72 $f = fopen( $filename, 'rb' );
73 if ( !$f ) {
74 return null;
75 }
76 # The image structure always starts at offset 0 in the XCF file.
77 # So we just read it :-)
78 $binaryHeader = fread( $f, 26 );
79 fclose( $f );
80
98 try {
99 $header = StringUtils::unpack(
100 "A9magic" . # A: space padded
101 "/a5version" . # a: zero padded
102 "/Nwidth" . # \
103 "/Nheight" . # N: unsigned long 32bit big endian
104 "/Nbase_type", # /
105 $binaryHeader
106 );
107 } catch ( UnpackFailedException ) {
108 return null;
109 }
110
111 # Check values
112 if ( $header['magic'] !== 'gimp xcf' ) {
113 wfDebug( __METHOD__ . " '$filename' has invalid magic signature." );
114
115 return null;
116 }
117 # TODO: we might want to check for correct values of width and height
118
119 wfDebug( __METHOD__ .
120 ": canvas size of '$filename' is {$header['width']} x {$header['height']} px" );
121
122 return $header;
123 }
124
125 public function getSizeAndMetadata( $state, $filename ) {
126 $header = self::getXCFMetaData( $filename );
127 $metadata = [];
128 if ( $header ) {
129 // Try to be consistent with the names used by PNG files.
130 // Unclear from base media type if it has an alpha layer,
131 // so just assume that it does since it "potentially" could.
132 switch ( $header['base_type'] ) {
133 case 0:
134 $metadata['colorType'] = 'truecolour-alpha';
135 break;
136 case 1:
137 $metadata['colorType'] = 'greyscale-alpha';
138 break;
139 case 2:
140 $metadata['colorType'] = 'index-coloured';
141 break;
142 default:
143 $metadata['colorType'] = 'unknown';
144 }
145 } else {
146 // Marker to prevent repeated attempted extraction
147 $metadata['error'] = true;
148 }
149 return [
150 'width' => $header['width'] ?? 0,
151 'height' => $header['height'] ?? 0,
152 'bits' => 8,
153 'metadata' => $metadata
154 ];
155 }
156
163 public function isFileMetadataValid( $file ) {
164 if ( !$file->getMetadataArray() ) {
165 // Old metadata when we just put an empty string in there
166 return self::METADATA_BAD;
167 }
168
169 return self::METADATA_GOOD;
170 }
171
172 protected function hasGDSupport() {
173 return false;
174 }
175
184 public function canRender( $file ) {
185 $xcfMeta = $file->getMetadataArray();
186 if ( isset( $xcfMeta['colorType'] ) && $xcfMeta['colorType'] === 'index-coloured' ) {
187 return false;
188 }
189 return parent::canRender( $file );
190 }
191}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Generic handler for bitmap images.
const METADATA_GOOD
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:93
A collection of static methods to play with strings.
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.