MediaWiki  REL1_19
XCF.php
Go to the documentation of this file.
00001 <?php
00020 class XCFHandler extends BitmapHandler {
00021 
00026         function mustRender( $file ) {
00027                 return true;
00028         }
00029 
00038         function getThumbType( $ext, $mime, $params = null ) {
00039                 return array( 'png', 'image/png' );
00040         }
00041 
00049         function getImageSize( $image, $filename ) {
00050                 return self::getXCFMetaData( $filename );
00051         }
00052 
00063         static function getXCFMetaData( $filename ) {
00064                 # Decode master structure
00065                 $f = fopen( $filename, 'rb' );
00066                 if( !$f ) {
00067                         return false;
00068                 }
00069                 # The image structure always starts at offset 0 in the XCF file.
00070                 # So we just read it :-)
00071                 $binaryHeader = fread( $f, 26 );
00072                 fclose($f);
00073 
00074                 # Master image structure:
00075                 #
00076                 # byte[9] "gimp xcf "  File type magic
00077                 # byte[4] version      XCF version
00078                 #                        "file" - version 0
00079                 #                        "v001" - version 1
00080                 #                        "v002" - version 2
00081                 # byte    0            Zero-terminator for version tag
00082                 # uint32  width        With of canvas
00083                 # uint32  height       Height of canvas
00084                 # uint32  base_type    Color mode of the image; one of
00085                 #                         0: RGB color
00086                 #                         1: Grayscale
00087                 #                         2: Indexed color
00088                 #        (enum GimpImageBaseType in libgimpbase/gimpbaseenums.h)
00089                 try {
00090                         $header = wfUnpack(
00091                                   "A9magic"     # A: space padded
00092                                 . "/a5version"  # a: zero padded
00093                                 . "/Nwidth"     # \
00094                                 . "/Nheight"    # N: unsigned long 32bit big endian
00095                                 . "/Nbase_type" # /
00096                         , $binaryHeader
00097                         );
00098                 } catch( MWException $mwe ) {
00099                         return false;
00100                 }
00101 
00102                 # Check values
00103                 if( $header['magic'] !== 'gimp xcf' ) {
00104                         wfDebug( __METHOD__ . " '$filename' has invalid magic signature.\n" );
00105                         return false;
00106                 }
00107                 # TODO: we might want to check for sane values of width and height
00108 
00109                 wfDebug( __METHOD__ . ": canvas size of '$filename' is {$header['width']} x {$header['height']} px\n" );
00110 
00111                 # Forge a return array containing metadata information just like getimagesize()
00112                 # See PHP documentation at: http://www.php.net/getimagesize
00113                 $metadata = array();
00114                 $metadata[0] = $header['width'];
00115                 $metadata[1] = $header['height'];
00116                 $metadata[2] = null;   # IMAGETYPE constant, none exist for XCF.
00117                 $metadata[3] = sprintf(
00118                         'height="%s" width="%s"', $header['height'], $header['width']
00119                 );
00120                 $metadata['mime'] = 'image/x-xcf';
00121                 $metadata['channels'] = null;
00122                 $metadata['bits'] = 8;  # Always 8-bits per color
00123 
00124                 assert( '7 == count($metadata); # return array must contains 7 elements just like getimagesize() return' );
00125 
00126                 return $metadata;
00127         }
00128 
00134         protected static function getScalerType( $dstPath, $checkDstPath = true ) {
00135                 return "im";
00136         }
00137 }