Remove silly 'm' prefix from StubObject/DeprecatedGlobal members
[mediawiki.git] / includes / media / XCF.php
blob41e6f036c03ca14e6f7fe2c358b433a508b1c2ed
1 <?php
2 /**
3 * Handler for the Gimp's native file format (XCF)
5 * Overview:
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
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
25 * @file
26 * @ingroup Media
29 /**
30 * Handler for the Gimp's native file format; getimagesize() doesn't
31 * support these files
33 * @ingroup Media
35 class XCFHandler extends BitmapHandler {
36 /**
37 * @param File $file
38 * @return bool
40 function mustRender( $file ) {
41 return true;
44 /**
45 * Render files as PNG
47 * @param string $ext
48 * @param string $mime
49 * @param array $params
50 * @return array
52 function getThumbType( $ext, $mime, $params = null ) {
53 return array( 'png', 'image/png' );
56 /**
57 * Get width and height from the XCF header.
59 * @param File $image
60 * @param string $filename
61 * @return array
63 function getImageSize( $image, $filename ) {
64 return self::getXCFMetaData( $filename );
67 /**
68 * Metadata for a given XCF file
70 * Will return false if file magic signature is not recognized
71 * @author Hexmode
72 * @author Hashar
74 * @param string $filename Full path to a XCF file
75 * @return bool|array metadata array just like PHP getimagesize()
77 static function getXCFMetaData( $filename ) {
78 # Decode master structure
79 $f = fopen( $filename, 'rb' );
80 if ( !$f ) {
81 return false;
83 # The image structure always starts at offset 0 in the XCF file.
84 # So we just read it :-)
85 $binaryHeader = fread( $f, 26 );
86 fclose( $f );
88 # Master image structure:
90 # byte[9] "gimp xcf " File type magic
91 # byte[4] version XCF version
92 # "file" - version 0
93 # "v001" - version 1
94 # "v002" - version 2
95 # byte 0 Zero-terminator for version tag
96 # uint32 width With of canvas
97 # uint32 height Height of canvas
98 # uint32 base_type Color mode of the image; one of
99 # 0: RGB color
100 # 1: Grayscale
101 # 2: Indexed color
102 # (enum GimpImageBaseType in libgimpbase/gimpbaseenums.h)
103 try {
104 $header = wfUnpack(
105 "A9magic" . # A: space padded
106 "/a5version" . # a: zero padded
107 "/Nwidth" . # \
108 "/Nheight" . # N: unsigned long 32bit big endian
109 "/Nbase_type", # /
110 $binaryHeader
112 } catch ( MWException $mwe ) {
113 return false;
116 # Check values
117 if ( $header['magic'] !== 'gimp xcf' ) {
118 wfDebug( __METHOD__ . " '$filename' has invalid magic signature.\n" );
120 return false;
122 # TODO: we might want to check for sane values of width and height
124 wfDebug( __METHOD__ .
125 ": canvas size of '$filename' is {$header['width']} x {$header['height']} px\n" );
127 # Forge a return array containing metadata information just like getimagesize()
128 # See PHP documentation at: http://www.php.net/getimagesize
129 $metadata = array();
130 $metadata[0] = $header['width'];
131 $metadata[1] = $header['height'];
132 $metadata[2] = null; # IMAGETYPE constant, none exist for XCF.
133 $metadata[3] = sprintf(
134 'height="%s" width="%s"', $header['height'], $header['width']
136 $metadata['mime'] = 'image/x-xcf';
137 $metadata['channels'] = null;
138 $metadata['bits'] = 8; # Always 8-bits per color
140 assert( '7 == count($metadata); ' .
141 '# return array must contains 7 elements just like getimagesize() return' );
143 return $metadata;
147 * Must use "im" for XCF
149 * @param string $dstPath
150 * @param bool $checkDstPath
151 * @return string
153 protected static function getScalerType( $dstPath, $checkDstPath = true ) {
154 return "im";