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