d3fa36dddc26dd3232f8c4d52f946e13c3fce36b
[lhc/web/wiklou.git] / includes / media / ExifBitmap.php
1 <?php
2 /**
3 * Handler for bitmap images with exif metadata.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Media
22 */
23
24 /**
25 * Stuff specific to JPEG and (built-in) TIFF handler.
26 * All metadata related, since both JPEG and TIFF support Exif.
27 *
28 * @ingroup Media
29 */
30 class ExifBitmapHandler extends BitmapHandler {
31
32 const BROKEN_FILE = '-1'; // error extracting metadata
33 const OLD_BROKEN_FILE = '0'; // outdated error extracting metadata.
34
35 function convertMetadataVersion( $metadata, $version = 1 ) {
36 // basically flattens arrays.
37 $version = explode( ';', $version, 2 );
38 $version = intval( $version[0] );
39 if ( $version < 1 || $version >= 2 ) {
40 return $metadata;
41 }
42
43 $avoidHtml = true;
44
45 if ( !is_array( $metadata ) ) {
46 $metadata = unserialize( $metadata );
47 }
48 if ( !isset( $metadata['MEDIAWIKI_EXIF_VERSION'] ) || $metadata['MEDIAWIKI_EXIF_VERSION'] != 2 ) {
49 return $metadata;
50 }
51
52 // Treat Software as a special case because in can contain
53 // an array of (SoftwareName, Version).
54 if ( isset( $metadata['Software'] )
55 && is_array( $metadata['Software'] )
56 && is_array( $metadata['Software'][0] )
57 && isset( $metadata['Software'][0][0] )
58 && isset( $metadata['Software'][0][1] )
59 ) {
60 $metadata['Software'] = $metadata['Software'][0][0] . ' (Version '
61 . $metadata['Software'][0][1] . ')';
62 }
63
64 $formatter = new FormatMetadata;
65
66 // ContactInfo also has to be dealt with specially
67 if ( isset( $metadata['Contact'] ) ) {
68 $metadata['Contact'] =
69 $formatter->collapseContactInfo(
70 $metadata['Contact'] );
71 }
72
73 foreach ( $metadata as &$val ) {
74 if ( is_array( $val ) ) {
75 $val = $formatter->flattenArrayReal( $val, 'ul', $avoidHtml );
76 }
77 }
78 $metadata['MEDIAWIKI_EXIF_VERSION'] = 1;
79 return $metadata;
80 }
81
82 function isMetadataValid( $image, $metadata ) {
83 global $wgShowEXIF;
84 if ( !$wgShowEXIF ) {
85 # Metadata disabled and so an empty field is expected
86 return self::METADATA_GOOD;
87 }
88 if ( $metadata === self::OLD_BROKEN_FILE ) {
89 # Old special value indicating that there is no Exif data in the file.
90 # or that there was an error well extracting the metadata.
91 wfDebug( __METHOD__ . ": back-compat version\n" );
92 return self::METADATA_COMPATIBLE;
93 }
94 if ( $metadata === self::BROKEN_FILE ) {
95 return self::METADATA_GOOD;
96 }
97 wfSuppressWarnings();
98 $exif = unserialize( $metadata );
99 wfRestoreWarnings();
100 if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) ||
101 $exif['MEDIAWIKI_EXIF_VERSION'] != Exif::version() )
102 {
103 if ( isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) &&
104 $exif['MEDIAWIKI_EXIF_VERSION'] == 1 )
105 {
106 //back-compatible but old
107 wfDebug( __METHOD__ . ": back-compat version\n" );
108 return self::METADATA_COMPATIBLE;
109 }
110 # Wrong (non-compatible) version
111 wfDebug( __METHOD__ . ": wrong version\n" );
112 return self::METADATA_BAD;
113 }
114 return self::METADATA_GOOD;
115 }
116
117 /**
118 * @param $image File
119 * @return array|bool
120 */
121 function formatMetadata( $image ) {
122 $meta = $this->getCommonMetaArray( $image );
123 if ( count( $meta ) === 0 ) {
124 return false;
125 }
126
127 return $this->formatMetadataHelper( $meta );
128 }
129
130 public function getCommonMetaArray( File $file ) {
131 $metadata = $file->getMetadata();
132 if ( $metadata === self::OLD_BROKEN_FILE ||
133 $metadata === self::BROKEN_FILE ||
134 $this->isMetadataValid( $file, $metadata ) === self::METADATA_BAD )
135 {
136 // So we don't try and display metadata from PagedTiffHandler
137 // for example when using InstantCommons.
138 return array();
139 }
140
141 $exif = unserialize( $metadata );
142 if ( !$exif ) {
143 return array();
144 }
145 unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
146
147 return $exif;
148 }
149
150 function getMetadataType( $image ) {
151 return 'exif';
152 }
153
154 /**
155 * Wrapper for base classes ImageHandler::getImageSize() that checks for
156 * rotation reported from metadata and swaps the sizes to match.
157 *
158 * @param File $image
159 * @param string $path
160 * @return array
161 */
162 function getImageSize( $image, $path ) {
163 global $wgEnableAutoRotation;
164 $gis = parent::getImageSize( $image, $path );
165
166 // Don't just call $image->getMetadata(); FSFile::getPropsFromPath() calls us with a bogus object.
167 // This may mean we read EXIF data twice on initial upload.
168 if ( $wgEnableAutoRotation ) {
169 $meta = $this->getMetadata( $image, $path );
170 $rotation = $this->getRotationForExif( $meta );
171 } else {
172 $rotation = 0;
173 }
174
175 if ( $rotation == 90 || $rotation == 270 ) {
176 $width = $gis[0];
177 $gis[0] = $gis[1];
178 $gis[1] = $width;
179 }
180 return $gis;
181 }
182
183 /**
184 * On supporting image formats, try to read out the low-level orientation
185 * of the file and return the angle that the file needs to be rotated to
186 * be viewed.
187 *
188 * This information is only useful when manipulating the original file;
189 * the width and height we normally work with is logical, and will match
190 * any produced output views.
191 *
192 * @param $file File
193 * @return int 0, 90, 180 or 270
194 */
195 public function getRotation( $file ) {
196 global $wgEnableAutoRotation;
197 if ( !$wgEnableAutoRotation ) {
198 return 0;
199 }
200
201 $data = $file->getMetadata();
202 return $this->getRotationForExif( $data );
203 }
204
205 /**
206 * Given a chunk of serialized Exif metadata, return the orientation as
207 * degrees of rotation.
208 *
209 * @param string $data
210 * @return int 0, 90, 180 or 270
211 * @todo FIXME orientation can include flipping as well; see if this is an
212 * issue!
213 */
214 protected function getRotationForExif( $data ) {
215 if ( !$data ) {
216 return 0;
217 }
218 wfSuppressWarnings();
219 $data = unserialize( $data );
220 wfRestoreWarnings();
221 if ( isset( $data['Orientation'] ) ) {
222 # See http://sylvana.net/jpegcrop/exif_orientation.html
223 switch ( $data['Orientation'] ) {
224 case 8:
225 return 90;
226 case 3:
227 return 180;
228 case 6:
229 return 270;
230 default:
231 return 0;
232 }
233 }
234 return 0;
235 }
236 }