MediaWiki  master
XCFHandler.php
Go to the documentation of this file.
1 <?php
35 class XCFHandler extends BitmapHandler {
40  public function mustRender( $file ) {
41  return true;
42  }
43 
52  public function getThumbType( $ext, $mime, $params = null ) {
53  return [ 'png', 'image/png' ];
54  }
55 
66  private static function getXCFMetaData( $filename ) {
67  # Decode master structure
68  $f = fopen( $filename, 'rb' );
69  if ( !$f ) {
70  return null;
71  }
72  # The image structure always starts at offset 0 in the XCF file.
73  # So we just read it :-)
74  $binaryHeader = fread( $f, 26 );
75  fclose( $f );
76 
94  try {
95  $header = wfUnpack(
96  "A9magic" . # A: space padded
97  "/a5version" . # a: zero padded
98  "/Nwidth" . # \
99  "/Nheight" . # N: unsigned long 32bit big endian
100  "/Nbase_type", # /
101  $binaryHeader
102  );
103  } catch ( MWException $mwe ) {
104  return null;
105  }
106 
107  # Check values
108  if ( $header['magic'] !== 'gimp xcf' ) {
109  wfDebug( __METHOD__ . " '$filename' has invalid magic signature." );
110 
111  return null;
112  }
113  # TODO: we might want to check for correct values of width and height
114 
115  wfDebug( __METHOD__ .
116  ": canvas size of '$filename' is {$header['width']} x {$header['height']} px" );
117 
118  return $header;
119  }
120 
121  public function getSizeAndMetadata( $state, $filename ) {
122  $header = self::getXCFMetaData( $filename );
123  $metadata = [];
124  if ( $header ) {
125  // Try to be consistent with the names used by PNG files.
126  // Unclear from base media type if it has an alpha layer,
127  // so just assume that it does since it "potentially" could.
128  switch ( $header['base_type'] ) {
129  case 0:
130  $metadata['colorType'] = 'truecolour-alpha';
131  break;
132  case 1:
133  $metadata['colorType'] = 'greyscale-alpha';
134  break;
135  case 2:
136  $metadata['colorType'] = 'index-coloured';
137  break;
138  default:
139  $metadata['colorType'] = 'unknown';
140  }
141  } else {
142  // Marker to prevent repeated attempted extraction
143  $metadata['error'] = true;
144  }
145  return [
146  'width' => $header['width'] ?? 0,
147  'height' => $header['height'] ?? 0,
148  'bits' => 8,
149  'metadata' => $metadata
150  ];
151  }
152 
159  public function isFileMetadataValid( $file ) {
160  if ( !$file->getMetadataArray() ) {
161  // Old metadata when we just put an empty string in there
162  return self::METADATA_BAD;
163  }
164 
165  return self::METADATA_GOOD;
166  }
167 
175  protected function getScalerType( $dstPath, $checkDstPath = true ) {
176  return "im";
177  }
178 
187  public function canRender( $file ) {
188  $xcfMeta = $file->getMetadataArray();
189  if ( isset( $xcfMeta['colorType'] ) && $xcfMeta['colorType'] === 'index-coloured' ) {
190  return false;
191  }
192  return parent::canRender( $file );
193  }
194 }
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfUnpack( $format, $data, $length=false)
Wrapper around php's unpack.
Generic handler for bitmap images.
MediaWiki exception.
Definition: MWException.php:33
const METADATA_BAD
const METADATA_GOOD
Handler for the Gimp's native file format; getimagesize() doesn't support these files.
Definition: XCFHandler.php:35
canRender( $file)
Can we render this file?
Definition: XCFHandler.php:187
getThumbType( $ext, $mime, $params=null)
Render files as PNG.
Definition: XCFHandler.php:52
isFileMetadataValid( $file)
Should we refresh the metadata.
Definition: XCFHandler.php:159
mustRender( $file)
Definition: XCFHandler.php:40
getScalerType( $dstPath, $checkDstPath=true)
Must use "im" for XCF.
Definition: XCFHandler.php:175
getSizeAndMetadata( $state, $filename)
Get image size information and metadata array.
Definition: XCFHandler.php:121
$mime
Definition: router.php:60
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
if(!is_readable( $file)) $ext
Definition: router.php:48
$header