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 * @stable to extend 29 * @ingroup Media 30 */ 31class ExifBitmapHandler extends BitmapHandler { 32 /** Error extracting metadata */ 33 public const BROKEN_FILE = '-1'; 34 35 /** Outdated error extracting metadata */ 36 public const OLD_BROKEN_FILE = '0'; 37 38 public function convertMetadataVersion( $metadata, $version = 1 ) { 39 // basically flattens arrays. 40 $version = intval( explode( ';', $version, 2 )[0] ); 41 if ( $version < 1 || $version >= 2 ) { 42 return $metadata; 43 } 44 45 $avoidHtml = true; 46 47 if ( !is_array( $metadata ) ) { 48 $metadata = unserialize( $metadata ); 49 } 50 if ( !isset( $metadata['MEDIAWIKI_EXIF_VERSION'] ) || $metadata['MEDIAWIKI_EXIF_VERSION'] != 2 ) { 51 return $metadata; 52 } 53 54 // Treat Software as a special case because in can contain 55 // an array of (SoftwareName, Version). 56 if ( isset( $metadata['Software'] ) 57 && is_array( $metadata['Software'] ) 58 && is_array( $metadata['Software'][0] ) 59 && isset( $metadata['Software'][0][0] ) 60 && isset( $metadata['Software'][0][1] ) 61 ) { 62 $metadata['Software'] = $metadata['Software'][0][0] . ' (Version ' 63 . $metadata['Software'][0][1] . ')'; 64 } 65 66 $formatter = new FormatMetadata; 67 68 // ContactInfo also has to be dealt with specially 69 if ( isset( $metadata['Contact'] ) ) { 70 $metadata['Contact'] = 71 $formatter->collapseContactInfo( 72 $metadata['Contact'] ); 73 } 74 75 foreach ( $metadata as &$val ) { 76 if ( is_array( $val ) ) { 77 $val = $formatter->flattenArrayReal( $val, 'ul', $avoidHtml ); 78 } 79 } 80 $metadata['MEDIAWIKI_EXIF_VERSION'] = 1; 81 82 return $metadata; 83 } 84 85 /** 86 * @param File $image 87 * @param string $metadata 88 * @return bool|int 89 */ 90 public function isMetadataValid( $image, $metadata ) { 91 global $wgShowEXIF; 92 if ( !$wgShowEXIF ) { 93 # Metadata disabled and so an empty field is expected 94 return self::METADATA_GOOD; 95 } 96 if ( $metadata === self::OLD_BROKEN_FILE ) { 97 # Old special value indicating that there is no Exif data in the file. 98 # or that there was an error well extracting the metadata. 99 wfDebug( __METHOD__ . ": back-compat version" ); 100 101 return self::METADATA_COMPATIBLE; 102 } 103 if ( $metadata === self::BROKEN_FILE ) { 104 return self::METADATA_GOOD; 105 } 106 Wikimedia\suppressWarnings(); 107 $exif = unserialize( $metadata ); 108 Wikimedia\restoreWarnings(); 109 if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) 110 || $exif['MEDIAWIKI_EXIF_VERSION'] != Exif::version() 111 ) { 112 if ( isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) 113 && $exif['MEDIAWIKI_EXIF_VERSION'] == 1 114 ) { 115 // back-compatible but old 116 wfDebug( __METHOD__ . ": back-compat version" ); 117 118 return self::METADATA_COMPATIBLE; 119 } 120 # Wrong (non-compatible) version 121 wfDebug( __METHOD__ . ": wrong version" ); 122 123 return self::METADATA_BAD; 124 } 125 126 return self::METADATA_GOOD; 127 } 128 129 /** 130 * @param File $image 131 * @param bool|IContextSource $context Context to use (optional) 132 * @return array|bool 133 */ 134 public function formatMetadata( $image, $context = false ) { 135 $meta = $this->getCommonMetaArray( $image ); 136 if ( count( $meta ) === 0 ) { 137 return false; 138 } 139 140 return $this->formatMetadataHelper( $meta, $context ); 141 } 142 143 public function getCommonMetaArray( File $file ) { 144 $metadata = $file->getMetadata(); 145 if ( $metadata === self::OLD_BROKEN_FILE 146 || $metadata === self::BROKEN_FILE 147 || $this->isMetadataValid( $file, $metadata ) === self::METADATA_BAD 148 ) { 149 // So we don't try and display metadata from PagedTiffHandler 150 // for example when using InstantCommons. 151 return []; 152 } 153 154 $exif = unserialize( $metadata ); 155 if ( !$exif ) { 156 return []; 157 } 158 unset( $exif['MEDIAWIKI_EXIF_VERSION'] ); 159 160 return $exif; 161 } 162 163 public function getMetadataType( $image ) { 164 return 'exif'; 165 } 166 167 /** 168 * Wrapper for base classes ImageHandler::getImageSize() that checks for 169 * rotation reported from metadata and swaps the sizes to match. 170 * 171 * @param File|FSFile $image 172 * @param string $path 173 * @return array|false 174 */ 175 public function getImageSize( $image, $path ) { 176 $gis = parent::getImageSize( $image, $path ); 177 178 // Don't just call $image->getMetadata(); FSFile::getPropsFromPath() calls us with a bogus object. 179 // This may mean we read EXIF data twice on initial upload. 180 if ( $this->autoRotateEnabled() ) { 181 $meta = $this->getMetadata( $image, $path ); 182 $rotation = $this->getRotationForExif( $meta ); 183 } else { 184 $rotation = 0; 185 } 186 187 if ( $rotation == 90 || $rotation == 270 ) { 188 $width = $gis[0]; 189 $gis[0] = $gis[1]; 190 $gis[1] = $width; 191 } 192 193 return $gis; 194 } 195 196 /** 197 * On supporting image formats, try to read out the low-level orientation 198 * of the file and return the angle that the file needs to be rotated to 199 * be viewed. 200 * 201 * This information is only useful when manipulating the original file; 202 * the width and height we normally work with is logical, and will match 203 * any produced output views. 204 * 205 * @param File $file 206 * @return int 0, 90, 180 or 270 207 */ 208 public function getRotation( $file ) { 209 if ( !$this->autoRotateEnabled() ) { 210 return 0; 211 } 212 213 $data = $file->getMetadata(); 214 215 return $this->getRotationForExif( $data ); 216 } 217 218 /** 219 * Given a chunk of serialized Exif metadata, return the orientation as 220 * degrees of rotation. 221 * 222 * @param string $data 223 * @return int 0, 90, 180 or 270 224 * @todo FIXME: Orientation can include flipping as well; see if this is an issue! 225 */ 226 protected function getRotationForExif( $data ) { 227 if ( !$data ) { 228 return 0; 229 } 230 Wikimedia\suppressWarnings(); 231 $data = unserialize( $data ); 232 Wikimedia\restoreWarnings(); 233 if ( isset( $data['Orientation'] ) ) { 234 # See http://sylvana.net/jpegcrop/exif_orientation.html 235 switch ( $data['Orientation'] ) { 236 case 8: 237 return 90; 238 case 3: 239 return 180; 240 case 6: 241 return 270; 242 default: 243 return 0; 244 } 245 } 246 247 return 0; 248 } 249} 250