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
25 * Stuff specific to JPEG and (built-in) TIFF handler.
26 * All metadata related, since both JPEG and TIFF support Exif.
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 ) {
45 if ( !is_array( $metadata ) ) {
46 $metadata = unserialize( $metadata );
48 if ( !isset( $metadata['MEDIAWIKI_EXIF_VERSION'] ) ||
$metadata['MEDIAWIKI_EXIF_VERSION'] != 2 ) {
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] )
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;
85 * @param array $metadata
88 function isMetadataValid( $image, $metadata ) {
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
;
129 * @param bool|IContextSource $context Context to use (optional)
132 function formatMetadata( $image, $context = false ) {
133 $meta = $this->getCommonMetaArray( $image );
134 if ( count( $meta ) === 0 ) {
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.
152 $exif = unserialize( $metadata );
156 unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
161 function getMetadataType( $image ) {
166 * Wrapper for base classes ImageHandler::getImageSize() that checks for
167 * rotation reported from metadata and swaps the sizes to match.
170 * @param string $path
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 );
185 if ( $rotation == 90 ||
$rotation == 270 ) {
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
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.
204 * @return int 0, 90, 180 or 270
206 public function getRotation( $file ) {
207 if ( !$this->autoRotateEnabled() ) {
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 ) {
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'] ) {
248 protected function transformImageMagick( $image, $params ) {
249 global $wgUseTinyRGBForJPGThumbnails;
251 $ret = parent
::transformImageMagick( $image, $params );
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(
263 self
::SRGB_ICC_PROFILE_NAME
,
264 realpath( __DIR__
) . '/tinyrgb.icc'
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
281 public function swapICCProfile( $filepath, $oldProfileString, $profileFilepath ) {
284 if ( !$wgExiftool ||
!is_executable( $wgExiftool ) ) {
288 $cmd = wfEscapeShellArg( $wgExiftool,
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
302 $cmd = wfEscapeShellArg( $wgExiftool,
303 '-overwrite_original',
304 '-icc_profile<=' . $profileFilepath,
308 $output = wfShellExecWithStderr( $cmd, $retval );
310 if ( $retval !== 0 ) {
311 $this->logErrorForExternalProcess( $retval, $output, $cmd );