Revert r101492, broken, see CR. Also revert followup r101496.
[mediawiki.git] / includes / media / ExifBitmap.php
blob96f5d21d96b295e72165a03a52c15e5a05144128
1 <?php
2 /**
3 * @file
4 * @ingroup Media
5 */
7 /**
8 * Stuff specific to JPEG and (built-in) TIFF handler.
9 * All metadata related, since both JPEG and TIFF support Exif.
11 * @ingroup Media
13 class ExifBitmapHandler extends BitmapHandler {
15 const BROKEN_FILE = '-1'; // error extracting metadata
16 const OLD_BROKEN_FILE = '0'; // outdated error extracting metadata.
18 function convertMetadataVersion( $metadata, $version = 1 ) {
19 // basically flattens arrays.
20 $version = explode(';', $version, 2);
21 $version = intval($version[0]);
22 if ( $version < 1 || $version >= 2 ) {
23 return $metadata;
26 $avoidHtml = true;
28 if ( !is_array( $metadata ) ) {
29 $metadata = unserialize( $metadata );
31 if ( !isset( $metadata['MEDIAWIKI_EXIF_VERSION'] ) || $metadata['MEDIAWIKI_EXIF_VERSION'] != 2 ) {
32 return $metadata;
35 // Treat Software as a special case because in can contain
36 // an array of (SoftwareName, Version).
37 if (isset( $metadata['Software'] )
38 && is_array( $metadata['Software'] )
39 && is_array( $metadata['Software'][0])
40 && isset( $metadata['Software'][0][0] )
41 && isset( $metadata['Software'][0][1])
42 ) {
43 $metadata['Software'] = $metadata['Software'][0][0] . ' (Version '
44 . $metadata['Software'][0][1] . ')';
47 // ContactInfo also has to be dealt with specially
48 if ( isset( $metadata['Contact'] ) ) {
49 $metadata['Contact'] =
50 FormatMetadata::collapseContactInfo(
51 $metadata['Contact'] );
54 foreach ( $metadata as &$val ) {
55 if ( is_array( $val ) ) {
56 $val = FormatMetadata::flattenArray( $val, 'ul', $avoidHtml );
59 $metadata['MEDIAWIKI_EXIF_VERSION'] = 1;
60 return $metadata;
63 function isMetadataValid( $image, $metadata ) {
64 global $wgShowEXIF;
65 if ( !$wgShowEXIF ) {
66 # Metadata disabled and so an empty field is expected
67 return self::METADATA_GOOD;
69 if ( $metadata === self::OLD_BROKEN_FILE ) {
70 # Old special value indicating that there is no EXIF data in the file.
71 # or that there was an error well extracting the metadata.
72 wfDebug( __METHOD__ . ": back-compat version\n");
73 return self::METADATA_COMPATIBLE;
75 if ( $metadata === self::BROKEN_FILE ) {
76 return self::METADATA_GOOD;
78 wfSuppressWarnings();
79 $exif = unserialize( $metadata );
80 wfRestoreWarnings();
81 if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) ||
82 $exif['MEDIAWIKI_EXIF_VERSION'] != Exif::version() )
84 if ( isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) &&
85 $exif['MEDIAWIKI_EXIF_VERSION'] == 1 )
87 //back-compatible but old
88 wfDebug( __METHOD__.": back-compat version\n" );
89 return self::METADATA_COMPATIBLE;
91 # Wrong (non-compatible) version
92 wfDebug( __METHOD__.": wrong version\n" );
93 return self::METADATA_BAD;
95 return self::METADATA_GOOD;
98 /**
99 * @param $image File
100 * @return array|bool
102 function formatMetadata( $image ) {
103 $metadata = $image->getMetadata();
104 if ( $metadata === self::OLD_BROKEN_FILE ||
105 $metadata === self::BROKEN_FILE ||
106 $this->isMetadataValid( $image, $metadata ) === self::METADATA_BAD )
108 // So we don't try and display metadata from PagedTiffHandler
109 // for example when using InstantCommons.
110 return false;
113 $exif = unserialize( $metadata );
114 if ( !$exif ) {
115 return false;
117 unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
118 if ( count( $exif ) == 0 ) {
119 return false;
121 return $this->formatMetadataHelper( $exif );
124 function getMetadataType( $image ) {
125 return 'exif';
129 * Wrapper for base classes ImageHandler::getImageSize() that checks for
130 * rotation reported from metadata and swaps the sizes to match.
132 * @param File $image
133 * @param string $path
134 * @return array
136 function getImageSize( $image, $path ) {
137 $gis = parent::getImageSize( $image, $path );
139 // Don't just call $image->getMetadata(); File::getPropsFromPath() calls us with a bogus object.
140 // This may mean we read EXIF data twice on initial upload.
141 $meta = $this->getMetadata( $image, $path );
142 $rotation = $this->getRotationForExif( $meta );
144 if ($rotation == 90 || $rotation == 270) {
145 $width = $gis[0];
146 $gis[0] = $gis[1];
147 $gis[1] = $width;
149 return $gis;
153 * On supporting image formats, try to read out the low-level orientation
154 * of the file and return the angle that the file needs to be rotated to
155 * be viewed.
157 * This information is only useful when manipulating the original file;
158 * the width and height we normally work with is logical, and will match
159 * any produced output views.
161 * @param $file File
162 * @return int 0, 90, 180 or 270
164 public function getRotation( $file ) {
165 global $wgEnableAutoRotation;
166 if ( !$wgEnableAutoRotation ) {
167 return 0;
170 $data = $file->getMetadata();
171 return $this->getRotationForExif( $data );
175 * Given a chunk of serialized Exif metadata, return the orientation as
176 * degrees of rotation.
178 * @param string $data
179 * @return int 0, 90, 180 or 270
180 * @fixme orientation can include flipping as well; see if this is an issue!
182 protected function getRotationForExif( $data ) {
183 if ( !$data ) {
184 return 0;
186 wfSuppressWarnings();
187 $data = unserialize( $data );
188 wfRestoreWarnings();
189 if ( isset( $data['Orientation'] ) ) {
190 # See http://sylvana.net/jpegcrop/exif_orientation.html
191 switch ( $data['Orientation'] ) {
192 case 8:
193 return 90;
194 case 3:
195 return 180;
196 case 6:
197 return 270;
198 default:
199 return 0;
202 return 0;