34a1f51156e93fd6a45375c540f20b5cd2c4f9bb
[lhc/web/wiklou.git] / includes / media / ExifBitmap.php
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 * @ingroup Media
29 */
30 class ExifBitmapHandler extends BitmapHandler {
31
32 const BROKEN_FILE = '-1'; // error extracting metadata
33 const OLD_BROKEN_FILE = '0'; // outdated error extracting metadata.
34
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;
41 }
42
43 $avoidHtml = true;
44
45 if ( !is_array( $metadata ) ) {
46 $metadata = unserialize( $metadata );
47 }
48 if ( !isset( $metadata['MEDIAWIKI_EXIF_VERSION'] ) || $metadata['MEDIAWIKI_EXIF_VERSION'] != 2 ) {
49 return $metadata;
50 }
51
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] . ')';
62 }
63
64 // ContactInfo also has to be dealt with specially
65 if ( isset( $metadata['Contact'] ) ) {
66 $metadata['Contact'] =
67 FormatMetadata::collapseContactInfo(
68 $metadata['Contact'] );
69 }
70
71 foreach ( $metadata as &$val ) {
72 if ( is_array( $val ) ) {
73 $val = FormatMetadata::flattenArray( $val, 'ul', $avoidHtml );
74 }
75 }
76 $metadata['MEDIAWIKI_EXIF_VERSION'] = 1;
77 return $metadata;
78 }
79
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;
85 }
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;
91 }
92 if ( $metadata === self::BROKEN_FILE ) {
93 return self::METADATA_GOOD;
94 }
95 wfSuppressWarnings();
96 $exif = unserialize( $metadata );
97 wfRestoreWarnings();
98 if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) ||
99 $exif['MEDIAWIKI_EXIF_VERSION'] != Exif::version() )
100 {
101 if ( isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) &&
102 $exif['MEDIAWIKI_EXIF_VERSION'] == 1 )
103 {
104 //back-compatible but old
105 wfDebug( __METHOD__.": back-compat version\n" );
106 return self::METADATA_COMPATIBLE;
107 }
108 # Wrong (non-compatible) version
109 wfDebug( __METHOD__.": wrong version\n" );
110 return self::METADATA_BAD;
111 }
112 return self::METADATA_GOOD;
113 }
114
115 /**
116 * @param $image File
117 * @return array|bool
118 */
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 )
124 {
125 // So we don't try and display metadata from PagedTiffHandler
126 // for example when using InstantCommons.
127 return false;
128 }
129
130 $exif = unserialize( $metadata );
131 if ( !$exif ) {
132 return false;
133 }
134 unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
135 if ( count( $exif ) == 0 ) {
136 return false;
137 }
138 return $this->formatMetadataHelper( $exif );
139 }
140
141 function getMetadataType( $image ) {
142 return 'exif';
143 }
144
145 /**
146 * Wrapper for base classes ImageHandler::getImageSize() that checks for
147 * rotation reported from metadata and swaps the sizes to match.
148 *
149 * @param File $image
150 * @param string $path
151 * @return array
152 */
153 function getImageSize( $image, $path ) {
154 global $wgEnableAutoRotation;
155 $gis = parent::getImageSize( $image, $path );
156
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;
164 }
165
166 if ($rotation == 90 || $rotation == 270) {
167 $width = $gis[0];
168 $gis[0] = $gis[1];
169 $gis[1] = $width;
170 }
171 return $gis;
172 }
173
174 /**
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.
178 *
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.
182 *
183 * @param $file File
184 * @return int 0, 90, 180 or 270
185 */
186 public function getRotation( $file ) {
187 global $wgEnableAutoRotation;
188 if ( !$wgEnableAutoRotation ) {
189 return 0;
190 }
191
192 $data = $file->getMetadata();
193 return $this->getRotationForExif( $data );
194 }
195
196 /**
197 * Given a chunk of serialized Exif metadata, return the orientation as
198 * degrees of rotation.
199 *
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!
204 */
205 protected function getRotationForExif( $data ) {
206 if ( !$data ) {
207 return 0;
208 }
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;
223 }
224 }
225 return 0;
226 }
227 }
228