Merge "Remove not used private member variable mParserWarnings from OutputPage"
[mediawiki.git] / includes / media / ExifBitmap.php
blob3537cc665fe3e6909af5327f8f389840cab98685
1 <?php
2 /**
3 * Handler for bitmap images with exif metadata.
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.
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.
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
20 * @file
21 * @ingroup Media
24 /**
25 * Stuff specific to JPEG and (built-in) TIFF handler.
26 * All metadata related, since both JPEG and TIFF support Exif.
28 * @ingroup Media
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';
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;
43 $avoidHtml = true;
45 if ( !is_array( $metadata ) ) {
46 $metadata = unserialize( $metadata );
48 if ( !isset( $metadata['MEDIAWIKI_EXIF_VERSION'] ) || $metadata['MEDIAWIKI_EXIF_VERSION'] != 2 ) {
49 return $metadata;
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] . ')';
64 $formatter = new FormatMetadata;
66 // ContactInfo also has to be dealt with specially
67 if ( isset( $metadata['Contact'] ) ) {
68 $metadata['Contact'] =
69 $formatter->collapseContactInfo(
70 $metadata['Contact'] );
73 foreach ( $metadata as &$val ) {
74 if ( is_array( $val ) ) {
75 $val = $formatter->flattenArrayReal( $val, 'ul', $avoidHtml );
78 $metadata['MEDIAWIKI_EXIF_VERSION'] = 1;
80 return $metadata;
83 /**
84 * @param File $image
85 * @param array $metadata
86 * @return bool|int
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;
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" );
99 return self::METADATA_COMPATIBLE;
101 if ( $metadata === self::BROKEN_FILE ) {
102 return self::METADATA_GOOD;
104 MediaWiki\suppressWarnings();
105 $exif = unserialize( $metadata );
106 MediaWiki\restoreWarnings();
107 if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
108 || $exif['MEDIAWIKI_EXIF_VERSION'] != Exif::version()
110 if ( isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
111 && $exif['MEDIAWIKI_EXIF_VERSION'] == 1
113 // back-compatible but old
114 wfDebug( __METHOD__ . ": back-compat version\n" );
116 return self::METADATA_COMPATIBLE;
118 # Wrong (non-compatible) version
119 wfDebug( __METHOD__ . ": wrong version\n" );
121 return self::METADATA_BAD;
124 return self::METADATA_GOOD;
128 * @param File $image
129 * @param bool|IContextSource $context Context to use (optional)
130 * @return array|bool
132 function formatMetadata( $image, $context = false ) {
133 $meta = $this->getCommonMetaArray( $image );
134 if ( count( $meta ) === 0 ) {
135 return false;
138 return $this->formatMetadataHelper( $meta, $context );
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
147 // So we don't try and display metadata from PagedTiffHandler
148 // for example when using InstantCommons.
149 return array();
152 $exif = unserialize( $metadata );
153 if ( !$exif ) {
154 return array();
156 unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
158 return $exif;
161 function getMetadataType( $image ) {
162 return 'exif';
166 * Wrapper for base classes ImageHandler::getImageSize() that checks for
167 * rotation reported from metadata and swaps the sizes to match.
169 * @param File $image
170 * @param string $path
171 * @return array
173 function getImageSize( $image, $path ) {
174 $gis = parent::getImageSize( $image, $path );
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;
185 if ( $rotation == 90 || $rotation == 270 ) {
186 $width = $gis[0];
187 $gis[0] = $gis[1];
188 $gis[1] = $width;
191 return $gis;
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.
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.
203 * @param File $file
204 * @return int 0, 90, 180 or 270
206 public function getRotation( $file ) {
207 if ( !$this->autoRotateEnabled() ) {
208 return 0;
211 $data = $file->getMetadata();
213 return $this->getRotationForExif( $data );
217 * Given a chunk of serialized Exif metadata, return the orientation as
218 * degrees of rotation.
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!
224 protected function getRotationForExif( $data ) {
225 if ( !$data ) {
226 return 0;
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;
245 return 0;
248 protected function transformImageMagick( $image, $params ) {
249 global $wgUseTinyRGBForJPGThumbnails;
251 $ret = parent::transformImageMagick( $image, $params );
253 if ( $ret ) {
254 return $ret;
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
261 $this->swapICCProfile(
262 $params['dstPath'],
263 self::SRGB_ICC_PROFILE_NAME,
264 realpath( __DIR__ ) . '/tinyrgb.icc'
268 return false;
272 * Swaps an embedded ICC profile for another, if found.
273 * Depends on exiftool, no-op if not installed.
274 * @param string $filepath File to be manipulated (will be overwritten)
275 * @param string $oldProfileString Exact name of color profile to look for
276 * (the one that will be replaced)
277 * @param string $profileFilepath ICC profile file to apply to the file
278 * @since 1.26
279 * @return bool
281 public function swapICCProfile( $filepath, $oldProfileString, $profileFilepath ) {
282 global $wgExiftool;
284 if ( !$wgExiftool || !is_executable( $wgExiftool ) ) {
285 return false;
288 $cmd = wfEscapeShellArg( $wgExiftool,
289 '-DeviceModelDesc',
290 '-S',
291 '-T',
292 $filepath
295 $output = wfShellExecWithStderr( $cmd, $retval );
297 if ( $retval !== 0 || strcasecmp( trim( $output ), $oldProfileString ) !== 0 ) {
298 // We can't establish that this file has the expected ICC profile, don't process it
299 return false;
302 $cmd = wfEscapeShellArg( $wgExiftool,
303 '-overwrite_original',
304 '-icc_profile<=' . $profileFilepath,
305 $filepath
308 $output = wfShellExecWithStderr( $cmd, $retval );
310 if ( $retval !== 0 ) {
311 $this->logErrorForExternalProcess( $retval, $output, $cmd );
313 return false;
316 return true;