Merge "Add mediawiki.org to default $wgNoFollowDomainExceptions"
[lhc/web/wiklou.git] / includes / media / XCF.php
1 <?php
2 /**
3 * Handler for the Gimp's native file format (XCF)
4 *
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
9 *
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.
14 *
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.
19 *
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
24 *
25 * @file
26 * @ingroup Media
27 */
28
29 /**
30 * Handler for the Gimp's native file format; getimagesize() doesn't
31 * support these files
32 *
33 * @ingroup Media
34 */
35 class XCFHandler extends BitmapHandler {
36
37 /**
38 * @param $file
39 * @return bool
40 */
41 function mustRender( $file ) {
42 return true;
43 }
44
45 /**
46 * Render files as PNG
47 *
48 * @param $ext
49 * @param $mime
50 * @param $params
51 * @return array
52 */
53 function getThumbType( $ext, $mime, $params = null ) {
54 return array( 'png', 'image/png' );
55 }
56
57 /**
58 * Get width and height from the XCF header.
59 *
60 * @param $image
61 * @param $filename
62 * @return array
63 */
64 function getImageSize( $image, $filename ) {
65 return self::getXCFMetaData( $filename );
66 }
67
68 /**
69 * Metadata for a given XCF file
70 *
71 * Will return false if file magic signature is not recognized
72 * @author Hexmode
73 * @author Hashar
74 *
75 * @param string $filename Full path to a XCF file
76 * @return bool|array metadata array just like PHP getimagesize()
77 */
78 static function getXCFMetaData( $filename ) {
79 # Decode master structure
80 $f = fopen( $filename, 'rb' );
81 if ( !$f ) {
82 return false;
83 }
84 # The image structure always starts at offset 0 in the XCF file.
85 # So we just read it :-)
86 $binaryHeader = fread( $f, 26 );
87 fclose( $f );
88
89 # Master image structure:
90 #
91 # byte[9] "gimp xcf " File type magic
92 # byte[4] version XCF version
93 # "file" - version 0
94 # "v001" - version 1
95 # "v002" - version 2
96 # byte 0 Zero-terminator for version tag
97 # uint32 width With of canvas
98 # uint32 height Height of canvas
99 # uint32 base_type Color mode of the image; one of
100 # 0: RGB color
101 # 1: Grayscale
102 # 2: Indexed color
103 # (enum GimpImageBaseType in libgimpbase/gimpbaseenums.h)
104 try {
105 $header = wfUnpack(
106 "A9magic" . # A: space padded
107 "/a5version" . # a: zero padded
108 "/Nwidth" . # \
109 "/Nheight" . # N: unsigned long 32bit big endian
110 "/Nbase_type", # /
111 $binaryHeader
112 );
113 } catch ( MWException $mwe ) {
114 return false;
115 }
116
117 # Check values
118 if ( $header['magic'] !== 'gimp xcf' ) {
119 wfDebug( __METHOD__ . " '$filename' has invalid magic signature.\n" );
120 return false;
121 }
122 # TODO: we might want to check for sane values of width and height
123
124 wfDebug( __METHOD__ . ": canvas size of '$filename' is {$header['width']} x {$header['height']} px\n" );
125
126 # Forge a return array containing metadata information just like getimagesize()
127 # See PHP documentation at: http://www.php.net/getimagesize
128 $metadata = array();
129 $metadata[0] = $header['width'];
130 $metadata[1] = $header['height'];
131 $metadata[2] = null; # IMAGETYPE constant, none exist for XCF.
132 $metadata[3] = sprintf(
133 'height="%s" width="%s"', $header['height'], $header['width']
134 );
135 $metadata['mime'] = 'image/x-xcf';
136 $metadata['channels'] = null;
137 $metadata['bits'] = 8; # Always 8-bits per color
138
139 assert( '7 == count($metadata); # return array must contains 7 elements just like getimagesize() return' );
140
141 return $metadata;
142 }
143
144 /**
145 * Must use "im" for XCF
146 *
147 * @return string
148 */
149 protected static function getScalerType( $dstPath, $checkDstPath = true ) {
150 return "im";
151 }
152 }