3 * Handler for the Gimp's native file format (XCF)
6 * http://en.wikipedia.org/wiki/XCF_(file_format)
7 * Specification in Gnome repository:
8 * http://svn.gnome.org/viewvc/gimp/trunk/devel-docs/xcf.txt?view=markup
15 * Handler for the Gimp's native file format; getimagesize() doesn't
20 class XCFHandler
extends BitmapHandler
{
26 function mustRender( $file ) {
38 function getThumbType( $ext, $mime, $params = null ) {
39 return array( 'png', 'image/png' );
43 * Get width and height from the XCF header.
49 function getImageSize( $image, $filename ) {
50 return self
::getXCFMetaData( $filename );
54 * Metadata for a given XCF file
56 * Will return false if file magic signature is not recognized
60 * @param $filename String Full path to a XCF file
61 * @return bool|array metadata array just like PHP getimagesize()
63 static function getXCFMetaData( $filename ) {
64 # Decode master structure
65 $f = fopen( $filename, 'rb' );
69 # The image structure always starts at offset 0 in the XCF file.
70 # So we just read it :-)
71 $binaryHeader = fread( $f, 26 );
74 # Master image structure:
76 # byte[9] "gimp xcf " File type magic
77 # byte[4] version XCF version
81 # byte 0 Zero-terminator for version tag
82 # uint32 width With of canvas
83 # uint32 height Height of canvas
84 # uint32 base_type Color mode of the image; one of
88 # (enum GimpImageBaseType in libgimpbase/gimpbaseenums.h)
91 "A9magic" # A: space padded
92 . "/a5version" # a: zero padded
94 . "/Nheight" # N: unsigned long 32bit big endian
98 } catch( MWException
$mwe ) {
103 if( $header['magic'] !== 'gimp xcf' ) {
104 wfDebug( __METHOD__
. " '$filename' has invalid magic signature.\n" );
107 # TODO: we might want to check for sane values of width and height
109 wfDebug( __METHOD__
. ": canvas size of '$filename' is {$header['width']} x {$header['height']} px\n" );
111 # Forge a return array containing metadata information just like getimagesize()
112 # See PHP documentation at: http://www.php.net/getimagesize
114 $metadata[0] = $header['width'];
115 $metadata[1] = $header['height'];
116 $metadata[2] = null; # IMAGETYPE constant, none exist for XCF.
117 $metadata[3] = sprintf(
118 'height="%s" width="%s"', $header['height'], $header['width']
120 $metadata['mime'] = 'image/x-xcf';
121 $metadata['channels'] = null;
122 $metadata['bits'] = 8; # Always 8-bits per color
124 assert( '7 == count($metadata); # return array must contains 7 elements just like getimagesize() return' );
130 * Must use "im" for XCF
134 protected static function getScalerType( $dstPath, $checkDstPath = true ) {