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