w/s
[lhc/web/wiklou.git] / includes / media / ExifBitmap.php
1 <?php
2 /**
3 * @file
4 * @ingroup Media
5 */
6
7 /**
8 * Stuff specific to JPEG and (built-in) TIFF handler.
9 * All metadata related, since both JPEG and TIFF support Exif.
10 *
11 * @ingroup Media
12 */
13 class ExifBitmapHandler extends BitmapHandler {
14
15 const BROKEN_FILE = '-1'; // error extracting metadata
16 const OLD_BROKEN_FILE = '0'; // outdated error extracting metadata.
17
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;
24 }
25
26 $avoidHtml = true;
27
28 if ( !is_array( $metadata ) ) {
29 $metadata = unserialize( $metadata );
30 }
31 if ( !isset( $metadata['MEDIAWIKI_EXIF_VERSION'] ) || $metadata['MEDIAWIKI_EXIF_VERSION'] != 2 ) {
32 return $metadata;
33 }
34
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] . ')';
45 }
46
47 // ContactInfo also has to be dealt with specially
48 if ( isset( $metadata['Contact'] ) ) {
49 $metadata['Contact'] =
50 FormatMetadata::collapseContactInfo(
51 $metadata['Contact'] );
52 }
53
54 foreach ( $metadata as &$val ) {
55 if ( is_array( $val ) ) {
56 $val = FormatMetadata::flattenArray( $val, 'ul', $avoidHtml );
57 }
58 }
59 $metadata['MEDIAWIKI_EXIF_VERSION'] = 1;
60 return $metadata;
61 }
62
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;
68 }
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;
74 }
75 if ( $metadata === self::BROKEN_FILE ) {
76 return self::METADATA_GOOD;
77 }
78 wfSuppressWarnings();
79 $exif = unserialize( $metadata );
80 wfRestoreWarnings();
81 if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) ||
82 $exif['MEDIAWIKI_EXIF_VERSION'] != Exif::version() )
83 {
84 if ( isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) &&
85 $exif['MEDIAWIKI_EXIF_VERSION'] == 1 )
86 {
87 //back-compatible but old
88 wfDebug( __METHOD__.": back-compat version\n" );
89 return self::METADATA_COMPATIBLE;
90 }
91 # Wrong (non-compatible) version
92 wfDebug( __METHOD__.": wrong version\n" );
93 return self::METADATA_BAD;
94 }
95 return self::METADATA_GOOD;
96 }
97
98 /**
99 * @param $image File
100 * @return array|bool
101 */
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 )
107 {
108 // So we don't try and display metadata from PagedTiffHandler
109 // for example when using InstantCommons.
110 return false;
111 }
112
113 $exif = unserialize( $metadata );
114 if ( !$exif ) {
115 return false;
116 }
117 unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
118 if ( count( $exif ) == 0 ) {
119 return false;
120 }
121 return $this->formatMetadataHelper( $exif );
122 }
123
124 function getMetadataType( $image ) {
125 return 'exif';
126 }
127
128 /**
129 * Wrapper for base classes ImageHandler::getImageSize() that checks for
130 * rotation reported from metadata and swaps the sizes to match.
131 *
132 * @param File $image
133 * @param string $path
134 * @return array
135 */
136 function getImageSize( $image, $path ) {
137 global $wgEnableAutoRotation;
138 $gis = parent::getImageSize( $image, $path );
139
140 // Don't just call $image->getMetadata(); FSFile::getPropsFromPath() calls us with a bogus object.
141 // This may mean we read EXIF data twice on initial upload.
142 if ( $wgEnableAutoRotation ) {
143 $meta = $this->getMetadata( $image, $path );
144 $rotation = $this->getRotationForExif( $meta );
145 } else {
146 $rotation = 0;
147 }
148
149 if ($rotation == 90 || $rotation == 270) {
150 $width = $gis[0];
151 $gis[0] = $gis[1];
152 $gis[1] = $width;
153 }
154 return $gis;
155 }
156
157 /**
158 * On supporting image formats, try to read out the low-level orientation
159 * of the file and return the angle that the file needs to be rotated to
160 * be viewed.
161 *
162 * This information is only useful when manipulating the original file;
163 * the width and height we normally work with is logical, and will match
164 * any produced output views.
165 *
166 * @param $file File
167 * @return int 0, 90, 180 or 270
168 */
169 public function getRotation( $file ) {
170 global $wgEnableAutoRotation;
171 if ( !$wgEnableAutoRotation ) {
172 return 0;
173 }
174
175 $data = $file->getMetadata();
176 return $this->getRotationForExif( $data );
177 }
178
179 /**
180 * Given a chunk of serialized Exif metadata, return the orientation as
181 * degrees of rotation.
182 *
183 * @param string $data
184 * @return int 0, 90, 180 or 270
185 * @fixme orientation can include flipping as well; see if this is an issue!
186 */
187 protected function getRotationForExif( $data ) {
188 if ( !$data ) {
189 return 0;
190 }
191 wfSuppressWarnings();
192 $data = unserialize( $data );
193 wfRestoreWarnings();
194 if ( isset( $data['Orientation'] ) ) {
195 # See http://sylvana.net/jpegcrop/exif_orientation.html
196 switch ( $data['Orientation'] ) {
197 case 8:
198 return 90;
199 case 3:
200 return 180;
201 case 6:
202 return 270;
203 default:
204 return 0;
205 }
206 }
207 return 0;
208 }
209 }
210