Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / media / ExifBitmap.php
blob98c423fd57164134b8b5cf696dd504a4068daf0a
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 {
32 const BROKEN_FILE = '-1'; // error extracting metadata
33 const OLD_BROKEN_FILE = '0'; // outdated error extracting metadata.
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 // ContactInfo also has to be dealt with specially
65 if ( isset( $metadata['Contact'] ) ) {
66 $metadata['Contact'] =
67 FormatMetadata::collapseContactInfo(
68 $metadata['Contact'] );
71 foreach ( $metadata as &$val ) {
72 if ( is_array( $val ) ) {
73 $val = FormatMetadata::flattenArray( $val, 'ul', $avoidHtml );
76 $metadata['MEDIAWIKI_EXIF_VERSION'] = 1;
77 return $metadata;
80 function isMetadataValid( $image, $metadata ) {
81 global $wgShowEXIF;
82 if ( !$wgShowEXIF ) {
83 # Metadata disabled and so an empty field is expected
84 return self::METADATA_GOOD;
86 if ( $metadata === self::OLD_BROKEN_FILE ) {
87 # Old special value indicating that there is no EXIF data in the file.
88 # or that there was an error well extracting the metadata.
89 wfDebug( __METHOD__ . ": back-compat version\n" );
90 return self::METADATA_COMPATIBLE;
92 if ( $metadata === self::BROKEN_FILE ) {
93 return self::METADATA_GOOD;
95 wfSuppressWarnings();
96 $exif = unserialize( $metadata );
97 wfRestoreWarnings();
98 if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) ||
99 $exif['MEDIAWIKI_EXIF_VERSION'] != Exif::version() )
101 if ( isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) &&
102 $exif['MEDIAWIKI_EXIF_VERSION'] == 1 )
104 //back-compatible but old
105 wfDebug( __METHOD__ . ": back-compat version\n" );
106 return self::METADATA_COMPATIBLE;
108 # Wrong (non-compatible) version
109 wfDebug( __METHOD__ . ": wrong version\n" );
110 return self::METADATA_BAD;
112 return self::METADATA_GOOD;
116 * @param $image File
117 * @return array|bool
119 function formatMetadata( $image ) {
120 $metadata = $image->getMetadata();
121 if ( $metadata === self::OLD_BROKEN_FILE ||
122 $metadata === self::BROKEN_FILE ||
123 $this->isMetadataValid( $image, $metadata ) === self::METADATA_BAD )
125 // So we don't try and display metadata from PagedTiffHandler
126 // for example when using InstantCommons.
127 return false;
130 $exif = unserialize( $metadata );
131 if ( !$exif ) {
132 return false;
134 unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
135 if ( count( $exif ) == 0 ) {
136 return false;
138 return $this->formatMetadataHelper( $exif );
141 function getMetadataType( $image ) {
142 return 'exif';
146 * Wrapper for base classes ImageHandler::getImageSize() that checks for
147 * rotation reported from metadata and swaps the sizes to match.
149 * @param File $image
150 * @param string $path
151 * @return array
153 function getImageSize( $image, $path ) {
154 global $wgEnableAutoRotation;
155 $gis = parent::getImageSize( $image, $path );
157 // Don't just call $image->getMetadata(); FSFile::getPropsFromPath() calls us with a bogus object.
158 // This may mean we read EXIF data twice on initial upload.
159 if ( $wgEnableAutoRotation ) {
160 $meta = $this->getMetadata( $image, $path );
161 $rotation = $this->getRotationForExif( $meta );
162 } else {
163 $rotation = 0;
166 if ( $rotation == 90 || $rotation == 270 ) {
167 $width = $gis[0];
168 $gis[0] = $gis[1];
169 $gis[1] = $width;
171 return $gis;
175 * On supporting image formats, try to read out the low-level orientation
176 * of the file and return the angle that the file needs to be rotated to
177 * be viewed.
179 * This information is only useful when manipulating the original file;
180 * the width and height we normally work with is logical, and will match
181 * any produced output views.
183 * @param $file File
184 * @return int 0, 90, 180 or 270
186 public function getRotation( $file ) {
187 global $wgEnableAutoRotation;
188 if ( !$wgEnableAutoRotation ) {
189 return 0;
192 $data = $file->getMetadata();
193 return $this->getRotationForExif( $data );
197 * Given a chunk of serialized Exif metadata, return the orientation as
198 * degrees of rotation.
200 * @param string $data
201 * @return int 0, 90, 180 or 270
202 * @todo FIXME orientation can include flipping as well; see if this is an
203 * issue!
205 protected function getRotationForExif( $data ) {
206 if ( !$data ) {
207 return 0;
209 wfSuppressWarnings();
210 $data = unserialize( $data );
211 wfRestoreWarnings();
212 if ( isset( $data['Orientation'] ) ) {
213 # See http://sylvana.net/jpegcrop/exif_orientation.html
214 switch ( $data['Orientation'] ) {
215 case 8:
216 return 90;
217 case 3:
218 return 180;
219 case 6:
220 return 270;
221 default:
222 return 0;
225 return 0;