Merge "Don't check namespace in SpecialWantedtemplates"
[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 const BROKEN_FILE = '-1'; // error extracting metadata
32 const OLD_BROKEN_FILE = '0'; // outdated error extracting metadata.
33 const SRGB_ICC_PROFILE_NAME = 'IEC 61966-2.1 Default RGB colour space - sRGB';
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
80 return $metadata;
81 }
82
83 /**
84 * @param File $image
85 * @param array $metadata
86 * @return bool|int
87 */
88 function isMetadataValid( $image, $metadata ) {
89 global $wgShowEXIF;
90 if ( !$wgShowEXIF ) {
91 # Metadata disabled and so an empty field is expected
92 return self::METADATA_GOOD;
93 }
94 if ( $metadata === self::OLD_BROKEN_FILE ) {
95 # Old special value indicating that there is no Exif data in the file.
96 # or that there was an error well extracting the metadata.
97 wfDebug( __METHOD__ . ": back-compat version\n" );
98
99 return self::METADATA_COMPATIBLE;
100 }
101 if ( $metadata === self::BROKEN_FILE ) {
102 return self::METADATA_GOOD;
103 }
104 MediaWiki\suppressWarnings();
105 $exif = unserialize( $metadata );
106 MediaWiki\restoreWarnings();
107 if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
108 || $exif['MEDIAWIKI_EXIF_VERSION'] != Exif::version()
109 ) {
110 if ( isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
111 && $exif['MEDIAWIKI_EXIF_VERSION'] == 1
112 ) {
113 //back-compatible but old
114 wfDebug( __METHOD__ . ": back-compat version\n" );
115
116 return self::METADATA_COMPATIBLE;
117 }
118 # Wrong (non-compatible) version
119 wfDebug( __METHOD__ . ": wrong version\n" );
120
121 return self::METADATA_BAD;
122 }
123
124 return self::METADATA_GOOD;
125 }
126
127 /**
128 * @param File $image
129 * @param bool|IContextSource $context Context to use (optional)
130 * @return array|bool
131 */
132 function formatMetadata( $image, $context = false ) {
133 $meta = $this->getCommonMetaArray( $image );
134 if ( count( $meta ) === 0 ) {
135 return false;
136 }
137
138 return $this->formatMetadataHelper( $meta, $context );
139 }
140
141 public function getCommonMetaArray( File $file ) {
142 $metadata = $file->getMetadata();
143 if ( $metadata === self::OLD_BROKEN_FILE
144 || $metadata === self::BROKEN_FILE
145 || $this->isMetadataValid( $file, $metadata ) === self::METADATA_BAD
146 ) {
147 // So we don't try and display metadata from PagedTiffHandler
148 // for example when using InstantCommons.
149 return array();
150 }
151
152 $exif = unserialize( $metadata );
153 if ( !$exif ) {
154 return array();
155 }
156 unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
157
158 return $exif;
159 }
160
161 function getMetadataType( $image ) {
162 return 'exif';
163 }
164
165 /**
166 * Wrapper for base classes ImageHandler::getImageSize() that checks for
167 * rotation reported from metadata and swaps the sizes to match.
168 *
169 * @param File $image
170 * @param string $path
171 * @return array
172 */
173 function getImageSize( $image, $path ) {
174 $gis = parent::getImageSize( $image, $path );
175
176 // Don't just call $image->getMetadata(); FSFile::getPropsFromPath() calls us with a bogus object.
177 // This may mean we read EXIF data twice on initial upload.
178 if ( $this->autoRotateEnabled() ) {
179 $meta = $this->getMetadata( $image, $path );
180 $rotation = $this->getRotationForExif( $meta );
181 } else {
182 $rotation = 0;
183 }
184
185 if ( $rotation == 90 || $rotation == 270 ) {
186 $width = $gis[0];
187 $gis[0] = $gis[1];
188 $gis[1] = $width;
189 }
190
191 return $gis;
192 }
193
194 /**
195 * On supporting image formats, try to read out the low-level orientation
196 * of the file and return the angle that the file needs to be rotated to
197 * be viewed.
198 *
199 * This information is only useful when manipulating the original file;
200 * the width and height we normally work with is logical, and will match
201 * any produced output views.
202 *
203 * @param File $file
204 * @return int 0, 90, 180 or 270
205 */
206 public function getRotation( $file ) {
207 if ( !$this->autoRotateEnabled() ) {
208 return 0;
209 }
210
211 $data = $file->getMetadata();
212
213 return $this->getRotationForExif( $data );
214 }
215
216 /**
217 * Given a chunk of serialized Exif metadata, return the orientation as
218 * degrees of rotation.
219 *
220 * @param string $data
221 * @return int 0, 90, 180 or 270
222 * @todo FIXME: Orientation can include flipping as well; see if this is an issue!
223 */
224 protected function getRotationForExif( $data ) {
225 if ( !$data ) {
226 return 0;
227 }
228 MediaWiki\suppressWarnings();
229 $data = unserialize( $data );
230 MediaWiki\restoreWarnings();
231 if ( isset( $data['Orientation'] ) ) {
232 # See http://sylvana.net/jpegcrop/exif_orientation.html
233 switch ( $data['Orientation'] ) {
234 case 8:
235 return 90;
236 case 3:
237 return 180;
238 case 6:
239 return 270;
240 default:
241 return 0;
242 }
243 }
244
245 return 0;
246 }
247
248 protected function transformImageMagick( $image, $params ) {
249 global $wgUseTinyRGBForJPGThumbnails;
250
251 $ret = parent::transformImageMagick( $image, $params );
252
253 if ( $ret ) {
254 return $ret;
255 }
256
257 if ( $params['mimeType'] === 'image/jpeg' && $wgUseTinyRGBForJPGThumbnails ) {
258 // T100976 If the profile embedded in the JPG is sRGB, swap it for the smaller
259 // (and free) TinyRGB
260
261 $this->swapICCProfile(
262 $params['dstPath'],
263 self::SRGB_ICC_PROFILE_NAME,
264 realpath( __DIR__ ) . '/tinyrgb.icc'
265 );
266 }
267
268 return false;
269 }
270
271 /**
272 * Swaps an embedded ICC profile for another, if found. Depends on exiftool, no-op if not installed.
273 * @param string $filepath File to be manipulated (will be overwritten)
274 * @param string $oldProfileString Exact name of color profile to look for (the one that will be replaced)
275 * @param string $profileFilepath ICC profile file to apply to the file
276 * @since 1.26
277 * @return bool
278 */
279 public function swapICCProfile( $filepath, $oldProfileString, $profileFilepath ) {
280 global $wgExiftool;
281
282 if ( !$wgExiftool || !is_executable( $wgExiftool ) ) {
283 return false;
284 }
285
286 $cmd = wfEscapeShellArg( $wgExiftool,
287 '-DeviceModelDesc',
288 '-S',
289 '-T',
290 $filepath
291 );
292
293 $output = wfShellExecWithStderr( $cmd, $retval );
294
295 if ( $retval !== 0 || strcasecmp( trim( $output ), $oldProfileString ) !== 0 ) {
296 // We can't establish that this file has the expected ICC profile, don't process it
297 return false;
298 }
299
300 $cmd = wfEscapeShellArg( $wgExiftool,
301 '-overwrite_original',
302 '-icc_profile<=' . $profileFilepath,
303 $filepath
304 );
305
306 $output = wfShellExecWithStderr( $cmd, $retval );
307
308 if ( $retval !== 0 ) {
309 $this->logErrorForExternalProcess( $retval, $output, $cmd );
310
311 return false;
312 }
313
314 return true;
315 }
316 }